Replace fragments in Android -
i can't replace fragments using codes below. mean second fragment appeared under first fragment. first fragmnent didn't hide expected. wrong here? lot!
my activity :
public class mainactivity extends activity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); final fragmentmanager fm = this.getfragmentmanager(); final fragment fragmenttwo = new fragmenttwo(); button fmchangebutton = (button) this.findviewbyid(r.id.fmchangebutton); fmchangebutton.setonclicklistener(new onclicklistener(){ @override public void onclick(view v) { fragmenttransaction ft = fm.begintransaction(); ft.replace(r.id.fragment1, fragmenttwo); ft.commit(); } }); }; };
activity_main.xml
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <button android:id="@+id/fmchangebutton" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="change fragment" /> <fragment android:id="@+id/fragment1" android:name="com.example.fragementex.fragmentone" android:layout_width="match_parent" android:layout_height="wrap_content" /> </linearlayout>
fragmnetone.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:orientation="vertical" > <edittext android:id="@+id/edittext1" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="clip_horizontal" android:ems="10" android:gravity="center_vertical|center_horizontal|top|fill_vertical" android:text="this fragment one" > </edittext> </linearlayout>
fragmnettwo.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:orientation="vertical" > <edittext android:id="@+id/edittext1" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="clip_horizontal" android:ems="10" android:gravity="center_vertical|center_horizontal|top|fill_vertical" android:text="this fragment two" > </edittext> </linearlayout>
you cannot replace fragment attached layout, need create host container framelayout
this:
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <button android:id="@+id/fmchangebutton" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="change fragment" /> <framelayout android:id="@+id/frame_layout" android:layout_width="match_parent" android:layout_height="wrap_content" /> </linearlayout>
then, able - first - add
, display first fragment , replace
second follows:
@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); // set layout contains framelayout setcontentview(r.layout.activity_main); final fragmentmanager fm = this.getfragmentmanager(); final fragment fragmentone = new fragmentone(); final fragment fragmenttwo = new fragmenttwo(); // if first initialization if(savedinstancestate == null) { // display "add" first fragment this.getfragmentmanager().begintransaction() .add(r.id.frame_layout, fragmentone).commit(); } button fmchangebutton = (button) this.findviewbyid(r.id.fmchangebutton); fmchangebutton.setonclicklistener(new onclicklistener(){ @override public void onclick(view v) { fragmenttransaction ft = fm.begintransaction(); // "replace" fragment inside framelayout ft.replace(r.id.frame_layout, fragmenttwo); ft.commit(); } }); }
Comments
Post a Comment