Android: Dynamically replacing fragments on a custom layout -
i trying programatically add fragments activity custom layout, want change them on button clicks.
i read tutorials , managed add them when activity has linearlayout. however, when use mine fragment not show up.
it not begin laid out, not know how this. laying out framelayout want put them, not know fragments have laid out. here code (i want display blogfragment framelayout placed):
main_activity.xml
<com.example.myfirstapp.mainlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/main_window" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".mainactivity" > <fragment android:name = "com.example.myfirstapp.buttonsfragment" android:id = "@+id/buttons" android:layout_width="match_parent" android:layout_height="30dp" android:layout_weight="1" /> <fragment android:name = "com.example.myfirstapp.signaturefragment" android:id = "@+id/signature" android:layout_width="match_parent" android:layout_height="20dp" android:background="@color/orange" android:layout_weight="1" /> <framelayout android:id="@+id/central_window" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#00ff00" android:layout_weight="1" /> </com.example.myfirstapp.mainlayout>
mainlayout.java
public class mainlayout extends viewgroup { @override protected void onlayout(boolean changed, int l, int t, int r, int b) { final int w = r-l; final int h = b-t; getchildat(0).layout(0, 0, w, 30); getchildat(1).layout(0, h-20, w, h); getchildat(2).layout(0, 30, w, h-20); } /* * constructors */ }
mainactivity.java
@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); blogfragment blogfragment = new blogfragment(); fragmentmanager fragmentmanager = getfragmentmanager(); fragmenttransaction ft = fragmentmanager.begintransaction(); ft.add(r.id.central_window, blogfragment); ft.commit(); }
Comments
Post a Comment