I am wondering is there any reason to do one over the other. I have been adding fragments via xml and using show/hide. I wanted to learn about replace/add/remove so I tried to add them programmatically. This has led to problems fragment remove issue and just seems less straight forward to me.
It also seems easy to place them via xml, whilst I'm still not sure how to programatically (as you don't have a reference to the fragment in the xml of the view that youa re adding it to?). Is there any reason for me to add fragments programmatically?
Fragments added in XML cannot be replaced or removed, other than that major limitation choose whichever method is easiest for you.
Related
I had this question earlier today, and have been stuck on this for 3 hours!
The answer suggested that it is okay to remove the activity_content as long as I remove the line:
<include layout="#layout/content_menu" />
I did that...And started to edit directly in the layout, after copying the default contents of content, except...the layout isn't even there when editing! So, am I supposed to keep both layouts, and just edit the content one? Why do I even need to do that? Is there a reason for this update in android studio?
I'm just creating my activities like this:
yes you can just leave the activity_yourclassname.xml as it is and make changes to content_yourclassname.xml only for the main content.
and yes this update is made just to make code more cleaner and more reusable. so that you could focus only on the main internal content of the code without getting into complex layouts.
I am working on an application that involves me having to place a toolbar at the bottom of each activity. The toolbar will have 4 buttons on it, each starting new activities.
I am just wondering, and I apologize for how general the question, is there a proper way to go about implementing this? my plan was to but a linear layout element in each xml layout file with horizontal orientation. Then four button elements inside of that...
It seems like a huge amount of work considering I will have a lot of activities..
What you want sounds a lot like Android's ActionBar. Check this link for a guide to its usage. If you use this, your app will look and feel more consistent with the operating system, as so many apps now use the ActionBar (which is a lot of them), and you gain a lot of power for what you want to show there, and how you want to do it.
ActionBar was only introduced in Android version 3 (Honeycomb), but there's a compatibility library, ActionBarSherlock which allows you to use it in older versions of Android as well.
Hope it works for you!
You can use a same xml for various views. So I suggest creating a generic xml, then using Inflaters and other resources as strings, xmls etc. to prepare the generic xml whenever any Activity is loaded. So you can reuse it the xml.
Is it possible? supported?
I want to make an app that hosts widgets in fragments, I know how to do it on activity but don't know if it works with fragments.
Im sure you mean AppWidgets, so take a look at this
https://github.com/DagW/FragmentHomescreen
It contains a complete example project for adding widgets to fragments in a viewpager.
It also contains code for adding appwidgets programatically.
Is is possible to use FragmentTransaction and the remove() method to get rid of fragments that are defined in the layout.xml (using the fragment tag) ?
I did not get this to work using the support libraries v4. The fragment stays in place after you commit the FragmentTransaction, after calling remove(). Can anyone tell me if this is by design, a bug or a feature?
It is possible to replace a fragment that is defined in the lyaout.xml, so I find it a bit strange that it should not be possible to remove it?
The native APIs available starting in Honeycomb work the same as those in the support libarary, so you cannot remove an instance of a Fragment which has been declared in your layout XML file.
With FragmentTransactions you manipulate ViewGroups such as LinearLayouts that act as containers to hold the layout of other Fragments. However, when you declare a Fragment in your layout, it doesn't have a container in the same sense because it is permanently part of the View hierarchy, so you can't remove it. That is by design, to support things like navigation Fragments that you'd never remove anyways. :)
One thing that's interesting, and I found it out totally by accident, is that you can add new Fragments into a Fragment that was declared with the tag in your layout; and it acts as a container for other Fragments
Like #david-c-sainte-claire and #martÃn-marconcini said, you can't use remove() method and FragmentTransaction to remove the fragment that was defined in the XML. That doesn't mean you are out of luck. You can always use setVisibility() method.
findViewById(R.id.fragment_main).setVisibility(View.GONE);
I did not this to work using the support libraries v4. The fragment
stays in place after you commit the FragmentTransaction, after calling
remove(). Can anyone tell me if this is by design, a bug or a feature?
This is by design (or a lack of a feature, not definitely a feature if you ask me :P). So as long as you are using the support libraries, you can't achieve this.
I'm creating an android app that will have multiple pages with the same layout, but the only thing that changes will be a string that is displayed on the top (using setText). Can I use a different xml file in the same activity class.., or does Android not allow that?
No problem with that. You can use in the same activity as many XML layouts as you want. Simply switch between them using setContentView()