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.
Related
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.
I've set up a ViewPager in my App with tabs, but I need to position the tabs underneath a button so the layout would be
TitleBar
Button to search activities on a day
Tabs of each day there are activities
Is that kind of layout possible to do using Tabs or will I need a different approach to solve this?
The way the activity is running is theres a main activity, and the layout is just the button and a FrameLayout, and the the list (where the ViewPager is) is set up in a fragment and the fragment is loaded into the FrameLayout. I don't know if this is the best method for this so if not please add your recommendations of a better method I could use.
EDIT: If I could get it so all the tabs were at the bottom of the screen this would also be fine.
Is that kind of layout possible to do using Tabs
Not with action bar tabs. Not only can you not control where the tabs go, you cannot even control if there will be tabs versus a drop-down list.
will I need a different approach to solve this?
You are welcome to use some other tab solution (ViewPager with a tabbed indicator, FragmentTabHost, etc.) where you have more control.
Note that your proposed design does not adhere to Android design aesthetics. Most apps would not have "Button to search activities on a day", but instead a search action bar item, or perhaps a SearchView in the action bar.
If I could get it so all the tabs were at the bottom of the screen this would also be fine.
This is completely against the Android design guidelines. Don't use bottom tab bars on Android.
When I click one of tabs, I just want to show a dialogue on top of current view without switching the tab.
I could achieve this behavior by overriding onTabChanged when I used TabHost and TabSpec.
But now I switched to using actionbar's tab, and having hard time finding the solution.
i have an action bar with a Tab navigation. While running on 3.1 everything was fine, but once i installed the app on a 4.1 device the tabs where forced to be displayed as a spinner, wrecking my layout design.
Looks like the tabs are taking too much space (if i remove some of the fragments everyting looks ok again.
Is there a way to stop this behaviour and display the navigation as tabs again just as in android 3.1? Does anyone know?
thanks!
I found the answer here. Had to set the navigation mode AFTER adding the tabs.
First of, just to clarify: This is not a bug and it works as intended. Reference this discussion on the Google code forums.
However, I came to the solution that if you override:
<bool name="abs__action_bar_embed_tabs">false</bool> //for ActionBarSherlock
<bool name="android:action_bar_embed_tabs">false</bool> //for default ActionBar
You won't have a NAVIGATION_MODE_LIST in portrait mode. However you won't have embedded tabs and if you rotate your screen to landscape it won't work either.
By default you'll have embedded tabs and therefore a NAVIGATION_MODE_LIST on a screen with a width of >480dp.
This behavior occurs (I assume) because the embedded tabs are limited to the width of the ActionBar, so if you override the boolean value it'll have tabs in a separate row and it won't collapse. But unfortunately I can't explain myself why this does not work in landscape.
Sorry, but you can't stop this. This is a feature, not a bug, according to Google. See: http://code.google.com/p/android/issues/detail?id=24439
As I understand you should consider using a ViewPager + PagerTitleStrip if you have many tabs and want to make them scrollable all time.
Here is a quote from ttps://code.google.com/p/android/issues/detail?id=24439#c9:
If your UI contains many tabs to the point where you hit the scrolling
tabs or collapse-to-spinner case regularly, you might consider using a
PagerTitleStrip as an indicator rather than full tabs to present this
info in a less cluttered way. This can be especially useful if the set
of tabs displayed is under user control. Clickable tabs work best when
there is a small, bounded set such as in the YouTube app. Scrolling
tab strips lose one-touch access to any tab, their primary advantage
over a simple title strip. Examples of the PagerTitleStrip style can
be found in the Android Market and Google+ apps.
I would not recommend using tricks as nobody guarantee that tricks will works stable.
If you really want to still use tabs and stop it from collapsing to a spinner, it is technically possible using reflection IF you happen to be setting a customView for the tabs:
View v = <your custom tab view>;
Tab tab = actionBar.newTab().setCustomView(v);
do{
v = (View)v.getParent();
} while (v!=null && !v.getClass().getSimpleName().equalsIgnoreCase("ScrollingTabContainerView"));
if(v!=null) {
try {
Method allowCollapse = v.getClass().getDeclaredMethod("setAllowCollapse", new Class[] { Boolean.TYPE });
allowCollapse.setAccessible(true);
allowCollapse.invoke(v, new Object[]{ false });
} catch (Exception e) {
}
}
If there is not enough space, the tabs will scroll in the ActionBar. The Tabs and any Menu actionItems are given 1/2 the screen width so you can calculate if the tabs will end up scrolling or not and make adjustments to tabs, menu actionItem labels etc or force the tabs into a stacked mode to avoid the scrolling tabs... OR as previously suggested, using the PagerTitleStrip instead.
Is it possible to use ActionBar without activity? I want to use it in the StatusBar view, instead of view highlighted with purple color.
This screen shot, based on an image from the Android ui pages, labels the different components, just to make sure we're talking about the same thing.
You can't add an ActionBar outside of an Activity. You need an Activity to get an ActionBar reference. ActionBar references are retrieved via the Activity API, by calling the getActionBar() method inside the Activity.
So you can't use an ActionBar to replace the StatusBar. You can change the StatusBar's color, and possibly layout, but only on a rooted phone. See this forum discussion for some perspective on the issue.
AFAIK, there is no API for changing the Notification Drawer's layout and functionality. There are no API methods that allows us to place an ActionBar inside the drawer outside of the notifications e.g. at the top of the drawer like in your screenshot.
You can create custom layouts for notifications(the messages inside the drawer). The Android Developer pages have an example on how to do it (see "Creating a Custom Notification Layout" at the bottom of the page). Again, you can't use an ActionBar but you can add Views to a Notification layout.
Use notifications. You will see something in the status bar and no Activity will be involved.
There's GOT to be a way to change the ActionBar from a class declared within an Activity:
(1) create method in activity to .setTitle
(2) pass activity context to Controller class (MVC) declared in Activity
(3) have Controller class call activity method to modify actionBar
...what do you think?
You should make a custom rom to change that view.
That Purple View is not part of your application but a part of Android.
If you do not want an Activity, why not make it transparent.
How do I create a transparent Activity on Android?