How to move from one activity to another activity in android studio | Android Learning Simple Guide

 In This Tutorial I Will show you how you can move from one activity to another activity in android studio

First -  design your main activity 

  1. In Android Studio, from the res/layout directory, edit the activity_main.xml file.
        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:id="@+id/firstAcivity"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fontFamily="cursive"
android:gravity="center|clip_vertical"
android:text="Click Here To Move Another Activity"
android:textColor="#03FFB8"
android:textSize="74sp"
android:textStyle="bold"
app:layout_constraintVertical_bias="0.0" />

</RelativeLayout>

    Java Code -
public class MainActivity extends AppCompatActivity {

TextView TV1;

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

// Initialize the text view
TV1 = findViewById(R.id.firstAcivity);

// set onClickListener on textView
TV1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// method to move one activity to another activity
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
}
});

}
}

2. Create a New activity Name - SecondActivity in second activity paste given code

Xml Code - 
<?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"
android:orientation="vertical"
tools:context=".SecondActivity">

<TextView
android:id="@+id/tv1s"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="cursive"
android:gravity="center"
android:onClick="showToast"
android:text="@string/tv1s"
android:textSize="36sp"
android:textStyle="bold" />

<TextView
android:id="@+id/tv2s"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="cursive"
android:gravity="center"
android:onClick="goBack"
android:text="@string/tv2s"
android:textSize="36sp"
android:textStyle="bold" />

</LinearLayout>
Java Code -
public class SecondActivity extends AppCompatActivity {

TextView tv1s, tv2s;

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

// Initialize text views
tv1s = findViewById(R.id.tv1s);
tv2s = findViewById(R.id.tv2s);

// set text on text views
tv1s.setText("This Is Seconds Activity");
tv2s.setText("Click Here To Go Back");
}



// method to move one activity to another activity
public void goBack(View view) {
Intent intent = new Intent(SecondActivity.this, MainActivity.class);
startActivity(intent);
}

// method to show a toast massage
public void showToast(View view) {
Toast.makeText(this, "This Is Second Activity", Toast.LENGTH_SHORT).show();
}
}

This is a simple tutorial to move from one activity to another activity in android studio 


Post a Comment

Previous Post Next Post