Android Tutorial
Software Setup and Configuration
Android Studio
File Structure
Components
Core Topics
Layout
View
Button
Intent and Intent Filters
Toast
RecyclerView
Fragments
Adapters
Other UI Component
Image Loading Libraries
Date and Time
Material Design
Bars
Working with Google Maps
Chart
Animation
Database
Advance Android
Jetpack
Architecture
App Publish
App Monetization
Creating a multi-screen app in Android typically involves creating multiple activities, where each activity represents a screen. The process of navigating from one activity to another is achieved using Intents.
Here's a simple guide to create an Android app with two screens:
Start by creating a new project in Android Studio.
By default, Android Studio creates an activity for you, which we'll consider as the first screen. Let's assume it's named MainActivity
.
Open res/layout/activity_main.xml
and create a simple layout with a button to navigate to the second screen:
<Button android:id="@+id/btnGoToSecondScreen" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Go to Second Screen" android:layout_gravity="center"/>
To create a second screen, you'll need to create another activity.
New
-> Activity
-> Empty Activity
.SecondActivity
and click Finish
.Now, design your second screen by editing the res/layout/activity_second.xml
file. For this example, simply add a TextView
:
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="This is the Second Screen" android:layout_gravity="center"/>
In your MainActivity.java
, set up an OnClickListener
for the button to navigate to the SecondActivity
:
import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button btnGoToSecondScreen = findViewById(R.id.btnGoToSecondScreen); btnGoToSecondScreen.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // Create an Intent to navigate to SecondActivity Intent intent = new Intent(MainActivity.this, SecondActivity.class); startActivity(intent); } }); } }
When you added SecondActivity
using Android Studio, it should automatically be added to the AndroidManifest.xml
. If not, make sure it's present:
<activity android:name=".SecondActivity"></activity>
Start the app in an emulator or on a real device. You should see the first screen (MainActivity
). When you click on the button, you'll be navigated to the second screen (SecondActivity
).
This is a simple demonstration of creating a multi-screen app. Depending on your needs, you can add more screens, use fragments for better modularization and screen management, implement transitions between screens, or use the Navigation Component for more complex navigation patterns.
Building a multi-screen app in Android Studio:
To build a multi-screen app in Android Studio, create multiple activities or fragments representing different screens. Use intents to navigate between activities or fragment transactions to switch between fragments.
Navigating between activities in Android:
Use the Intent
class to navigate between activities. For example, to start a new activity:
Intent intent = new Intent(CurrentActivity.this, NextActivity.class); startActivity(intent);
Handling back stack in Android multi-screen app:
The back stack manages the navigation history. Use the addToBackStack
method when performing fragment transactions to add them to the back stack:
getSupportFragmentManager().beginTransaction() .replace(R.id.fragment_container, newFragment) .addToBackStack(null) .commit();
Passing data between screens in Android:
Use Intent
extras for activities or bundle arguments for fragments to pass data between screens. For example:
// Activity A Intent intent = new Intent(this, ActivityB.class); intent.putExtra("key", value); startActivity(intent); // Activity B Intent intent = getIntent(); String data = intent.getStringExtra("key");
TabLayout and ViewPager for multiple screens in Android:
Use TabLayout
and ViewPager
to implement swipeable tabs for multiple screens. Link them together using a ViewPager
adapter. Example:
ViewPager viewPager = findViewById(R.id.viewPager); TabLayout tabLayout = findViewById(R.id.tabLayout); viewPager.setAdapter(new MyPagerAdapter(getSupportFragmentManager())); tabLayout.setupWithViewPager(viewPager);
Implementing a navigation drawer in Android app:
Create a navigation drawer for multiple screens using DrawerLayout
and NavigationView
. Example:
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/drawerLayout" android:layout_width="match_parent" android:layout_height="match_parent"> <!-- Your main content --> <com.google.android.material.navigation.NavigationView android:id="@+id/navigationView" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="start" app:menu="@menu/drawer_menu"/> </androidx.drawerlayout.widget.DrawerLayout>
Creating a responsive layout for various screen sizes in Android:
Utilize different layout folders (e.g., layout
, layout-sw600dp
, layout-sw720dp
) and provide different XML layouts for various screen sizes. Use constraints, guidelines, and weights for flexible UIs.