Open new Activity on Button Click on Android Studio

Hello,developers!

If you are new to Android Development,it can be quite overwhelming.Android is a vast library with lots of features and services.The official documentation can be found here.



The official android documentation can be quite intimidating for beginners.It Is designed for somewhat experienced programmers.

Today I will show you how you can open a new activity in android studio using a button.

Here’s our activity_main.xml  file.


<?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:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="realtales.myapplication.MainActivity">

    <Button android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/button1" android:text="Run" android:onClick="openNextActivity"/>
        </RelativeLayout>
</RelativeLayout>


 

We implement a method on onClick called openNextActivity.Nothing’s new here.

Now,make a second activity.

You do this by :

1)Go to your Java folder.Then,right click on it and scroll below to New Activity and click on Empty Activity.Name the activity what you want.We’ll name it SecondActivtiy.java.

2)Even though android automatically does this in latest versions,if you are running older versions,you need to add your activity to your AndroidManifest.xml under Manifests.

It would look something like this:

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="realtales.myapplication">

    <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>


Now,we have to implement our method openNextActivity  on our MainActivity.java.

You can do this by implementing the following method:

MainActivty.java


public void openNextActivity (View v){
Intent intent=new Intent(this,SecondActivty.class);
startActivity(intent);

}

The whole code would look like this:


package realtales.myapplication;

import android.content.Intent;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {

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

}
public void openNextActivity(View v){
Intent intent=new Intent(this,SecondActivity.class);
startActivity(intent);

}

}

So,what’s new here?

The Intent keyword.

Intent is just like a messenger.It passes the message to the activity you are trying to interact with.It can be used to pass texts,images and actions from one activity to another.



It takes in two arguments.The first being the reference to the class  you are trying to navigate from and the second being the class which you are trying to navigate to.

This keyword can be replaced with getApplicationContext() as well.

After making a new intent and passing the argument,you need to implement the startActivity method with your Intent variable as a parameter.


Leave a Reply

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

%d bloggers like this: