Android ActionbarSherlock Customization - android

I'm working on a application that has a different appearance depending on the result of an API call. This call returns a set of data, also containing information about the appearance of the app. Since I get this information on the run, I cant edit styles.xml to design the ActionBarSherlock I'm using.
What is was wondering, is there a way to customize the ActionBar solely from code? I mean more than the icon, title or background. But textcolors and menuitems and so on.

do something like the following where actionbar is a layout with a bunch of buttons that you can restyle programatically
//ActionBar Functions
protected void initActionBar()
{
// Inflate the custom view for action bar
View customNav = LayoutInflater.from(this).inflate(R.layout.actionbar, null);
// Attach to the action bar
getSupportActionBar().setCustomView(customNav);
getSupportActionBar().setDisplayShowCustomEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(false);
getSupportActionBar().setDisplayShowTitleEnabled(false);
setActionBar();
}

Related

Android Toolbar set title explanation

I have my app Toolbar here:
toolbar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
When i want to set the screen title, i have to do the following:
ActionBar bar = getSupportActionBar();
if (bar != null) {
bar.setTitle(SCREEN_TITLE);
}
My questions is, why when calling the toolbar.setTitle() is not working. But instead i need to get the action bar first and then i can set the title?
P.S. In the other hand, I can set the Toolbar icon normally without getting the action bar as so:
toolbar.setNavigationIcon(R.drawable.ic_arrow_back_white);
A Toolbar is a generalization of action bars for use within application layouts.
Unlike Action bar , toolbar is not part of window decor.You defines it and place it just like any other widget...therefor you have freedom to place it anywhere in the parent layout.
You have freedom to put any widget inside toolbar.
You can define multiple toolbars.
Toolbar may be placed at any arbitrary level of nesting within a view hierarchy. An application may choose to designate a Toolbar as the action bar for an Activity using the setActionBar() method.
As Toolbar is generalization of ActionBar. you have to first set Toolbar as ActionBar then you can use all method of ActionBar.
EDIT:
Here is Link that explains Toolbar concept.
Here is bug link reported in android issues: In issue they suggest that Once you call setSupportActionBar(Toolbar), the Action Bar is then responsible for handling the title, meaning that you need to call getSupportActionBar().setTitle(...) to set a custom title. It also gives explanation about why toolbar.setNavigationIcon(R.drawable.ic_arrow_back_white); works.
I hope it helps!

Android - How to get rid of the bar above the ActionBar

EDIT: Android-Developers answer below explains solution very well
My problem is there are another bar, not exactly sure what it's name is - TitleBar? (The bar with the menu button and the white square logo) Above my ActionBar. What I ideally want is to merge my ActionBar with that bar or if not possible, implement my own bar completely amongst the tabs with icon and menu button. I have tried several things (see below image) and the rough code of how I am add the tabs is shown below as well.
Adding tabs to the ActionBar code:
actionBar = getActionBar(); // Get reference to ActionBar
// Add some navigation tabs...
// 1. Enable ActionBar navigation tabs
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.setDisplayShowTitleEnabled(false);
// 2. Add the tabs
Tab programTab = actionBar.newTab();
Tab settingsTab = actionBar.newTab();
Tab historyTab = actionBar.newTab();
programTab.setText("Program")
.setTabListener(new TabListener<Fragment_Program>(
this, "Program", Fragment_Program.class));
settingsTab.setText("Settings")
.setTabListener(new TabListener<Fragment_Settings>(
this, "Settings", Fragment_Settings.class));
historyTab.setText("History")
.setTabListener(new TabListener<Fragment_History>(
this, "History", Fragment_History.class));
actionBar.addTab(programTab);
actionBar.addTab(settingsTab);
actionBar.addTab(historyTab);
I was under the impression getActionBar() got reference to the existing bar which sits above my ActionBar in the screenshot.. that would be ideal.
Current application theme is android:theme="#android:style/Theme.Holo.Light.DarkActionBar"
Anyway, I have tried the following:
Various manifest changes including various .NoTitleBar combinations
requestWindowFeature(Window.FEATURE_NO_TITLE); after super.onCreate() and before setContentView()
The reason I am using standard ActionBar is this for a hardware device that will be running at least 4.0 and the version is highly unlikely to change.
Any thoughts?
Cheers
As I can understand you want to remove that view right?
The problem is that it your ActionBar. I guess you want to use Tabs, but without the ActionBar like many of the apps which are doing that. To achieve this use :
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
bar.setDisplayShowTitleEnabled(false);
bar.setDisplayShowHomeEnabled(false);
But don't forget something, if you want to achieve this, you should not inflate any menu in your Fragment / Activity :
#Override
public boolean onCreateOptionsMenu(Menu menu) {
//getSupportMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
You should see #ColorWP answer here
The Window flags are set already set inside super.onCreate(savedInstanceState); in which case you may want to use the following order of commands:
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);

Android custom action bar with back and next button

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.

Sherlock action bar's menu item background

i am having issue with rendering custom view in Sherlock action bar. what happen is i've successfully set the custom view in action bar designed to set the Title textview in center, but when i set the other menu items like refresh then it's background appears with black color. same things happens with home button. please go through the following snapshot.
as you can see in this image when i set home button visible or set the refresh menu item it should not displayed like this with black background.
in my Activity screen
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu_activity);
....
View customNav = LayoutInflater.from(this).inflate(R.layout.custom_navigation, null);
TextView textView = (TextView)customNav.findViewById(R.id.screen_title);
textView.setText("Category");
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(customNav);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0,Constants.REFRESH_ITEM_ID,0,Constants.LABEL_REFRESH)
.setIcon(R.drawable.ic_refresh)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
return true;
}
I've tried a lot but fail to fix it.Please help me.
Thanks in Advance.
It seems you use Theme.Sherlock.Dark in manifest file for whole application or this activity.
It is normal behavior of ActionBar -- it is defined by style, but your custom view-element has is own backround, text style, etc...
From setCustomView() javadoc:
Custom navigation views appear between the application icon and any action buttons and may use any space available there.
This method just add new view to bar, but doesn't set 'view for whole bar'.
Take a look at http://jgilfelt.github.com/android-actionbarstylegenerator -- this is a style generator for action bar. Pay attention to the toggle button "Style compatibility".
I have found the solution of this not exactly the way i wanted but the other way. actually i've created the images with centred screen title for various screens and setting it as the action bar background rather than creating a custom view and SherlockActionBar is flexible enough to hide the default title displayed on the upper-left corner. all i have done is.
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar()
.setBackgroundDrawable(getResources().getDrawable(R.drawable.menu_head));

Action Bar remove title and reclaim space

I am trying to remove the title from my action bar and reclaim the space it would otherwise consume for the use of menu items.
I have tried using a custom theme but so far nothing has worked. I have tried changing the text size to 0sp, changing the color to transparent, and setting visibility to gone.
I have also tried this :
ActionBar actionbar = getActionBar();
actionbar.setDisplayShowTitleEnabled(false);
I am targeting sdk 15+ and using the standard Android action bar (i.e. not Sherlock).
Thanks for any suggestions.
Here's what I'm talking about (the area bordered in red I want back):
Did you check out the ActionBar documentation yet? There's a method, setDisplayShowTitleEnabled(boolean) that does what you need.
Simply get a reference to your ActionBar, and call that with false:
ActionBar actionBar = getActionBar();
actionBar.setDisplayShowTitleEnabled(false);

Categories

Resources