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.
Related
I have 20 fragments in my android application.
There are two fragments which have CollaspingToolbarLayout - i want this fragment's statusbar color as like their CollaspingToolbar color which is primarycolor
Other eighteen fragments have Toolbar - i want this fragment's statusbar color as like their toolbar color which is white
I can do it by adding this in each fragments but I have to add this in all the fragments.
GetActivity().getWindow().setStatusBarColor(//color here//);
Is there any other way to do so?
Thank you in advance.
Your case is very specific.
You can set StatusBar color back to white when closing your fragments which have CollapsingToolbarLayout. In this way, you do not need to touch other 18 fragments. You only touch only those 2 fragments.
Note: I really recommend to have a parent activity or fragment which have dedicated methods for such actions. It makes your code clean and more readable.
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!
I want to implement a toolbar for navigation which appears at the bottom of almost all activities in my app. It will have a fixed amout of elements (four).
Screenshot:
(certain activities will still have their own toolbar at the top):
In order to make it somewhat reusable, I've created a separate layout file for the toolbar which allows me to include it on multiple activities with:
<include android:id="#+id/toolbar_main" layout="#layout/toolbar_main" />
Every item of the toolbar leads to another activity, which means it acts as a navigation throughout the whole application.
However, as I want to use it on multiple activities, I'm not sure on what would be the correct place for the Java-Code behind the onClick-Events of the menu items. I've seen approaches using a base-class which can be extended by all activities using this navigation toolbar. I've also thought about not using a toolbar at all and creating a fragment for the navigation. I haven't used fragments yet, however, they seem to have their own code-behind class in addition to their design .xml, which would be suitable for the click-Events. Any suggestions? I might add, that I want to center and stretch the navigation bar later on, which seems to be not so easy using a Toolbar with associated menu.
Make A Base activity, which will be extending by all your other activities. Write all your logic related to that toolbar on Base Activity. Thats all.
You could use the TabLayout with ViewPager. Place them in main Activity, and use Fragment for every root screen.
I am working on widget, where for widget settings i am looking to provide a dialog for with tab fragment, the problem is dialog do not have action bar tabs, i have tried various lay out patterns, but none of them seems to work.
In the manifest, have made the activity
<activity android:theme="#android:style/Theme.Dialog" android:launchMode="singleInstance"
android:name="WidgetConfigureActivity"></activity>
I am not sure, which layout to use exactly ViewPager, FragmentTabHost in the UI, basically not clear which layout to go for.
WidgetConfigureActivity extends FragmentActivity for now, the below is the code for it(code is taken from FragmentTabHost)
FragmentTabHost mTabHost;
#Override
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);
setContentView(R.layout.widget_configure_activity);
mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.tabhost);
//this above setup line gives error => (The method setup(Context, FragmentManager, int) in the type FragmentTabHost is not applicable for the arguments (WidgetConfigureActivity, FragmentManager, int))
mTabHost.addTab(mTabHost.newTabSpec("simple").setIndicator("Simple"),
MyFragment.class, null);
}
ViewPager is not that important, but how to have tabbed dialog with fragments is the question?
4. How to make UI/layout NOT change even if apply Theme.Dialog to my activity, all the font appear white in white background? (i have seen text by tilting the screen)
I am not sure, which layout to use exactly ViewPager, FragmentTabHost
in the UI, basically not clear which layout to go for.
It depends on you needing swipe tabs(so the user can swipe not only click through the tabs) or not. If you do need swipeable tabs(and I recommend that you implement it as it's a nice option for the user) you could use a ViewPager along a TabHost(instead of using a FragmentTabHost). There are a lot of sample out there on how to do this, I've made one myself that you can find here.
How to make UI/layout NOT change even if apply Theme.Dialog to my
activity, all the font appear white in white background? (i have seen
text by tilting the screen)
You'll need to make your own theme, extending from Theme.Dialog and "fix" the properties you want. Alejandro Colorado has pointed out the solution.
A possible answer to your 4th point is adding this to your style (which is based on the standard Theme.Dialog):
<item name="android:textColor">?android:attr/textColorPrimaryInverseDisableOnly</item>
Likewise, try this example of FragmentTabHost, although the tabs are at the bottom.
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.