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));
Related
I'm trying to customize the font in one activity action bar. I managed to do it by using this code during activity's onCreate:
if(getSupportActionBar()!=null){
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.secondary_toolbar);
TextView actionBarTitle = getSupportActionBar().getCustomView().findViewById(R.id.toolbar_title);
actionBarTitle.setText(R.string.new_payment_method);
}
The problem is that if I change the toolbar to a custom layout, the code lines which enables the arrow to let the activity to navigate to the previous activity doesn't work anymore. I'm stuck finding a way to modify the font in the action bar and showing the back arrow which allows me to navigate back, at the same time.
I have seen a lot of similar questions to mine, but they all seem to differ in some way that makes them not so helpful.
I have a split action bar in my activity. I have customized the top action bar to display my own view since I wanted to add icons to the top and not show the launcher icon or activity title. I have added a menu with icons which appears at the bottom. Here is the code that does both these things:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_basic_fractions);
ActionBar actionBar = getActionBar();
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);
View view = View.inflate(this,R.layout.action_bar_top,null);
actionBar.setCustomView(view, new ActionBar.LayoutParams(Gravity.RIGHT | Gravity.CENTER_VERTICAL));;
}
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_basic_fractions, menu);
return true;
}
This code produces the effect I wanted - a split menu with some icon buttons at the top (from the layout view) and some at the bottom (from the menu)
However, I want to add a drop down list to the top action bar. I have found ways to do it if I had a menu there instead of a layout view (for example with submenus or actionLayout), but not with this mixed kind of action bar.
An alternative would be if I could add a menu instead of a layout to be in the top action bar since I am not adding anything special, just a few normal menu buttons, but I couldn't find anyway to force them to appear in the top action bar without making it a layout like this.
Thanks for your help.
This is very similar to many question already posted on stackoverflow, but I still haven't found the right solution. The problem I'm facing is how to implement a custom actionbar.xml file and add a navigation drawer function to the top left icon.
I'm trying to create an action bar that looks like this with two ImageButtons on side and an ImageView as a logo in the center. When user presses on the left ImageButton from actionbar.xml, I would like navigation drawer coming from the left, like in this tutorial. The last thing that troubles me, is how to put a title of pressed fragment in the center of the action bar instead of a logo.
I've already written all xml and java files, but I just can't put the pieces together. Please help.
I believe you have solution to create navigation drawer.
Now for your question HOW TO CREATE A CUSTOM ACTION BAR
Try like this ..
actionBar = getSupportActionBar(); // Get Action Bar reference.
actionBar.setCustomView(R.layout.action_bar_confirm_cabs); Set your action bar view like this
fare_ll = (TextView) actionBar.getCustomView().findViewById(R.id.fare_ll); Get id of your custom view like this ...
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); // Set property for Custom view like this.
fare_ll.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
That's it.... You are good to go.
I would like to show/hide the action bar upon a click.
It does show and hide but it is not smooth...the bottom part hides but a different background for a little while before disappearing.
I even tried it in a simple hello world app and the result is the same.
Here's the code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tv = (TextView) findViewById(R.id.shit);
tv.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
getActionBar().hide();
}
});
}
Use overlay mode instead of showing and hiding the actionbar.
From the actionbar documentation:
Beware that hiding and removing the action bar causes your activity to
re-layout in order to account for the space consumed by the action
bar. If your activity regularly hides and shows the action bar (such
as in the Android Gallery app), you might want to use overlay mode.
Overlay mode draws the action bar on top of your activity layout
rather than in its own area of the screen. This way, your layout
remains fixed when the action bar hides and re-appears. To enable
overlay mode, create a theme for your activity and set
android:windowActionBarOverlay to true. For more information, see the
section about Styling the Action Bar.
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();
}