Android Activity Example – Switch Between Screens

Android activity is used to represent Screen. Normally one android app has more than one Screen ( activity ), but how to switch between them? This example will show you how to slip from one screen to another.

There are two activity class in this example, SwitchScreenActivity1.java represent screen one and SwitchScreenActivity2.java represent screen two. The components in the screen is not complex, one text view and a button. When you click the button, it will go to another screen. Below will introduce them in detail.

android example switch between activity screen one

android example switch between activity screen one.png

Example File Structure And Source Code.

You can read article Setup Android App Development EnvironmentAndroid Hello World Example Project File Structure if you do not know the basic concepts and structure.

android example source file structure

AndroidManifest.xml

<activity android:name="com.dev2qa.example.activity.SwitchScreenActivity1">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

<activity android:name="com.dev2qa.example.activity.SwitchScreenActivity2"></activity>

SwitchScreenActivity1.java

public class SwitchScreenActivity1 extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_switch_screen1);

        // Get go to screen two button.
        Button btnToScreen2 = (Button)this.findViewById(R.id.btnGotoScreen2);

        // Create a button click listener.
        ToScreen2ButtonListener btnClickListener = new ToScreen2ButtonListener();

        // Register OnClickListener to the button.
        btnToScreen2.setOnClickListener(btnClickListener);

    }

    class ToScreen2ButtonListener implements View.OnClickListener
    {
        @Override
        public void onClick(View v) {
            // When button clicked, get the intent object.
            // The intent object will tell android os start SwitchScreenActivity2 activity.
            Context context = v.getContext();
            Intent intent = new Intent(context, SwitchScreenActivity2.class);
            startActivity(intent);
        }
    }
}

activity_switch_screen1.xml

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    tools:layout_editor_absoluteX="8dp"
    tools:layout_editor_absoluteY="8dp">

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/holo_green_light"
        android:text="This is screen one. "
        android:textColor="@android:color/holo_red_light"
        android:textSize="100sp"
        tools:layout_editor_absoluteX="16dp"
        tools:layout_editor_absoluteY="-11dp" />

    <Button
        android:id="@+id/btnGotoScreen2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Go to screen two." />
</LinearLayout>

SwitchScreenActivity2.java

public class SwitchScreenActivity2 extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_switch_screen2);

        // Get go to screen one button.
        Button btnToScreen2 = (Button)this.findViewById(R.id.btnGotoScreen1);

        // Create button click listener.
        ToScreen1ButtonListener btnClickListener = new ToScreen1ButtonListener();

        // Register OnClickListener to the button.
        btnToScreen2.setOnClickListener(btnClickListener);
    }

    class ToScreen1ButtonListener implements View.OnClickListener
    {
        @Override
        public void onClick(View v) {
            // When button clicked, get the intent object.
            // The intent object will tell android os start SwitchScreenActivity1 activity.
            Context context = v.getContext();
            Intent intent = new Intent(context, SwitchScreenActivity1.class);
            startActivity(intent);
        }
    }
}

activity_switch_screen2.xml

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    tools:layout_editor_absoluteY="8dp"
    tools:layout_editor_absoluteX="8dp">

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@android:color/holo_green_dark"
        android:text="This is screen two."
        android:textColor="@color/colorPrimary"
        android:textSize="36sp"
        android:textStyle="bold" />

    <Button
        android:id="@+id/btnGotoScreen1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Back To Screen One." />


</LinearLayout>

Download “Android-Activity-Example-Switch-Between-Screens.zip” Android-Activity-Example-Switch-Between-Screens.zip – Downloaded 53 times – 3 KB

(Visited 372 times, 1 visits today)
READ :   Android ActionBar Tab Navigation Example

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.