How to open layout on button click (android) -
how can open layout xml file when click on button in main.xml file?
so if have main.xml has button sying click here , click it, opens second.xml file (layout).
this tutorial explain need step step.
first create 2 layout:
main.xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#ffffff" > <textview android:layout_width="fill_parent" android:layout_height="wrap_content" android:textcolor="#000000" android:text="this activity 1" /> <button android:text="next" android:id="@+id/button01" android:layout_width="250px" android:textsize="18px" android:layout_height="55px"> </button> </linearlayout>
second.xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#ffffff" > <textview android:layout_width="fill_parent" android:layout_height="wrap_content" android:textcolor="#000000" android:text="this activity 2" /> <button android:text="previous" android:id="@+id/button02" android:layout_width="250px" android:textsize="18px" android:layout_height="55px"> </button> </linearlayout>
second add activity manifest file
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.rr" android:versioncode="1" android:versionname="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".activity1" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> <activity android:name=".activity2"></activity> </application> <uses-sdk android:minsdkversion="3" /> </manifest>
activity1.java
import android.app.activity; import android.content.intent; import android.os.bundle; import android.view.view; import android.widget.button; public class activity1 extends activity { /** called when activity first created. */ @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); button next = (button) findviewbyid(r.id.button01); next.setonclicklistener(new view.onclicklistener() { public void onclick(view view) { intent myintent = new intent(view.getcontext(), activity2.class); startactivityforresult(myintent, 0); } }); } }
to switch activity2 have to:
gets reference button id button01 on layout using
(button) findviewbyid(r.id.button01)
.create onclick listener button.
- and important part, creates “intent” start activity. intent needs 2 parameters: context , name of activity want start (activity2.class)
activity2.java
import android.app.activity; import android.content.intent; import android.os.bundle; import android.view.view; import android.widget.button; public class activity2 extends activity { /** called when activity first created. */ public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.second); button next = (button) findviewbyid(r.id.button02); next.setonclicklistener(new view.onclicklistener() { public void onclick(view view) { intent intent = new intent(); setresult(result_ok, intent); finish(); } }); }
Comments
Post a Comment