+ 3
how can I go to the next activity with a button in android studio?
I know how create an empty activity I just don't know how to access the new activity and what are the codes?
9 odpowiedzi
+ 4
Main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<Button
android:text="Click"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn"/>
</LinearLayout>
+ 4
This is second layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:gravity="center">
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Welcome"
android:textSize="56sp"/>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="To The"
android:textSize="56sp"
android:layout_margin="10dp"/>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="SecondActivty"
android:textSize="56sp"/>
</LinearLayout>
+ 4
MainActvity.java
package com.mycompany.myapp3;
import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;
import android.content.*;
public class MainActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn=findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View p1)
{
Intent intent=new Intent(MainActivity.this,secondactivity.class);
startActivity(intent);
}
});
}
}
+ 4
secondactvity.java
package com.mycompany.myapp3;
import android.os.*;
import android.app.*;
public class secondactivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.secondactvity);
}
}
+ 3
hamid this could be pretty picky stuff for beginners. You need a simple
Intent, a second layout that mean you have to create another layout and a another class that's hold the layout u creat that means the second class will hold the UI for your new Activity.
+ 3
And add this to your Manifest.xml
<activity android:name=".secondactivity">
</activity>
+ 3
You can also find this basic things on internet you can search for more.
+ 3
0_O-[Mägár_Sám_Äkà_Nüllpøïntêr_Èxëcéptïön]~~
THANK YOU SO MUCH
it was enough for me because I searched a bit of it and wasn't this much clear for me.
so valuable information thank again 🙏🙏
+ 2
0_O-[Mägár_Sám_Äkà_Nüllpøïntêr_Èxëcéptïön]~~
you know how to do this??