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.
Related
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!
I want to change the Hamburger icon on Android ActionBar to Up arrow on button click, and also to change the Up arrow back to Hamburger icon on another button click event.
I have tried using ,
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
However, none of them works. I can change the Hamburger icon to up arrow easily using,
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
But not the vice-versa.
Also, how do I override the home button control?
I mean when a certain layout is open, I want the up arrow to close that layout on click and not to call the navigation drawer.
Please help.
You could use the following piece of code:
ViewGroup home =(ViewGroup)findViewById(android.R.id.home).getParent();
// get the first child (up imageview)
((ImageView) home.getChildAt(0)).setImageResource(your image here);
Sorry, no privileges to comment so updating the answer. Well, since this is an imageView, I am assuming you could place an onClickListener and toggle between using the hamburger and the back arrow. This should be close to the material design's animated hamburger behavior.
I want to create a toolbar or bar after action bar.
In toolbar if i click on previous button the text will change to previous date and if i press next button it is will change to next date.
How I can create the bar after action bar.
Simply use the new Toolbar view. It was added to the support lib along with Android 5 and what it does is allow you to add the ActionBar as a simple View. You can then add your below view with simple "android_below="#id/toolbarId".
If i use default actionbar i can set home button to open and close drawerlayout with this code.
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
And it's work well.
But i dont't want default actionbar i want actionbar with customlayout.
When i use custom actionbar i don't know how to implement home button for open and close drawerlayout.
How to implement drawerlayout with custom action bar?
PS. I use custom actionbar because i want set title text to align center of actionbar, set text and text size to it's.
EDIT
thank you every body i can solve it's by use this.
getActionBar().setCustomView(R.layout.actionbar);
getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_HOME_AS_UP);
it's work well but the home button image change from app logo to app icon.
You can create custom Action bar like this ..
// gets actionBar reference ..
actionBar = getSupportActionBar();
// This sets custom layout to action bar
actionBar.setCustomView(R.layout.action_provider);
// this is how... .... you get id of layout defined
iv_d = (ImageView) actionBar.getCustomView().findViewById(R.id.action_menu);
Now just open you drawer on click of this icon using OnClick()
That's it .. you are good to go.
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));