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 are not complex, with one text view and a button. When you click the button, it will go to another screen. Below will introduce them in detail.
Example File Structure And Source Code.
You can read the article Setup Android App Development Environment, Android Hello World Example Project File Structure if you do not know the basic concepts and structure. Below is this android example project file structures, the core source files are SwitchScreenActivity1.java, SwitchScreenActivity2.java, activity_switch_screen1.xml, activity_switch_screen2.xml.
./ ├── app │ ├── main │ │ ├── AndroidManifest.xml │ │ ├── java │ │ │ └── com │ │ │ └── dev2qa │ │ │ ├── example │ │ │ │ └── activity │ │ │ │ ├── SwitchScreenActivity1.java │ │ │ │ └── SwitchScreenActivity2.java │ │ └── res │ │ ├── layout │ │ │ ├── activity_switch_screen1.xml │ │ │ └── activity_switch_screen2.xml
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>