Latest News

How To Move From One Activity To Another Activity In Android Studio

In this tutorial we will learn how to open new activity when a button is clicked using android studio. When button in an activity(Main Activity) is clicked the new activity(New Activity) will be opened..

Step 1: Create a new Project of open new project:


Step 2: Create a new Activity(e.g. Empty Activity)

Note: after clicking we will move to this activity...


Step 3: 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"><Buttonandroid:id="@+id/button"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Open New Activity" /></LinearLayout>

Step 4: MainActivity.java

package com.blogspot.devofandroid.myapplication;import android.content.Intent;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.Toast;public class MainActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//initializeButton mButton = (Button)findViewById(R.id.button);//handle onClickmButton.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {//intent to start NewActivitystartActivity(new Intent(MainActivity.this , NewActivity.class));}});}}

Step 5: actiavity_new.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=".NewActivity"><TextViewandroid:text="New Activity"android:textSize="30sp"android:layout_width="wrap_content"android:layout_height="wrap_content" /></LinearLayout>


Step 6: NewActivity.java

package com.blogspot.devofandroid.myapplication;import android.support.v7.app.ActionBar;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;public class NewActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_new);}}

Step 7: Output


0 Response to "How To Move From One Activity To Another Activity In Android Studio"