Hi guys, In this article , We will learn how to open a new Activity from a Button click.
For this we will create a new Intent and pass it to the startActivity method.
Step 1 − Create a new project in Android Studio, go to File and click on New Project and type your project name and save
Step 2 − Now, Open res/layout/activity_main.xml and add the following code as given below:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Open new Activity!"
android:textSize="26dp"/></RelativeLayout>Step 3 − Create New Activity and then open main_activity2.xml<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:padding="10dp"
android:text="Welcome to New Activity"
android:textColor="@color/colorPrimaryDark"
android:textSize="32dp"/>
</RelativeLayout>Step 4 − Add the following code to src/MainActivity.javaimport android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openNewActivity();
}
});
}
public void openNewActivity(){
Intent intent = new Intent(this, MainActivity2.class);
startActivity(intent);
}
}Step 5 − Add the following code to src/MainActivity2.javaimport android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class NewActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
}
}Step 6 − Now in Mainfest.xml as usual you don't have to do any edition.<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="app.com.sample">
<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=".MainActivity2"></activity>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>Now You can run your app.subscribe myblog:
https://hamrodesigns.blogspot.com/