Android: Multiple Fragments from same XML file - android

I have a view pager that has a page for each subject in an array (English, Maths, Science, etc). The fragments in the view pager are all created from the same XML file. These fragments then have fragments added inside of them.
As all the parents inflate from the same XML file, I can not specify which fragment I want to add the child fragment to. I was wondering if their was a way to do this - Narrow the scope of the id maybe??

I think you want to implement multiple fragments with the capability to manipulate them depending on user selections. There is Google link Fragments
Review these code on that web page:
The many samples of Fragment and FragmentTransaction
Look at method showDetails in class TitlesFragment, for example.
If you want a working sample, look at Working with Android Fragments. Snippet from the webpage:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent">
<fragment
android:id="#+id/headFrag"
android:name="com.intertech.HeaderFrag"
android:layout_width="wrap_content"
android:layout_height="match_parent"/>
<fragment
android:id="#+id/bodyFrag"
android:name="com.intertech.BodyFrag"
android:layout_width="wrap_content"
android:layout_height="match_parent"/>
</LinearLayout>
And the code
...
FragmentManager fragmentManager = getFragmentManager ();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction ();
// work here to change Activity fragments (add, remove, etc.).
fragmentTransaction.add (R.id.myFrame, myFrag);
fragmentTransaction.commit ();
From above, notice there are 2 fragments in the layout. And fragmentTransaction.add method should specify parent container in R.id form.
Someday I may want/need to implement this. So far I implemented show/hide Layouts instead (not regretting it), and they can have IDs for me to manipulate.
Have fun and keep us posted, Tommy Kwee.

First. I suggest you to try to avoid child fragment, if you really doesn't have very good reason to use them.
But I don't understand where is problem. I don't see any problem if more fragment use the same XML for the inflate. You can call setArguments() on all parent fragment and put here some Bundle which can be used to recognize which fragment it is inside of it by getArguments().

Related

Kotlin hide replaced fragments don't work

I have a fragment with constraints that I want to preserve
<fragment
android:id="#+id/fr_test"
android:name="com.test.FragmentTest"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
I use transactions to hide/show it and all . works fine
fm.beginTransaction().hide(mainActivity.fr_test).commit()
fm.beginTransaction().show(mainActivity.fr_test).commit()
I use transactions to recreate fragment keeping the same container to use the same constraints
fm.beginTransaction().replace(R.id.mainActivity.fr_test, FragmentTest).commit()
But if now I try to hide/show the fragment with the same references as before it does nothing. I suppose that references are lost but I don't know how to find them. I tried to save the reference like this:
fragRef = FragmentTest()
viewsManager.fm.beginTransaction().replace(R.id.fr_test, fragRef).commit()
and then use:
fm.beginTransaction().hide(mainActivity.fragRef).commit()
fm.beginTransaction().show(mainActivity.fragRef).commit()
but still does the same.
What I'm doing wrong?
When you call replace, he calling remove(fragment) and after call add(int, fragment, string). I think that you should use add method instead replace and control your UI using hide and show

set android:name of <fragment> programmatically

Is it possible to set the name of added in XML layout from activity class, I need to make transition between fragments which are includes on the activity but only on and replace the fragment name by new one.
<fragment
android:name="com.example.harry.comparateur_de_prix.Accueil.AccueilFragment" //To change
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/fragment1">
</fragment>
Firstly you will need to change your Activity's XML layout to include a FrameLayout like this:
<!--Other XML layout elements, view-groups, views etc...; other stuff-->
<FrameLayout
android:id="#+id/id_1"
android:layout_width="#dimen/fl_width"
android:layout_height="#dimen/fl_height">
</FrameLayout>
What we want to do now is; to be able to replace that FrameLayout in the Activity's XML layout above that we just added with the layout of one of your Fragment's layout file.
Now in your Activity file,
instantiate a FragmentManger object
obtain a fragmentTransaction instance via the fragmentManagers beginTransaction() method.
/*1)*/ FragmentManager fragmentManager = getSupportFragmentManager();
/*2)*/ FragmentTransaction ft = fragmentManager.beginTransaction();
Then make these changes
ft.replace(R.id.id_1, new YourFragment());
ft.commit();
The argument YourFragment() will require you to insert an instance of one of your Fragments
, this will replace the FrameLayout's current layout with that of YourFragments(). We finally commit the fragmentTransaction.
If you want to replace the Fragment that the FrameLayout displays, simple begin a new fragment transaction and replace the frame layout with a new Fragment as shown above.
You want to use a FrameLayout instead here.
Then use FragmentTransaction to replace that ViewGroup by its ID attribute

Android - Activity inside linear layout

I am creating an app with a WebView and an invisible menu on top of the page (it only appears when double clicked on the screen). That menu is a simple linear layout with buttons inside. Each button will start a different activity and I would like to make it so the menu would work in all activities.
I was thinking of creating a main layout that would consist of (invisible) menu on top and on the rest of the screen it would have the space for other activities. I want each activity to have its own layout aswell.
Maybe I could create that layout with menu on top and the rest of the space would be linear layout. Then I would call each activity inside that linear layout.
Is that possible and if it is, how to do it?
Any help will be appreciated.
as i understand you can do it like that :
1- create a class which extneds LinearLayout for the menu.
2- create a class extends activity and inside of it create that menu view
3- all your other activities should extends the activity you created in step 2.
4- inside all other activities onStart you should add menu view to the screen
but i strongly suggest you to use navigation Drawer.
http://developer.android.com/design/patterns/navigation-drawer.html and
http://developer.android.com/training/implementing-navigation/nav-drawer.html
You can achieve this by creating one Activity which have your invisible layout at top and a FrameLayout as container for your Fragments :
MainActivit.java
public class MainActivity(){
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main)
}
}
and activity_main.xml
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/invisible_menu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" >
<!-- your other views here -->
</LinearLayout>
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/invisible_menu" />
</RelativeLayout>
That's the MainActivity which will hold all your Fragments. For using Fragments in your app you should check Android Developers - Fragments
Edit: Here is how you can add/replace Fragments via code:
To add your first Fragment simply call :
FragmentTransaction transaction = getFragmentManager().beginTransaction();
ExampleFragment fragment = new ExampleFragment();
transaction.add(R.id.fragment_container, fragment);
transaction.commit();
// Commit the transaction
transaction.commit();
And after that to replace the content with another Fragment , you should do something like this in your onClick :
// Create new fragment and transaction
Fragment newFragment = new ExampleFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
So basically you should use FragmentTransaction.add() and FragmentTransaction.replace() .
If you want to support older versions of Android (i.e. avoiding fragments), you can use an <include> to add the menu layout to each activity. You'll need to hook up the click events in the onCreate of each activity, though you can encapsulate this code in another class.

Android Fragment replace own root view

I have FrameLayout element in my MainActivity's layout, what is used as "connection point" for multiple type of Fragments.
View:
<FrameLayout
android:id="#+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Fragment replace:
Fragment fragment = new CoursesFragment();
getFragmentManager().beginTransaction().replace(R.id.main_content, fragment).commit();
Now I'm at the situation I need change main_content directly from CoursesFragment (fragment replaces own view). What is best approach to do this? Is it safe? I can reach MainActivity's context and change it, but I don't know if it's correct way.
I don't know what your View is, but the simplest approach is to provide both Views inside their own ViewGroup, while setting 'the other view' on setVisibility(View.GONE). If you need to switch, just set the first shown View to View.GONE, and the second to View.VISIBLE.
This way you don't have to hack around with the Activity's context, or Fragment Managers hide and show methods.

Duplicate ID with Fragment on PagerAdapter page

i´m facing a problem with the pageradapter and fragments.
i have a PagerAdapter and on every page i want to put a fragment. on the xml layout of the page, i use this code to add the fragment:
<fragment android:id="#+id/fragment1"
android:name="de.worldcup.android.ui.fragments.GroupTableFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
when i run the application, it crashes when it tries to instanciate the second page of the PagerAdapter (2nd time the instantiateItem method is called) at the line, when i try to inflate the xml layout of the page.
View v = inflater.inflate(R.layout.group_activity_item, null);
the error:
03-15 21:57:34.795: E/AndroidRuntime(6257): Caused by: java.lang.IllegalArgumentException: Binary XML file line #20: Duplicate id 0x7f050043, tag null, or parent id 0x0 with another fragment for de.worldcup.android.ui.fragments.GroupTableFragment
i looked up the id 0x7f050043 -> it is the #+id/fragment1 id.
any ideas how to fix this?
thanks :)
Facing the same problem myself I found this:
"Note: You cannot inflate a layout into a fragment when that layout includes a . Nested fragments are only supported when added to a fragment dynamically."
In the documentation. In short, create your fragment architecture dynamically to solve the problem.
I had the same problem. I don't think we can not have 2 fragments with the same id in a page.
What i do (but i think it's not the best solution, but it works):
Create one layout for each fragment with a different id.

Categories

Resources