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 multiple Screen app in Android

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:

1. Create a New Android Project:

Start by creating a new project in Android Studio.

2. Design the First Screen:

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"/>

3. Create the Second Screen:

To create a second screen, you'll need to create another activity.

  • Right-click on your package directory in the Android Studio Project pane.
  • Navigate to New -> Activity -> Empty Activity.
  • Name it 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"/>

4. Implement Navigation Logic:

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);
            }
        });
    }
}

5. Update the AndroidManifest.xml:

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>

6. Run the App:

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.

  1. 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.

  2. 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);
    
  3. 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();
    
  4. 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");
    
  5. 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);
    
  6. 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>
    
  7. 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.