Create second activity and connect it with main activity with a button click and and vice versa in Android Studio version 3.1.3.

Create new project with Application name: Start Another Activity; Minimum SDK: API 21Android 5.0 (Lollipop).

In app->java->MainActivity.java write the code:

package com.app.toyo.startanotheractivity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
        View v = findViewById(R.id.button1);
v.setOnClickListener(this);
}
    @Override
public void onClick(View arg0) {
if (arg0.getId() == R.id.button1) {
//define a new Intent for the second Activity
Intent intent = new Intent(this, SecondActivity.class);
//start the second Activity
this.startActivity(intent);
}
}
}

In app->res->layout->activity_main.xml write the code:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
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">
    <Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

In app->java create New -> Java Class: SecondActivity.java and write the code:

package com.app.toyo.startanotheractivity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
public class SecondActivity extends AppCompatActivity implements View.OnClickListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
View v = findViewById(R.id.button2);
v.setOnClickListener(this);
}
@Override
public void onClick(View arg0) {
if (arg0.getId() == R.id.button2) {
//define a new Intent for the second Activity
Intent intent = new Intent(this, MainActivity.class);
//start the second Activity
this.startActivity(intent);
}
}
}

In app->res->layout create New -> Layout resource file: activity_second.xml and write the code:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
    <Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button2"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

In manifest->AndroidManifest.xml write the code:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.app.toyo.startanotheractivity">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
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="com.app.toyo.startanotheractivity.SecondActivity"
            android:label="Second Activity">
        </activity>
</application>
</manifest>

In app->res->values->strings.xml write the code:

<resources>
<string name="app_name">Start another activity</string>
<string name="button1">CLICK</string>
<string name="button2">BACK</string>
</resources>

Run app in mobile device or Emulator (API 21 Android 5.0) and switch between main activity and second activity with buttons.

Follow Video Tutorial:

 

Download project with Source Code from here:
http://android-coffee.com/wp-content/uploads/projecs/Startanotheractivity.7z

Please subscribe here to our YouTube channel. Thank you!
Check next tutorials to learn all about Android.
For further questions leave a message.

Leave a Reply

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

Blue Captcha Image Refresh

*