Start Another Activity From Button in Android
Saturday, November 18, 2023, 6 PM
In this Android tutorial, we will learn how to open a new Activity from a button click on android app. I hope you have already learned to make your first android app,
So let's learn how to open an activity from button click on android
Create a new Button and TextView in your current activity_main.xml. Note the code in red line
The code for Button and TextView
<Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="144dp" android:onClick="open2" android:text="Button1" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" /> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:textSize="30dp" android:layout_centerHorizontal="true" android:layout_marginTop="129dp" android:text="Activity one" />
// Note this code android:onClick="open2"
This onClick code is used to set action for the button, we will call the open2 from MainActivity java. Next we need create a new Activity, this will be the activity that will be opened When we click on button.
Creating a new activity
Create a new activity, an empty activityPlace your mouse pointer on the app folder and click to see the option menu, select new > Activity then select Empty activity
give name to this new activity as Main2Activity, now click finish. This is the activity that will be opened when we click on the button
open activity_main2.xml and add a new TextView
The TextView code
Add this code for TextView, we will see this in the second activity<TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="30dp" android:text="Activity 2 opened" android:layout_centerVertical="true" android:layout_centerHorizontal="true" /> </RelativeLayout>
Adding code intent
Now open the Main2Activity javaAdd this code like you see in this photo, in the oncreate method
public void open2 (View view){ Intent intent = new Intent(MainActivity.this,Main2Activity.class); startActivity(intent); }}The full code should be like this
package com.example.myapp.myapplication; import android.content.Intent; import android.support.v7.app.AppCompatActivity; 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 open2 (View view){ Intent intent = new Intent(MainActivity.this,Main2Activity.class); startActivity(intent); } }
That's it! our app is ready now. let's test it on your device. So connect your phone to your computer with USB cable and run the app
Testing the App
now the app will be launched on your device, just click that buttonThe second activity is now launched when you clicked the button!
No comments found.