For some reasons I can’t use xml layout files.
But I need to create a tablet android app.
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 with 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;
}
Actually I can’t even create LinearLayout with two identical fragments:
...
fragmentTransaction.add(0x101, new ItemsList());
fragmentTransaction.add(0x101, new ItemsList());
...
Please help
Btw I can’t figure out why do we need to declare “FrameLayout” for itemDetails Fragment but “fragment” is enough for itemsList ListFragment?
UPD:
To do so one should just add third param:
...
fragmentTransaction.add(0x101, new ItemsList(), "uniqueTag1");
fragmentTransaction.add(0x101, new ItemsList(), "uniqueTag2");
...
Default value for the tag parameter is null, so I was trying to create two different elements with identical ids. Thanks to p-lo for his comment.
I’ve found the solution.
(the better way was added to the question. Thanks, p-lo)
If you want to place more than one fragment on activity you should create layouts for each fragment: