Add image on ActionBar and hide icon - android

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

Related

ActionBar when landscape then custom view first and tabs on the right

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.

Put launcher icon at right

I want my launcher icon to be shown at the top right side of my android project instead of its default place that is top left. How can I do that?
I couldn't find any related and helpful example.
Thanks
Following code worked for setting icon on top left corner(in ACTION BAR) :
The image name used is "conect_icon"
public void applyActionBar(android.support.v7.app.ActionBar ab,
ActionBarActivity mContext,View
custom) {
ab = mContext.getSupportActionBar();
ab.setCustomView(custom);
ab.setTitle(R.string.st_action_title_main);
ab.setDisplayHomeAsUpEnabled(true);
ab.setDisplayShowTitleEnabled(false);
ab.setDisplayHomeAsUpEnabled(true);
ab.setDisplayShowHomeEnabled(true);
ab.setDisplayShowCustomEnabled(true);
ab.setLogo(R.drawable.conect_icon);
ab.setDisplayUseLogoEnabled(true);
}
I'm assuming that you need your launcher icon to be shown in the right side of the actionbar instead of the left side. Although it is not suggested as a good practice by the UI patterns guidelines from the Android developer channel, you can do that by applying a customised Android actionbar. Use the following layout as your actionbar view.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="right"
android:orientation="horizontal">
<!-- Place your launcher icon image view here -->
</LinearLayout>
Then finally set the actionbar layout as follows.
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayShowHomeEnabled(false);
LayoutParams lp1 = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
View customNav = LayoutInflater.from(this).inflate(R.layout.actionbar, null); // your new layour
actionBar.setCustomView(customNav, lp1);
Hope this helps.

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.

Customize actionbar in actionbarsherlock

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

ActionBar Tabs Navigation with Custom ActionBar view strange behavior

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

Categories

Resources