One activity to another new activity in android

How to go on activity to another activity using intent?

What by mean one activity to another activity in android studio?

  • In simple words, when you click on any link or button in android studio redirect to another activity.
  • For example, you open an app on your mobile, then first show a splash screen.
  • Aftershow Application Home Page.
  • Different buttons or links are given on the application home page. When you click on a button, it gets redirected to another activity.
  • When you click listener event at that time, a new activity is opened.

How to make one activity to another new activity?

  • Here everything is explained in a simple way, so one can easily understand.
  • Moving from one activity to another is not so difficult.
  • So let’s start making one activity into another activity.

Step 1:

Start your project in android studio.

Step 2:

The default activity in android studio is MainActivity.java.

Step 3:

In activity_main.xml we’ll design a simple button.

Follow code

    <Button
        android:id="@+id/btnsubmit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="submit"
        android:text="submit" />

  • we used onClick attribute to define method instance calling setOnClickListner() method.
  • After you don’t need uses setOnClickListener() method.
  • Also, you can use ID attribute. If you used onClick you no need to make separate method in the java class file.
  • Using onClick we reduce code then anonymous java class.

In java class, we used the simple intent method

startActivity ( new Intent ( MainActivity.this,Main2Activity.class ) );
 
or Intent inte=new Intent(MainActivity.this,Main2Activity.class);
 
<strong>startActivity(inte);
  • Intent: this is an object that provides runtime binding between two activities.
  • Inte: object of Intent.
  • MainActivity.this: current activity.
  • Main2Activity.class: second activity

Example

Now, see the full example of MainActivity.java and activity_main.xml.

activity_main.xml

<?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:gravity="center"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/btnsubmit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="submit"
        android:text="submit" />
</LinearLayout>

Now, Go to the java class file follow the code.

MainActivity.java

package com.example.one2another;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
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 submit(View view) {
        startActivity ( new Intent ( MainActivity.this,Main2Activity.class ) );
    }
}

When users click on the button fired another activity. So, we need another activity.

Now create an empty activity.

Create new empty activity file ⇾ new ⇾ activity ⇾ Empty Activity.

create new empty activity
  • Rename if you want to other wise remain MainActivity2.java.
  • Now, the second activity MainActivity2.java and activity_main2.xml.

In activity_main.xml we pass a simple message in textview.

<TextView
    android:id="@+id/firstactivity"
    android:onClick="firstactivity"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Second Activity"
    android:textSize="30sp">
</TextView>

When clicking the button, show the message “Second Activity”

the activity_main2.xml file below shows a full example.

activity_main2.xml

<?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:gravity="center"
    tools:context=".Main2Activity">
<TextView
    android:id="@+id/firstactivity"
    android:onClick="firstactivity"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Second Activity"
    android:textSize="30sp">
</TextView>
</LinearLayout>

Now, Run your app.

one activity to second
One Activity to second activity

shown as below output

Watch the video on YouTube

Leave a Reply