Carl
Carl

Reputation: 249

In android, how do I dynamically swap a fragment by button click or other event?

I'm stuck at a sore point: fragments.

Here's a reference of the layout

I'm trying to swap out fragment C with fragment D upon button click. Fragment A and B needs to stay without refreshing.

It looks like I stopped at having it load a fragment without throwing away the old fragment, leading to fragment D appearing above fragment C, with C still visible.

How can I do this?

Upvotes: 2

Views: 6163

Answers (2)

GrouchyPanda
GrouchyPanda

Reputation: 1084

There is a call within FragmentTransaction to replace the fragment in the container. You just need to have a designated container hold the place where c and d will go, and then upon button click do something like: getSupportFragmentManager().beginTransaction().replace(R.id.container, fragment, "FragD");

Upvotes: 3

mohammed momn
mohammed momn

Reputation: 3210

Android Fragment API let you to make some transactions in it like add new one , delete and replace one with another one that's will help you to build flexible UI design based on your device size ,

and here some guide for you :

http://developer.android.com/training/basics/fragments/fragment-ui.html

http://www.cs.dartmouth.edu/~campbell/cs65/lecture09/lecture09.html

http://android-er.blogspot.com/2013/04/replace-fragment.html

and if you facing any problem in code tell me to discuss in it

Upvotes: 1

Related Questions