use android backAsUp with custom title bar - android

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);

Related

How to add custom font to the action bar?

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);

Tabs Coming above Action bar when inflating custom layout

I tried everything mentioned here on all Stackoverflow's other answers but its not working out. Here is my code.
actionBar = getActionBar();
actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#ffffff")));
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setCustomView(R.layout.actionbar_layout);
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.setLogo(null);
actionBar.setDisplayShowTitleEnabled(false);
View homeIcon = findViewById(android.R.id.home);
((View) homeIcon.getParent()).setVisibility(View.GONE);
The tabs show on top when you hide the Home item. It is a bit counter intuitive but it also makes some design sense. They're essentially nudging you to use the tabs as titles for the sections and use the action bar below them for actions inside those sections.
You need setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_HOME) to bring the tabs down again. It will probably need a non-null logo too (you can make a 1px transparent one in -nodpi to make it disappear)
Using the accepted solution leaves a left padding on the actionbar, which shrinks your custom view.
the solution is to add this code in the onCreate of the Activity:
View homeIcon = findViewById(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ? android.R.id.home : R.id.abs__home);
((View) homeIcon.getParent()).setLayoutParams(new LinearLayout.LayoutParams(0, 0));
((View) homeIcon).setVisibility(View.GONE);
getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_HOME);
getActionBar().setDisplayUseLogoEnabled(true);
getActionBar().setDisplayShowCustomEnabled(true);
getActionBar().setCustomView(R.layout.actionbar_layout);
This worked for me.
Please try to use LayoutInflater to inflate the view then set the view to the actionbar.
LayoutInflater inflator = (LayoutInflater) this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflator.inflate(R.layout.layout, null);
actionBar.setCustomView(v);
And from this
use RelativeLayout instead of LinearLayout as the main container. It's
important to have android:layout_gravity="fill_horizontal" set for it.
That should do it.

ActionBar custom layout like ICS edit contact

I just started playing with the new support library with ActionBar support. I'm trying to implement a bar that looks basically identical to the Ice Cream Sandwich layout on the edit contact screen. I understand I probably need to implement a custom view something similar to this - How to display custom view in ActionBar?. What I don't understand is exactly what that view is, and the best way to implement it.
Here's the screenshot of what I want in my actionbar:
Is that just a view with an image and some text, or a styled button, or something totally different? It has some state pressed properties.
Thanks for the help.
You can try using a custom view, by adding the following code when you want the button to appear :
// Inflate the view from XML file
LayoutInflater inflator = (LayoutInflater) this .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflator.inflate(R.layout.search, null);
// Tell the view to occupy the whole actionBar view (MATCH_PARENT)
LayoutParams layout = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
getActionBar().setCustomView(v, layout);

Restore default actionbar layout

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!!

Can i take tab bar and custom title-bar in same Activity

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.

Categories

Resources