I am trying to display a custom view in the action bar. I am using SherlockActionBar.
Here is my code. The custom view is never showing. What am I doing wrong?
View customNav = LayoutInflater.from(this).inflate(R.layout.my_layout, null);
getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setCustomView(customNav);
You must enable custom views first.
getSupportActionBar().setDisplayShowCustomEnabled(true); // missing in your code
getSupportActionBar().setCustomView(customNav);
Related
I wanted to know how to add my own custom font to the app name that appears in the action bar in android studio.
You can set CustomLayout in ActionBar
this.getActionBar().setDisplayShowCustomEnabled(true);
this.getActionBar().setDisplayShowTitleEnabled(false);
LayoutInflater inflator = LayoutInflater.from(this);
View v = inflator.inflate(R.layout.titleview, null);
//if you need to customize anything else about the text, do it here.
//I'm using a custom TextView with a custom font in my layout xml so all I need to do is set title
((TextView)v.findViewById(R.id.title)).setText(this.getTitle());
//assign the view to the actionbar
this.getActionBar().setCustomView(v);
You can assign the custom font to textview by using setTypeFace()
I did this by just making an image of the app name with the custom font then displaying the image as the logo in the action bar. Much simpler.
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayUseLogoEnabled(true);
actionBar.setLogo(R.drawable.app_logo);
I have a login screen with two options in my app - "login" and "create account". I want to implement the following thing, for example, on login screen :
I've got one activity and another, by tapping on "UP" button i want to return back. I have the following constructor of desired activity:
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bl__login_form_view_controller);
getActionBar().setDisplayHomeAsUpEnabled(true);
//----------------------added source - no effect-----------------------
getActionBar().setDisplayShowHomeEnabled(true);
getActionBar().setIcon(R.drawable.circle) ;
getActionBar().setTitle("A title");
//---------------------------------------------------------------------
setTitle ("Вход") ;
}
And i have UP button. How can i add circle image and some text? All tutorials say, that i can set app icon in AppManifest, but it would change app icon on main screen. And how can i add text to back button? I don't wanna implement any navigation logic or set parent activity for whole app, because it's a login screen, and the main menu with the navigation logic will be shown only after authorization. Thanks in advance!
setTitle will set the title and setIcon will set the icon to the ActionBar.
getActionBar().setTitle("Your Title");
getActionBar().setDisplayShowHomeEnabled(true);
getActionBar().setHomeButtonEnabled(true);
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setIcon(R.drawable.youricon);
Reference :Adding Up Action and http://developer.android.com/guide/topics/ui/actionbar.html
What about:
getActionBar().setIcon(R.drawable.circle);
use getActionBar().setIcon(R.drawable.youricon) for setting the icon and getActionBar().setTitle("A title"); for setting the text next to the icon.
For the icon:
getActionBar().setIcon(R.drawable.youricon);
For the title:
getActionBar().setTitle("A title");
you can use custom action bar using your own layout
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setHomeButtonEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);
final ViewGroup actionBarLayout = (ViewGroup)getLayoutInflater().inflate(R.layout.yourXML, null);
actionBarLayout.findViewById(R.id.buttonId).setOnClickListener(this);
actionBar.setCustomView(actionBarLayout);
its work for me. so i hope its work for you.
The accepted answer serves the problem well. I'm just adding some additional information of using a custom action bar layout.
getActionBar().setTitle("Your Title");
getActionBar().setDisplayShowHomeEnabled(true);
getActionBar().setHomeButtonEnabled(true);
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setIcon(R.drawable.youricon);
// You can use setLogo instead like
// getActionBar().setIcon(R.drawable.youricon);
// In case of support action bar, if its not showing properly, you need to add display options
//getActionBar().setDisplayOptions(actionBar.getDisplayOptions() | ActionBar.DISPLAY_SHOW_CUSTOM);
Now here's a code segment describing how to use a custom layout in android actionbar.
// Make a layout named 'custom_actionbar' and simply add elements you want to show in your actionbar.
final ViewGroup actionBarLayout = (ViewGroup) getLayoutInflater()
.inflate(R.layout.custom_actionbar, null);
// Now for support action bar, get the action bar
actionBar = getSupportActionBar();
// Set necessary attributes
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
actionBar.setElevation(0);
// Set the custom layout in actionbar here
actionBar.setCustomView(actionBarLayout);
actionBar.setDisplayOptions(actionBar.getDisplayOptions()
| ActionBar.DISPLAY_SHOW_CUSTOM);
// Now handle the elements of the custom layout here
Button b1 = (Button) findViewById(R.id.button1);
// Set actions for Button 1 here
b1.setText("Button 1");
// Another element in custom actionbar
ImageView iv1 = (ImageView) findViewById(R.id.myImage);
iv1.setBackgroundResource(R.drawable.myImagePlaceHolder);
Place the codes in onCrate and check yourself.
I have a custom title bar, a simple relative layout, that I set as the custom title bar of my activity. But at want the android BackAsUp icon < to show up as usual (i.e. to the left of my custom layout). How do I do that? here is my code so far.
this.getActionBar().setDisplayShowCustomEnabled(true);
this.getActionBar().setDisplayShowTitleEnabled(false);
LayoutInflater inflator = (LayoutInflater) this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflator.inflate(R.layout.my_title_bar, null);
this.getActionBar().setCustomView(v);
Update:
I have tried the answer at Remove Icon but have HomeAsUp in ActionBar they don't work. So one question, is order of setting those flags matter?
Adding the following combo solved the problem.
getActionBar().setIcon(
new ColorDrawable(getResources().getColor(android.R.color.transparent)));
this.getActionBar().setDisplayShowHomeEnabled(true);
I apply a custom View to the ActionBar, like this
// Inflate the "Done/Discard" custom ActionBar view.
LayoutInflater inflater = (LayoutInflater) DetailsHost.mActionBar
.getThemedContext().getSystemService(DetailsHost.LAYOUT_INFLATER_SERVICE);
final View customActionBarView = inflater.inflate(
R.layout.actionbar_custom_view_done_discard, null);
// Show the custom ActionBar view and hide the normal Home icon and title.
DetailsHost.mActionBar.setDisplayOptions(
ActionBar.DISPLAY_SHOW_CUSTOM,
ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_HOME
| ActionBar.DISPLAY_SHOW_TITLE);
DetailsHost.mActionBar.setCustomView(customActionBarView,
new ActionBar.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
(based on Roman Nuriks code).
How do I restore the initial layout? Note: I use ActionBarSherlock
Show/hide custom actionbar view
Since you only added a custom view to the bar without removing the title it should be sufficient to hide that custom View. You can use method setDisplayShowCustomEnabled(). Just call:
getActivity().getActionBar().setDisplayShowCustomEnabled(false);
And enable home functionality again:
getActivity().getActionBar().setDisplayShowHomeEnabled(true);
(Note in all code examples use getSupportActionBar() instead of getActionBar() if you're using actionbar compat. Also the getActivity() is only needed from fragments, in activities refer to the activity itself, in most cases this)
Restore actionbar title
If however you also removed the title when creating your custom view you'll have to enable that again also.
getActivity().getActionBar().setDisplayShowTitleEnabled(true);
Restore completely
You can also call the setDisplayOptions() method with a combination of options to reconfigure the actionbar in one call. The below example removes the custom view and shows the title.
getActivity().getActionBar().setDisplayOptions(
ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_TITLE);
See Android API docs for more details on these options.
ActionBar actionbar;
actionbar = getActionBar();
Button cls = (Button)findViewById(R.id.btn_close);
cls.setOnClickListener(new OnClickListener(){
public void onClick(View view){
actionbar.setDisplayShowCustomEnabled(false);
}
});
Note:
The button with id 'btn_close' was located in the custom actionbar layout.This function was written in mainactivity.
Hope this Helps!!
I have Tab-bar and custom title-bar in same activity.In static mode am able to display tab-bar and custom title-bar in the same activity. But when i assign dynamic values to custom title-bar attributes it shows error as
AndroidRuntimeException: You cannot combine custom titles with other title features
Please provide any suggestions.
Thanks in advance
Try:
this.getActionBar().setDisplayShowCustomEnabled(true);
this.getActionBar().setDisplayShowTitleEnabled(false);
this.getActionBar().setDisplayUseLogoEnabled(true);
final LayoutInflater inflator = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View v = inflator.inflate(R.layout.header_bar, null);
//if you need to customize anything else about the text, do it here.
//I'm using a custom TextView with a custom font in my layout xml so all I need to do is set title
((TextView)v.findViewById(R.id.header_bar_title)).setText(this.getTitle());
//assign the view to the actionbar
this.getActionBar().setCustomView(v);
and make sure you do this before setContentView() method.