Android actionBar.setTitle makes getActivity.setTitle() not working - android

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.

Related

Android create fragment onCreate

I am using navigation drawer with fragments.
Generally, I want the fragments to change the activity's toolbar title.
When starting the app, I supply a default fragment by using fragmentManager replace method.
But the fragment can't change the title of the activity's toolbar.
On the other hand, when changing fragments after App is started, it can change the activity's toolbar title
Can someone elaborate on this?
Thank you
Best regards Morten

Android ActionBar API, getSupportActionBar() and setDisplayHomeAsUpEnable meaning?

I added a icon on the action bar. And when i click on it, it opens another activity via intent. However, in some source, people add getSupportActionBar and setDisplayHomeAsUpEnable. Even if without those, it still works.
My question is what is the meaning og these 2 API?
If you have included actionBar or toolbar in your activity and if you want to go to previous activity on the tap of back arrow on the left side (in LTR configuration) of your actionbar, you will have to first get actionbar as,
ActionBar ab = getSupportActionBar();
and then provide action which is go to previous activity and for that you will have to call setDisplayHomeAsUpEnabled() method as,
if (ab != null) {
ab.setDisplayHomeAsUpEnabled(true);
}

Hiding toolbar title only for one fragment

In my app I want to remove toolbar title for one fragment only. When I use the following code
((AppCompatActivity)getActivity()).getSupportActionBar().setDisplayShowTitleEnabled(false);
it also removes title from all other fragments too. Is there any other way to remove title for only one fragment and keeping it in others. I also tried removing title attribute from navigation drawer implementation but then it starts showing app name in title. I don't want anything in this fragment . How this can be done?

Change actionbar to overlay from within Fragment

I have a fragment (Fragment 1) that is replaced by another fragment (Fragment 2). Fragment 1 is placed on the stack. I'm using compatibility mode (not ActionBarSherlock).
Here's my problem. I want the actionbar to be displayed as overlay in some fragments, but not in others. Specifically, when Fragment 2 is shown, I want it to appear in overlay and then go back to the normal actionbar once Fragment 2 exits.
Fragment 1 has an regular actionbar that is always visible. But, when I replace Fragment 1 with Fragment 2, I need to hide the actionbar after 5 seconds. If there is a touch event, the actionbar is shown again. This all works fine, but, Fragment 2 is redrawn each time the actionbar is hidden or revealed. Because of this, I want to make the actionbar in Fragment 2 show as an overlay.
I know I can change the actionbar overlay but I don't know how to do that programmatically from within the fragment. I DON'T want to change it for every fragment, just Fragment 2.
Ideas?????
This may not be the answer you were hoping for.
Consider a different question: Can we change activity theme after setContentView(...) has been called? The question has been asked many times, and a common solution is to recreate(calling finish() and startActivity(getIntent())) the activity and set the new theme before setContentView(...).
Your question is an extension to this - with added complexity of changing the theme from a fragment. In any case, I don't consider the solution mentioned above a good one.
ActionBar is one of the first components to be initialized when an Activity is created. I don't think you will find a way to somehow 'refresh' it with new attributes. See below how the requestFeature(Window.FEATURE_ACTION_BAR_OVERLAY) method deals with post-setContentView(...) calls:
#Override
public boolean requestFeature(int featureId) {
if (mContentParent != null) {
throw new AndroidRuntimeException("requestFeature() must be
called before adding content");
}
....
....
}
So, if setContentView(...) has already been called for the Activity (which it is, in your case), a runtime-exception will be thrown.
Is it possible that you don't even require this functionality?
Start by setting the ActionBar to be an overlay in your theme:
<item name="android:windowActionBarOverlay">true</item>
<!-- Support library attribute for compatibility -->
<item name="windowActionBarOverlay">true</item>
Here's my problem. I want the actionbar to be displayed as overlay in some fragments...
Okay. We have already provisioned for this above.
... but not in others.
Say you don't want to have the ActionBar as an overlay in Fragment B. Then, in Fragment B's layout, do the following:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="?android:attr/actionBarSize" > <<<-- ?attr/actionBarSize
for compatibility
....
....
</LinearLayout>
With the top-margin set to ActionBar's size, Fragment B looks as if it has a regular ActionBar - not an overlaid one. Another way to achieve this would be to have a View with android:layout_height="?android:attr/actionBarSize" placed as the first child in the layout above.
In essence:
your ActionBar will be an overlay.
in the fragments where the ActionBar will auto-hide, the fragments layout will not have any top-margin set.
in the fragments where the ActionBar should not be overlaid, the fragments layout will have top-margin set to actionBarSize.
A point of note (thanks to Jelle):
If your ActionBar is semi-transparent, it would be best to use padding instead of margin for a consistent look.

How to show the title in Fragment but hide in the Activity?

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

Categories

Resources