When I add a fragment to my xml file, it shows all the content but not the fragment's action bar. How can I change the default behaviour to show the actionbar too?
From the Fragment, in your onCreate() method, you can try calling
getActivity().getActionBar().show();
you can set the title by using
getActivity().getActionBar().setTitle("title");
Related
I have an Android App which consists of several Fragments.
Each time the fragment is shown I set the ActionBar title to this fragment.
I did this with
getActivity().setTitle("abc");
Later in the App I needed to work a bit more with the ActionBar of a Fragment. So I had to change the title like this:
ActionBar actionBar = ((AppCompatActivity) getActivity()).getSupportActionBar();
if (actionBar != null)
actionBar.setTitle("xyz");
This works perfectly fine and changes the title to "xyz".
Still when changing to another Fragment that uses setTitle("abc") from the Activity itself, the title still stays at "xyz". Once I've done this for the first time, I can only change the title with the getSupportActionBar() object.
My question is: is this normal? Does this call transform / invalidate the normal Activity-Title somehow?
If you check the Activity Source Code, setTitle() method sets the text to actionBar itself.
Consider there are two fragments, A and B.
Fragment A will be shown by default in the Activity.
So later when you update the title from fragment B, you use getSupportActionBar() or getActionBar() method and update the title.
Since this is the recent change that is occured, even if you go back to the previous fragment,that is fragment A, the title will be same, as set in the fragment B.
If you want to change the title again, do it in onResume() method of the fragment A.
I have couple of fragments with NavigationDrawer. One of the fragment's actionbar has Spinner and others have just the title.
Now when I open the fragment which has spinner in actionbar and then click on the navigation drawer to open the another fragment class. The actionbar has the spinner and then title.
How do I clear the actionbar just to show the title and not the spinner? I mean clear the actionbar and show what is required.
Let me know!
When you switch to Second fragment(which has only title to display), in it's onStart() method, you can disable/hide the Spinner, using below code:
ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
You then need to add code in your first fragment to show Spinner again!
I think invoking invalidateOptionsMenu() should work. You should invoke this method when opening your second fragment.
I have a Activity and it has include more than one fragment.
I know it can use requestWindowFeature(Window.FEATURE_NO_TITLE); to hide the title in Activity.
But I don't want to hide title for all fragment.
Can I hide the title for preference fragment ?
Based on what you say, you will have to use more than one activity. For the fragments that are in a same activity, the title bar is either shown or hidden.
Do not use requestWindowFeature(Window.FEATURE_NO_TITLE); We can get actionBar and hide it after setContentView
When active fragment, show action bar
When fragment detach , notify activity and activity can hide the action bar
Above is my current implement and seems work. Still testing :-D
Simple sample here - https://bitbucket.org/jimmy64/fragmentsample
Is there any way to add progress dialog in to the action bar sherlock from sherlock fragment (not fragment activity). I have to use sherlock fragments because in my application I use navigation drawer.
For the fragment activity, progress dialog works fine when I use following code.
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.search_all_fragment_layout);
setSupportProgressBarIndeterminateVisibility(true);
Is there any way to do this in Fragments ?
Thanks.
The ProgressBar can be shown by using this lines in your Activity:
supportRequestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setSupportProgressBarIndeterminate(true);
setSupportProgressBarIndeterminateVisibility(true);
If you wanna use it in your Fragment you have to refer to the Activity:
getActivity().supportRequestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
getActivity().setSupportProgressBarIndeterminate(true);
getActivity().setSupportProgressBarIndeterminateVisibility(true);
Consider making a method in your Activity which you call from the Fragment.
EDIT:
Have a method in your Activity like this:
public void activateProgressBar(boolean activite){
setSupportProgressBarIndeterminateVisibility(activate);
}
Make sure you called this in your onCreate() of the Activity:
supportRequestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setSupportProgressBarIndeterminate(true);
Now just call this in your Fragment:
((YourActivity)getActivity()).activateProgressBar(true / false);
I have this activity made with actionBarSherlock. Is it possible to hide title of activity (not only text but the title bar) I tried:
getSupportActionBar().hide();
but it hides tabs too.
Picture:
https://fbcdn-sphotos-b-a.akamaihd.net/hphotos-ak-prn1/q71/1017267_10200157522600680_915484448_n.jpg
1.You must call setNavigationMode(NAVIGATION_MODE_TABS) to make the tabs visible
getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
2.You must disable the activity title by calling setDisplayShowTitleEnabled(false)
getSupportActionBar().setDisplayShowTitleEnabled(false);
3.You must disable application home affordance by calling setDisplayShowHomeEnabled(false)
getSupportActionBar().setDisplayShowHomeEnabled(false);
This way should able to able to get a look like this in your activity.
try one of these possibly?
add this to the manifest file under application:
android:theme="#android:style/Theme.NoTitleBar"
or in your activity:
ActionBar actionBar = getActionBar();
actionBar.hide();
getActionBar().setDisplayOptions(Window.FEATURE_NO_TITLE);
It worked for me hope it will help you out.
And this will give result like this