Reputation: 631
Whenever i call the inflate method in "OnCreateView" in Fragment class, i expect to get a fragment with the ID specified in the android:id value. But I get a different id, the same of the containerID.
I'm trying to add fragments programmatically in a relativeLayout, and for the example shown here i have two fragments and one must have RelativeLayout.BELOW parameter set.
Here is the code, of the topsection fragment (ts_fragment):
The xml looks like this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:id="@+id/ts_fragment">
.
.-->other stuff
.
Here is the code to add fragment:
public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) {
View view=inflater.inflate(fragmentLayout,container,false);
setGraphics(view); //custom function which gets views and sets gui.
return view;
}
the view gets the ID of "fragmentLayout", but fragment gets a different ID, the container one, and this makes my fragments put themselves one over the other without distinguish id.
How to avoid this? thanks.
EDIT
I tried using something like: topFragment.getId(), but it returns the containerID. The javadoc says:
Return the identifier this fragment is known by. This is either the android:id value supplied in a layout or the container view ID supplied when adding the fragment.
I just don't want the container view ID, is there a way to force the fragment to have android:id value?
UPDATE
Trying to print the different ids:
My_Android: Main layout has id: 2131492958 //R.id.MainLayout ->ContainerID
My_Android: MyTopFragment{5cd93c9} - id: 2130968617 //R.id.ts_fragment value
My_Android: MyBottomFragment{2055ace} - id: 2130968601 //R.id.bs_fragment value
My_Android: MyTopFragment has ID: 2131492958 //After inflating process, the fragment gets this id, the same of R.id.MainLayout
My_Android: MyBottomFragment has ID: 2131492958 //Idem
Upvotes: 1
Views: 332
Reputation: 631
SOLVED
The key here was a bug in my gui setting. the top fragment was set to MATCH PARENT in WIDTH and HEIGHT! so no chance for the second one to get shown.
That said, setting WRAP_CONTENT, at least for HEIGHT parameter in LayoutParams for the First Fragment, makes things work.
No fragment Id was needed, the important is knowing the FRAGMENT VIEW'S ID.
Upvotes: 1
Reputation: 2285
Use fragment.setTag() Fragment by default has the id of the container. Android:id is the id of the view with in the fragment. Tag and fragment id are not related
Upvotes: 0