I want to set the actionbar icon and title to be visible for an activity.
My app theme turns them off:
// styles.xml
<item name="android:displayOptions"></item>
In my activity:
public void onCreate() {
...
ActionBar actionBar = activity.getActionBar();
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setIcon(R.drawable.ic_launcher);
actionBar.setHomeButtonEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
}
The title and up arrow appear. But I can't get the icon to appear. What am I doing wrong?
You are missing setDisplayShowHomeEnabled().
Related
Is there any way to change the ActionBar color of fragments and change the title displayed in the ActionBar to the title of the menu option that was pressed?
If you are using AppCompat you always have to call getSupportActionBar()
Change Title :
getSupportActionBar().setTitle("Your Title");
Change Color :
getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.YOUR_COLOR));
If in Fragment :
Change Title :
getActivity().getActionBar().setTitle("YOUR TITLE");
Change Color :
getActivity().getAcationBar().setBackgroundDrawable(new ColorDrawable(Color.YOUR_COLOR));
Hope this help you
This is the suggested answer written in java :
getActionBar().setTitle("Test");
getActionBar().setBackgroundDrawable(new ColorDrawable("COLOR"));
If you're using support libraries:
android.support.v7.app.ActionBar supportActionBar = ((AppCompatActivity) getActivity()).getSupportActionBar();
if (supportActionBar != null) {
supportActionBar.setTitle("My action bar title");
View view = new View(getContext());
view.setBackgroundColor(getResources().getColor(R.color.my_color));
supportActionBar.setBackgroundDrawable(view.getBackground());
}
Otherwise:
android.app.ActionBar actionBar = getActivity().getActionBar();
if (actionBar != null) {
actionBar.setTitle("My action bar title");
View view = new View(getContext());
view.setBackgroundColor(getResources().getColor(R.color.my_color));
actionBar.setBackgroundDrawable(view.getBackground());
}
I created an app with navigation drawer. I want to change the color of actionbar and default icon of drawer to be changed on selection of a particular navigation menu item and it should change to default values on selection of any other item.
My code
Fragment fragment;
if(fragment.toString().contains("HomeFragment")){
ab = ((BaseActivity)getApplicationContext()).getSupportActionBar();
ab.setHomeButtonEnabled(true);
ab.setDisplayHomeAsUpEnabled(true);
ab.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#FFFFFF")));
ab.setElevation(0);
ab.setDisplayHomeAsUpEnabled(true);
ab.setHomeAsUpIndicator(R.drawable.username);
}
I could change actionbar color and navigation drawer icon with the above code,but can't change to default color and icon on other navigation item selection.
Just continue your code
Fragment fragment;
if(fragment.toString().contains("HomeFragment")){
ab = ((BaseActivity)getApplicationContext()).getSupportActionBar();
ab.setHomeButtonEnabled(true);
ab.setDisplayHomeAsUpEnabled(true);
ab.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#FFFFFF")));
ab.setElevation(0);
ab.setDisplayHomeAsUpEnabled(true);
ab.setHomeAsUpIndicator(R.drawable.username);
} else {
ab = ((BaseActivity)getApplicationContext()).getSupportActionBar();
ab.setHomeButtonEnabled(true);
ab.setDisplayHomeAsUpEnabled(true);
ab.setBackgroundDrawable(new ColorDrawable(Color.parseColor("your default color")));
ab.setElevation(0);
//ab.setHomeAsUpIndicator(R.drawable.username); change it to your other icon;
}
is it possible to hide the title bar(Entertainment) but not the titles of the tab views(top rated,games,movies)?
you can try for Hiding title bar
final ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
Hello I am using Sherlock library to achieve the Tabs. Here I am looking to remove the ActionBar at the top and set the custom title layout instead of it.
I achieved a functionality to add the Tabs but could not set the custom title over ActionBar. Any way to do this ?
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pay_stub);
// adding action bar tabs to the activity
ActionBar mActionBar = getSupportActionBar();
Tab firstTab = mActionBar.newTab().setText(getString(R.string.paystub_current_tab)).setTabListener(this);
Tab secondTab = mActionBar.newTab().setText(getString(R.string.paystub_all_tab)).setTabListener(this);
// add the tabs to the action bar
mActionBar.addTab(firstTab);
mActionBar.addTab(secondTab);
// set the navigation mode the Action Bar Tabs
mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
}
Thanks in advance.
I resolved this by adding background color and icon of the ActionBar.
ActionBar actionBar = getSupportActionBar();
setTitle("");
actionBar.setIcon(R.drawable.headerlogo);
actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#F4A401")));
I'm setting an ActionBar in my app with the Compatibility version. For now I've done:
Import android-support-v7-appcompat and add as library to my project
Set Aplication theme as: Theme.AppCompat
Extend Activities to ActionBarActivity
After this, I use a method to set dynamically the subtitle:
private final void setStatus(int resId) {
ActionBar actionBar = getSupportActionBar();
actionBar.setSubtitle(resId);
}
private final void setStatus(CharSequence subTitle) {
ActionBar actionBar = getSupportActionBar();
actionBar.setSubtitle(subTitle);
}
While testing the app, the subtitle doesn't appear. If I add this:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_TITLE);
Then the subtitle appears, but the app icon dissapears. What can I do to mantain the app icon while showing the subtitle?
The display options are bitfields, so you should be able to enable several at the same time (using the OR operator), like this:
getSupportActionBar().setDisplayOptions(
ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_TITLE);
Or, to just add one value without affecting other fields, call the version with bitmask:
getSupportActionBar().setDisplayOptions(
ActionBar.DISPLAY_SHOW_TITLE,
ActionBar.DISPLAY_SHOW_TITLE);
This is what I get to solve the problem:
/**Resolves the issue, shows the app icon*/
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayShowHomeEnabled (true);
Use getSupportActionbar instead of actionbar
Actionbar actionbar = getSupportActionBar()
actionbar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
actionbar.setIcon(YOUR ICON);
Ok, all of the above answer looks similar with minor differences, none of it work for me but this combo
final ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_HOME);
actionBar.setDisplayShowHomeEnabled (true);
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setIcon(R.drawable.rn_logo_icon);
Please note, this fix is if you are using AppCompat theme