I'm having this strange behavior with the action ActionBar Tabs showing above the ActionBar.
This happens to be happening when I am setting a custom view for the ActionBar.I'm implementing the Done-Discard pattern using Roman Nurik's example here
This is happening due to the masking of ActionBar.DISPLAY_SHOW_HOME in setDisplayOptions()
final ActionBar actionBar = getActionBar();
actionBar.setDisplayOptions(
ActionBar.DISPLAY_SHOW_CUSTOM,
ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_HOME
| ActionBar.DISPLAY_SHOW_TITLE);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.setCustomView(customActionBarView, new ActionBar.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
The screen looks like this:
But when I dont mask the ActionBar.DISPLAY_SHOW_HOME it works fine but the App Logo is displayed. like this.
This seems to be a bug.Please suggest a fix.I don't want the logo to be displayed.
Solution here:
ActionBarSherlock - Tabs appearing ABOVE actionbar with custom view
and here: https://github.com/JakeWharton/ActionBarSherlock/issues/327
This seems a little hacky to me but here's the workaround:
Add this piece of code in your onCreate and hide the Home Icon. Now the ActionBar and Tabs work as expected.
Remember not to Disable/Mask the DISPLAY_HOME_HOME in the ActionBar.setDisplayOptions().This will not work if you mask/disable it.
View homeIcon = findViewById(android.R.id.home);
((View) homeIcon.getParent()).setVisibility(View.GONE);
First set "DisplayShowHomeEnabled" property of actionbar to "true":
actionBar.setDisplayShowHomeEnabled(true);
and then:
View homeIcon = findViewById(android.R.id.home);
((View) homeIcon.getParent()).setVisibility(View.GONE);
((View) homeIcon).setVisibility(View.GONE);
I hope it helps :)
Related
A client wants a big logo on the actionbar instead of an icon and I didn't found a way to set the padding to 0dp to stretch it to the height of the action bar. So I went for a custom view to display the logo, this looks good in potrait, but in landscape the custom view positioned after the tabs.
I tried the solutions I found here, but with no result.
Is there a way to postion the custom view before the tabs?
The code:
// Set up the action bar.
final ActionBar actionBar = getActionBar();
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_HOME);
//actionBar.setDisplayShowHomeEnabled(true);
//actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setCustomView(R.layout.custom_actionbar);
actionBar.setLogo(null);
View homeIcon = findViewById(android.R.id.home);
((View) homeIcon.getParent()).setVisibility(View.GONE);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
Edit:
After more research I'll dump the action bar, there is no way to do this.
I need to do 2 things over the ActionBar:
First would be adding an image on the right hand side of it.
Second, hide the icon/logo of the ActionBar's title
The result would be like this:
To achieve the first, I'm following the answer here, but it doesn't situate the image on the right side, it gets near the title. This is the code extracted from that post:
ActionBar actionBar = getActionBar();
actionBar.setDisplayOptions(actionBar.getDisplayOptions()
| ActionBar.DISPLAY_SHOW_CUSTOM);
ImageView imageView = new ImageView(actionBar.getThemedContext());
imageView.setScaleType(ImageView.ScaleType.CENTER);
imageView.setImageResource(R.drawable.ic_launcher);
ActionBar.LayoutParams layoutParams = new ActionBar.LayoutParams(
ActionBar.LayoutParams.WRAP_CONTENT,
ActionBar.LayoutParams.WRAP_CONTENT, Gravity.RIGHT
| Gravity.CENTER_VERTICAL);
layoutParams.rightMargin = 40;
imageView.setLayoutParams(layoutParams);
actionBar.setCustomView(imageView);
This is my imageview containing layout:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/ablogo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="#drawable/ic_launcher" />
</RelativeLayout>
Then, to get the second thing, this is, hide the default title icon, I simply use this:
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayShowHomeEnabled(false);
Doing this, I get to hide the icon, but the Tabs go above the ActionBar, this is, the actionBar gets situated below the Tabs.
For the first workaround, I created a layout to set as custom actionBar. The layout would be the same as above's, but the way to define it in the MainActivity changes this way:
/*Inflate your custom actionBar layout*/
final ViewGroup actionBarLayout = (ViewGroup) getLayoutInflater().inflate(R.layout.actionbar_logo, null);
/*Ads tabs*/
ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
/*Set up your ActionBar*/
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setCustomView(actionBarLayout);
This way I get to set the icon on the right hand side. But still have to solve an issue with the title.
For the second workaround, I don't hide/dissable the icon that way, I show it, but then I set it invisible. This way the tabs get perfectly situated under the actionBar.
actionBar.setDisplayShowHomeEnabled(true);
View homeIcon = findViewById(android.R.id.home);
((View) homeIcon.getParent()).setVisibility(View.GONE);
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.
I want to customize my actionbar like the Image shown below.
I am using Actionbarsherlock in implementation, as I have shown I will have a total of 5 icons, where 3 of them are centered and the other 2 are at the sides.
There are also separators added,
How do I add a style like this?
Just create a layout. As a separator you can use view with width of 1dp and your icons will be imageviews. Then set this layout to your actionbar.
You can add layout to your actionbar like that:
ActionBar actionBar = getSupportActionBar();
View actionBarView = getLayoutInflater().inflate(
R.your_layout, null);
actionBar.setCustomView(actionBarView);
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
actionBar.setDisplayUseLogoEnabled(true);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
Then if you want to add onClickListener to your ImageView, you can do it like this:
ImageView imageViewOption = (ImageView) actionBarView.findViewById(R.id.image_view_option); //it's important to use your actionbar view that you inflated before
imageViewOption.setOnClickListener(...);
And if you don't want to have your application icon in actionbar then you can play with setDisplayXXX options. You can find more here: http://developer.android.com/guide/topics/ui/actionbar.html
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!!