Moving from one activity to another activity in Android App using Intents. | Android Studio |


Moving from one activity to another in Android Studio.


Here, in this Android Studio Tutorial. So basically, we will start with basic thing in Android Studio and which is most important thing while you are making an app or any Android Studio project.
In, Android Studio we can move from one activity to another using Intents. So, let us take a look on What are Intents.
Intents are objects, from which you can request an action to perform from any other app component. Intent can be used to perform many actions in an Android App. But in this tutorial we will use Intent to start an Activity.

We will create a button and by on clicking it, new Activity will be started.

Let's implement this:

1. Create a New Project and Select Empty activity from the list. Your first activity will be created and by default it will be named as activity_main.xml. So, corresponding JAVA file will be created named as MainActivity.
2. Now we have to create an other activity (which we want to start when a user click a button in MainActivity.
3. Create an Activity by right clicking on layout (as shown in figure).
4. Now we have created Second Activity. Follow the next step.
5. Let us create a button inside the activity_main.xml

ADVERTISEMENT


Activity1.xml code

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Activity1"
android:textSize="25sp"
android:layout_marginTop="20dp"
android:layout_centerHorizontal="true"
/>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start Activity 2"
android:layout_centerInParent="true"
/>

</RelativeLayout>

6. Now our activity_main.xml code is ready and we have button now. 7. All we have now to write JAVA code in the java file of that from where we will start any other activity. In nutshell, we will write java code in MainActivity.java file.

MainActivity.java Code

package com.example.activity1;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

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

Button b1 = findViewById(R.id.button);

b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(MainActivity.this ,MainActivity2.class);
startActivity(i);

}
});
}
}

activity_main2.xml code

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".MainActivity2">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Activity2"
android:textSize="25sp"
android:layout_centerInParent="true"
/>

</RelativeLayout>
8. Now we have done Intent code to start a New activity click on Button. So, Run the app on Emulator or any Physical device.
If you like this tutorial, share it with your friends.
Previous Post Next Post

Contact Form