I've got a problem. I wanted to create something that looks like this:
action bar
Left icon would be a back button (I mean it would return to previous tab, the left one), right icon would be next button (it would go to next tab, the right one). I wanted also to make it repeatedly so when I'm on tab A (there are for example 3 tabs: A, B, C) and when I use right button, go to B, then C, and then again A and so on. The text in the middle would be a name of a tab. I would also like to make it compatible with earlier versions of android (like 2.something).
To achieve this, you need to implement custom action bar.
And to target devices with lower android versions use support library. Action bar is supported only in android versions 2.1 and above, with the help of the library.
Refer below link for setting up action bar.
https://developer.android.com/training/basics/actionbar/setting-up.html
The below piece of code will help you to get more idea on this.
// Inflate your custom layout
final ViewGroup actionBarLayout = (ViewGroup) getLayoutInflater().inflate(
R.layout.action_bar,
null);
// Set up your ActionBar
final ActionBar actionBar = getActionBar();
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setCustomView(actionBarLayout);
To create above action bar, use relative layout with two image views for direction buttons, and one textview for tabname. For a better design, avoid using tabs in this case. Use Fragments. On each click of the images, just inflate the fragments, each time.
if (openTabIndex=totalNumofTabs-1) {
//go to Tab index 0
} else {
//go to Tab index openTabIndex+1
}
I never worked with tabs before, but this is one if statement that if absent, you cannot do what you want.
Related
I have an application that has action bar sherlock and SlidingMenu integrated in it, the application has only 3 pages, and they're static pages, so no big deal about them. I have one activity and 3 fragments that i am changing when using the sliding menu.
What i want to achieve is something like this image
I want the user to be able to swipe with his finger on the ActionBar, and when that happens i change the fragment that is being displayed.
Note: i got the gesture recogniser from this awesome answer, but i can't seem to find a way to attach it to the ActionBar.
If i could put a View on the action bar and detect swipe on it, that would be great, but how can i do that ?!?
One last thing, how to implement this "Page Control" in android ?!?
So the answer to my question is that i simply created a custom_actionbar.xml, and then i used this to do the following:
View custom = LayoutInflater.from(this).inflate(R.layout.actionbar_custom, null);
getActionBar().setCustomView(custom);
// and you can use the things in the custom action bar like following !!
Button btnLeft = (Button) custom.findViewById(R.id.btnMenu);
Hope it helps someone :)
For my application , I'm planning to have a design as this:
http://img811.imageshack.us/img811/7045/problemel.png
-Design needs to have a title bar which is indeed the action bar in android. To overcome the compatibility issues, I used the sherlock action bar which is said to support older versions that dont have action bars. I havent tested yet for the older devices however.
-As far as I know, for navigation , we could rather use tabbed or dropdown list methods. I want to have constant tabs for every page in my app to stand below the page. This reflects the tabbed action bar, however I want it below not just under the action bar. I don't know how but youtube application somehow has it in the middle of the screen (if it's not a custom view). So how do we have tabs positioned in the bottom of the page?
-Also I want to have another menu, whose items depend on the page it's on. But the apperance will be the same for every page. In the picture on the left this menu has buttons as "Bt 1" ,"Bt 2". I dont want to repeat the same xml for every activity page, but I'm not sure how to achieve it without repeating. If the action bar allowed using both navigation tabs and the drop down list, I could put the items of this menu in the dropdown list just as the picture on the right (onto the gray button). But I guess it does not?!
Therefore I have to repeat the menu xml code in every page or is there another smart way?
Any advice will be appreciated.
This can be achieved not with tabs but by adding items to a linear_layout with a gravity of bottom but it is a bad practice as #D_Steve595 stated and should be avoided in android designs.
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowHomeEnabled(false);
According to Jake as in Android layout with action bar and tabs, we decide to follow his 2nd suggestion.
Use tab navigation in the action bar and set a custom view with a Spinner
Together with IcsSpinner from ActionBarSherlock itself, we manage to make it work in Gingerbread and ICS both.
However, we still suffer the following problem, during landscape mode.
Note, the spinner ("Portfolio 1") is in between PORTFOLIO tab and search icon. What we want is, the spinner is at the left side of WATCHLIST tab.
Is there anything we may try out?
Thanks.
You can set custom view for your action bar, user RelativeLayout to put your spinner at left side of "WATCH LiST"
in java code: actionBar.setCustomView(R.layout.custom_actionbar_layout)
a very late answer, but this is possible. You just have to replace (programatically) the view that is pointed by android.R.id.action_bar_title with a Spinner. I wrote a blog about it. Here's the link
I've just used ActionBar Sherlock to implement the android action bar on pre 3.0 android devices. I'm having one issue when I'm using tab navigation though.
Currently the action bar is devided in two rows at the top of the screen. The first row contains the app icon and app title, while the second row contains my navigation tabs. I tried to remove the top bar with actionBar.setDisplayOptions(0);, but now i still have two rows, but the top row is empty. How can I remove this top row?
Thanks!
I recognize this was posted about a month ago, but I think I can lend a hand. Try running these three methods just after constructing your action bar:
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
bar.setDisplayShowTitleEnabled(false);
bar.setDisplayShowHomeEnabled(false);
The first just sets the AB to tabs, which I assume you've already done. The second disables the String title up top (I should note that it appears when the app first launches, but then disappears very quickly), and the third shuts off the icon with the same functionality as the previous method. I actually had the same thing you're dealing with occur in my app, but once I ran both of those methods, the extra, and blank, top-most bar disappeared.
In addition to the accepted answer, Just remove onCreateOptionsMenu and onOptionsItemSelected to get clean tab view without having any empty action bar.
I want to create tabbed ActionBar (NAVIGATION_MODE_TABS) with custom view (DISPLAY_SHOW_CUSTOM). It should looks like this:
I just need to be able to navigate through the activities via buttons in the top row - as I would do with ActionBar in normal - non tabbed mode, and also be able to switch tabs in second row. If I set ActionBar to be in a tabbed mode, the custom view disappears, it seems that tabbed mode and custom view are mutually exclusive... Is it even possible?
There are more possible ways how to do that without ActionBar. Normally I would create a layout with buttons, include it in the TabActivity (which is now deprecated) and that's all. But I do not feel it would be the best solution, so any help or advice is appreciated.
Thanks in advance.
EDIT: After a little research I found that it works, it is just not gets displayed on my 4 inch screen. However it still does not work as I expected. On wider screens a custom view shows on the right side of the tabs - it is ok, but on the 4 inch phone screens it is not displayed at all.
I think the best solution is to use ActionBarSherlock library (I do not like using deprecated features and my app must work also on pre - sdk11 version devices) and fragments - one for top ActionBar and the second for tabbed view.
The buttons in the top row have to be added as optionMenu items:
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
for (int i = 0; i < menuItems; i++)
{
if (i == 0)
{
menu.add(R.string.home).setIcon(R.drawable.button_home).setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
menu.getItem(0).setOnMenuItemClickListener(this);
}
}
return super.onCreateOptionsMenu(menu);
}
The philosophy of the ActionBar is slightly different then I supposed to be when asking. From the Android developer guide:
When you want to provide navigation tabs in an activity, using the action bar's tabs is a great option (instead of using TabWidget), because the system adapts the action bar tabs for different screen sizes—placing them in the main action bar when the screen is sufficiently wide, or in a separate bar (known as the "stacked action bar") when the screen is too narrow.