For some reason I cannot use xml layout files.
But I need to create an Android application for the tablet.
I decided to use fragments.
I want to create the same layout that this xml generates:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent"> <fragment class="com.example.ItemsList" android:id="@+id/items_list" android:layout_weight="1" android:layout_width="0dp" android:layout_height="fill_parent" /> <FrameLayout android:id="@+id/item_details" android:layout_weight="1" android:layout_width="0dp" android:layout_height="fill_parent" android:background="?android:attr/detailsElementBackground" /> </LinearLayout>
But I have problems adding fragments to my linearLayout:
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(createUI()); } private View createUI() { LinearLayout layout = new LinearLayout(this); layout.setOrientation(LinearLayout.HORIZONTAL); layout.setLayoutParams(new LinearLayout.LayoutParams(AbsListView.LayoutParams.FILL_PARENT, AbsListView.LayoutParams.FILL_PARENT)); layout.setId(0x101); { FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction(); fragmentTransaction.add(0x101, new ItemsList()); fragmentTransaction.add(0x101, new ItemDetails()); fragmentTransaction.commit(); } return layout; }
In fact, I can’t even create a LinearLayout with two identical fragments:
... fragmentTransaction.add(0x101, new ItemsList()); fragmentTransaction.add(0x101, new ItemsList()); ...
Please, help
Btw I can’t understand why we need to declare “FrameLayout” for the itemDetails fragment, but the “fragment” is enough for the itemList ListFragment?
UPD:
To do this, simply add the third parameter:
... fragmentTransaction.add(0x101, new ItemsList(), "uniqueTag1"); fragmentTransaction.add(0x101, new ItemsList(), "uniqueTag2"); ...
The default value for the tag parameter is null, so I tried to create two different elements with the same identifiers. Thanks p-lo for his comment.