Fragment Activity leaves blank space in place of ActionBar in Overlay mode - android

I have a FragmentActivity which contains two fragments. The main fragment shows users some reading material therefore occupies complete space of Activity. The other fragment contains some menu items and is right draggable. I want my ActionBar to be Overlayed above the contents of both fragments. I used
getWindow().requestFeature(Window.FEATURE_ACTION_BAR_OVERLAY)
before setContentView() in onCreate() method of my Activity.
It did overlayed the ActionBar but when actionBar auto-hides it lefts blanks space instead of contents to be displayed on full screen.
I GOOGLED and STACK OVER FLOWED and somebody here suggested to use this line in the style.xml in activity theme instead of one above I used.
<item name="android:windowActionBarOverlay">true</item>
But unfortunately It didn't helped either.
I am stuck with it any help will be highly appreciated!

Related

Android: How to remove title padding when using sliding tabs toolbar

I am following this tutorial. The tutorial basically helps to create a sliding tab menu by using the AppCompat library. It basically creates a custom toolbar using the tabs and then replaces the standard ActionBar with this toolbar.
It works perfectly, but I do not wish to show the title. I only want the tabs at the top of the screen to be my main navigation. On my activity I have set
getSupportActionBar().setTitle(null);
but this only makes the text for the title blank. Space (padding?) is still reserved for the title. How do I remove it?
The picture below shows the problem visually.

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 hide the animation of actionbar show and hide in android?

I have an activity contain an tabhost. And I use several fragments for the tabhost, each tab only has one fragment, that means the fragment size is matching the parent. However , not all fragment has actionbar.
For example, the first one hide actionbar while the other show the actionbar.
The problem is , when I show / hide the actionbar, it does not transacts instantly, but it has an amination that e.g. for the case hide->show , the actionbar is hide and leave a blank space on the screen , then the content screen will expand to fit the screen . It cause some "glitch" in the ui , so are there any way to fix it?
I tried requestWindowFeature(com.actionbarsherlock.view.Window.FEATURE_ACTION_BAR_OVERL‌​AY);
Which the animation is hide but the actionbar is overlay the content instead of a part of the content,
Update:
The overlay of actionbar is like this screenshot , it is on top of the content. please have a look
Please see a work around here for requestWindowFeature(com.actionbarsherlock.view.Window.FEATURE_ACTION_BAR_OVERL‌​AY);
Using ActionBarSherlock; experiencing View.getLocationOnScreen() inconsistency between 2.2 and ICS/JB
Also for the hide and show method can you please confirm if you are calling it within onCreate method of your fragment?
Could you post the code where you are making the call?

Split actionbar for a specific fragment

I have an activity with three fragments. Fragment A has 1 item in actionbar. Fragment B has 4 items in actionbar, and Fragment C has 1. So, I want to have split actionbar when I change to fragment B, but I don't want split action bar in A-C fragments, because there is no reason to fill de bottom of the screen with a bar only for one item.
Can I change actionbar mode when I change between fragments?
Not sure if this can be done using Fragments as ActionBar is associated with the Activity. However, I tried what you are looking for with two activities and it worked.
Add the action bar in onCreateOptionsMenu in both the activities and then add
getWindow().setUiOptions(ActivityInfo.UIOPTION_SPLIT_ACTION_BAR_WHEN_NARROW);
right before onCreate method of the activity in which you want the split action bar to appear. Thats it. Simple isn't it. :)
You can also hide and show it using getActionBar.hide() and getActionBar.show() methods respectively.
(The only thing I missed is checking this with support libraries. Will try and update you on that as well)
Let me know if this helps you in some way. Happy coding :)

Briefly hiding ActionBar without resizing Activity

I'm using a ViewPager to scroll between different fragments. There are two types of fragments, using two different menu resources. I'm invalidating the menu to switch between those resources when necessary. That's all working pretty well, but the menu is "redrawn" without an animation.
To prevent having to mess with individual MenuItems I was hoping I could briefly hide the ActionBar while the new menu is loaded, showing it when that's done. That's working as expected as well, but the activity is resized when the ActionBar is toggled.
Is there any way to prevent this from happening, or otherwise hide the ugly transition between menu resources?
I didn't quite catch the menu part of your problem, but there is an easy solution to preventing your activity from resizing when the ActionBar appears or disappears.
You can tell the ActionBar to draw itself in overlay mode, meaning it will float on top of the activity, in stead of actually being part of the activity's layout. Use either android:windowActionBarOverlay in your theme, or the Window.FEATURE_ACTION_BAR_OVERLAY flag from code.
You probably want to use this feature in conjunction with the actionBarSize constant, that specifies the correct offset for the first view in your layout. This way, your content still appears below the ActionBar, but since the ActionBar itself is an overlay, upon hiding/showing it, the activity will not resize.
<SomeView
...
android:layout_marginTop="?android:attr/actionBarSize" />
More details can be found in the documentation.

Categories

Resources