This tutorial is a step by step explanation of how to create a new activity and start the new activity on button click from our main activity or first activity. For example, for some reason, you want to open a new activity or the next activity using your first activity.
Please follow the steps given below to start a new activity on button click:
Step 1) Open a project which has only one activity which is our main activity.
Step 2) Now to add a new activity to your app, go to your app and select ‘android’
Step 3) And in the app category you just need to go to this ‘res’ folder ( ‘res’ stands for resources folder and inside this resources folder), there is a folder called ‘layout’ just right click on this layout folder and go to ‘new’ and we are going to go to ‘activity’ and select a blank activity.
Step 4) So when you select the blank activity wizard will open. You can give a name to your activity and every other thing is by default created for you and just click ‘finish’.
Step 5) Once you click ‘finish’, there will be a second activity which will be created for you and you will be able to see an extra xml file created for your second activity in your layout folder of your resources directory.
Step 6) Similarly, in your Java folder you will be able to see that there will be a new class added to your Java folder which is secondactivity.java file
Step 7) In the manifest folder , there will be a file called andoidmanifest.xml file and in here also you will be able to see that new activity is added to your main manifest xml file.
Step 8) Now we are going to add in this manifest file where your main activity xml element is there and now you have this second activity tag. So by default, it’s a blank tag in a sense that it just has the name and the title but unlike this main activity, it doesn’t have this intent-filter tag. So what we are going to do is we are going to copy this intent filter from our main activity, so just copy it into the second activity and also change the name in the action and the category second activity.
Step 9) Now we are ready to create code to open the second activity from our first activity. So open your first activity and add a button which will be used to open my second activity. Just change the text to open second activity for example and leave the ID as default.
Step 10) Now go to your mainactivity.java file and create a button variable
public class MainActivity extends ActionBarActivity{
private static Button button_sdm;
}
Step 11) Now just below the onCreate method create a new method (this new method id not going to take any argument) and cast this button to its ID
Step 12) And now take button_sbm variable and add or set a listener to this button variable and inside this setOnClickListener create a new OnClickliListener.
Step 13) Now inside this onClick method we are going to create an object of the intent class. It helps us to open the new activity which is our second activity.The constructor of this intent takes the argument which is the package name of your second activity or whatever activity you want to open. So as you remember that we have changed the name of this action tag so copy that path and pass it as an argument of this intent constructor.
Step 14) Now just call this intent to start the activity by a method called startactivity which takes intent as an argument therefore just pass your intent variable and now everything is done.
Complete Code
public void onClickButtonListener(){
button_sdm = (Button)findViewById(R.id.button);
button_sdm.stOnClickListener(){
@Override
public void onClick(View v){
Intent intent = new Intent("com.example.program");
startActivity(intent);
}
}
}
Step 15) Now we just need to call this method inside our onCreate method so just copy the name of this method and just call it inside our main method.
protected void onCreate(Bundle savedinstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
onClickButtonListener();
}
Step 16) So now our activity is running now which is our main activity. So when we click our button the second activity opens. By this button we can go back to our first activity and once again when we click our button, it opens our second activity.
Read Next About How To Create Analog And Digital Clocks Android Application
Complete Code to Start New Activity on Button Click in Android
app>java>MainActivity.java
public class MainActivity extends ActionBarActivity{
private static Button button_sum;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
OnClickButtonListener();
}
public void OnClickButtonListener() {
button_sum = (Button)findViewById(R.id.button);
button_sum.setOnClickListener(
new View.setOnClickListener(){
@Override
public void onClick(View v) {
Intent intent = new Intent("com.example.programmingknowledge.thefirstapp.secondActivity");
startActivity(intent);
}
}
);
}
}
app>manifest>AndroidManifest.xml
<manifest>
<application
android:allowbackup="true"
android:icon="@drawable/ic_launcher"
android:label="TheFirstApp"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SecondActivity"
android:label="@string/title_activity_second">
<intent-filter>
<action android:name="com.example.programmingknowledge.thefirstapp.secondActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
app>res>layout>activity_second.xml
<?xml version="1.0" encoding="utf-8" ?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"
android:paddingBottom="16dp"
tools:context="com.example.programmingknowledge.thefirstapp.secondActivity">
<TextView
android:text="Hello World!"
android:layout_width="warp_content"
android:layout_height="warp_content" />
</RelativeLayout>