Reputation: 17743
I have looked around the internet and I have found only examples of Android fragmens, where they are placed on place (like two of them, one as a list, second is diplaying details of a selected item from list). My goal is to have classic android activity and I want to be able to add to it dynamically pre-prepared fragments with particular GUI. I want to add there dome group of EditText elements (editTextFragment), or several radiobuttons (radionbuttonFragment). Is there any example of how to do that? Making stable fragments is not usable for me.
Thanks
Upvotes: 1
Views: 6564
Reputation: 15515
This answer is for those who all searching the answer for same question. The answer is achieved by FragmentManager and FragmentTransaction with the following syntax.
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
then you need to add your fragment to this fragmentTransaction with the following syntax.
fragmentTransaction.replace(R.id.detailFragment, layout1);
And finally you MUST commit your transaction. Otherwise changes will not persist.
fragmentTransaction.commit();
Upvotes: 2
Reputation: 1
You can add and remove fragments to a FrameLayout in an activity's UI programmatically but your activity must extend FragmentActivty. Once you have created fragment classes and corresponding layouts, instantiated fragments can be added and removed via the FragmentManger and FragmentTransaction. See the section "Performing Fragment Transactions" in the Fragment documentation.
Upvotes: 0
Reputation: 28152
Fragment and views are very similar so think similar... Also look at google api demoes for fragments. If I remember correctly they use a framelayout and adds fragments to it.
Upvotes: 0