This example demonstrates how to switch between different Activities in Android. Create a new project . Here we need two Activities to switch between. Add these following code to MainActivity.java
public class MainActivity extends AppCompatActivity {
Button click_first_btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
click_first_btn = findViewById(R.id.click_first_btn);
click_first_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(),SecondActivity.class);
startActivity(i);
}
});
}
}
Now create another Activity and named that SecondActivity.java. Add this code to that
public class SecondActivity extends AppCompatActivity {
Button click_second_btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
click_second_btn = findViewById(R.id.click_second_btn);
click_second_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(SecondActivity.this,MainActivity.class);
startActivity(i);
}
});
}
}
This example demonstrates how to switch between different Activities in Android. Create a new project . Here we need two Activities to switch between. Add these following code to MainActivity.java
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Im First Activity" />
<Button
android:id="@+id/click_first_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click to go Second"/>
</LinearLayout>
This example demonstrates how to switch between different Activities in Android. Create a new project . Here we need two Activities to switch between. Add these following code to MainActivity.java
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
tools:context=".SecondActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Im Second Activity" />
<Button
android:id="@+id/click_second_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click to go First"/>
</LinearLayout>
This example demonstrates how to switch between different Activities in Android. Create a new project . Here we need two Activities to switch between. Add these following code to MainActivity.java