Android custom action bar remove icon - android

Below is a screenshot of my current setup.
I have created a custom action bar view, which I set in the code below, and what I would like is two images, one left aligned in the title bar, the other right aligned.
The problem is, that when I hide the app icon, it only hides it, not removes it hence the gap on the left. I found a couple of other SO questions that show how to remove the icon, but that also removes the tabs which I want to keep.
Can any one offer me a solution?
From my onCreate() function:
final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.action_bar_title, null);
View homeIcon = findViewById(android.R.id.home);
((View) homeIcon.getParent()).setVisibility(View.GONE);
((View) homeIcon).setVisibility(View.GONE);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setCustomView(v);
My xml custom layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="fill_horizontal"
android:layout_marginLeft="0dp"
android:orientation="horizontal"
>
<ImageView android:id="#+id/title_img_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="#drawable/test" />
<ImageView android:id="#+id/title_img_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="#drawable/test" />
</RelativeLayout>
I'm using the Holo.Light.DarkActionBar theme I believe.

So if I understand the thing which you are trying to achieve, why you just don't add a MenuItem with your custom image on left side and set you actionBarLogo with the image which you want to be on the right side. After that just override home button click and I guess you will achieve the same thing which you can create with custom view, without the padding on the left side of ActionBar.
P.S. If I understand you wrong, please give some more information on how it should look like.

the code is working!!!
ActionBar actionBar = getSherlockActivity().getSupportActionBar();
View customNav = LayoutInflater.from(getActivity()).inflate(R.layout.custom_action_bar_view, null);
ImageButton imageButton = (ImageButton) customNav.findViewById(R.id.imageButton);
ActionBar.LayoutParams lp = new ActionBar
.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
actionBar.setCustomView(customNav, lp);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowHomeEnabled(false);

Related

Android custom Action bar issue

I am working on Custom ActionBar and using below code :
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
LayoutInflater inflator = (LayoutInflater) this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
customView = inflator.inflate(R.layout.action_bar_title, null);
actionBar.setCustomView(customView);
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM
| ActionBar.DISPLAY_SHOW_HOME);
actionBar.setDisplayHomeAsUpEnabled(true);
but facing extra space issue on right side so please check screen shot for selected area.
some month ago i also faced this problem after that i found this solution. there is one trick you can resolve this issue, I know that is not right answer but by some logic you can get what you want.
<ImageView
android:id="#+id/addItem"
android:layout_width="35dp"
android:layout_height="35dp"
android:padding="10dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:contentDescription="#null"
android:src="#drawable/add" />
or do one thing you can put your add and info button to linearlayout and give right alignment as true,i am not sure about this but you should try once.

How to set app icon between home indicater icon and app title on action bar

This is my Action Bar i wanna set app icon between arrow and activity title so please tell me how to achieve this OR instead of acivity title i can set app icon there?
Please tell me how to achieve this.
i have used this code to do so.
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayUseLogoEnabled(true);
actionBar.setIcon(R.drawable.ic_notification1);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setHomeAsUpIndicator(R.drawable.ic_drawer);
In code:
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.layout_actionbar);
In layout folder make layout_actionbar.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView />
<TextView />
</LinearLayout>
The back arrow should remain in place, in imageview add your icon and in text view your text.

Android - Padding/Margin Exists but is not set

There is a left horizontal padding that exists in my app that i did not set and can't find out how to remove it, for the image i added a sample image to illustrate the issue. This is what is happening:
And here is the layout for that piece:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="fill_horizontal" >
<ImageView
android:id="#+id/actionBarLogo"
android:layout_width="match_parent"
android:layout_height="20dp"
android:layout_gravity="center"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_centerInParent="true"
android:scaleType="fitXY"
android:src="#drawable/th2" />
</RelativeLayout>
It also happened on my main activity but only by about 5dp which was fine because i added 5dp on the right. But for my action bar It seems like 15dp or so and thats too much off each side. Where would it be coming from?
EDIT
This is how i initialize the actionable
android.support.v7.app.ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayUseLogoEnabled(false);
actionBar.setDisplayHomeAsUpEnabled(false);
LayoutInflater mInflater = LayoutInflater.from(this);
View myActionBar = mInflater.inflate(R.layout.menu_custom, null);
android.support.v7.app.ActionBar.LayoutParams layout = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayOptions(actionBar.DISPLAY_SHOW_CUSTOM);
actionBar.setCustomView(myActionBar, layout);
actionBar.setDisplayShowCustomEnabled(true);
EDIT
When I try to add padding/margin to the right, the left padding/margin grows
EDIT
I'm not sure the difference between margin and padding but I think it could be margin instead if that makes a difference
First of all, remove the android:orientation="vertical" (not used in a relative layout), android:layout_gravity="fill_horizontal" (not needed when you have match_parent width),
android:layout_gravity="center" (not used inside a relative layout), android:layout_centerHorizontal and android:layout_centerHorizontal ( android:layout_centerInParent="true" is enough).
Secondly, is this an activity layout? Do you use it directly in 'setContentView()'? Or is it inside a Fragment or inside a custom view? Any level in the view hierarchy could be causing this. You can use the layout editor in Android Studio to find what's causing it (click on the 'Design' tab when looking at the xml file, and then click on the empty padding to see where it belongs). If you can't figure it out from there, you can use the Hierarchy Viewer (http://developer.android.com/tools/debugging/debugging-ui.html) on an emulator and see what's wrong.

ActionBar with custom layout does not occupy full screen width on Android 4.4.2 (KitKat)

I want to display an Action Bar with a custom layout. The custom layout has three ImageViews, one in the centre and the other two on the left & right ends of the action bar. There must be no back button or action items.
Here is what I've used:
android.support.v7.app.ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayUseLogoEnabled(false);
actionBar.setDisplayShowHomeEnabled(false);
ActionBar.LayoutParams lp = new ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.MATCH_PARENT);
View actionBarView = LayoutInflater.from(this).inflate(resource, null);
actionBar.setCustomView(actionBarView, lp);
The custom layout is inflated correctly, but does not occupy the full width of the screen. This happens on Android 4.4.2 (KitKat). On Gingerbread, the above arrangement works properly.
I've tried the recommended solutions in the following discussions:
ActionBar - custom view with centered ImageView, Action Items on sides
RelativeLayout/customview doesn't fill tab's width
However the problem remains. There has to be a way to force the layout to occupy the complete screen width.
I am using the Action Bar from the V7 AppCompat library. Does anyone know a way around this ??
EDIT 1:
Here is the XML layout file:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="fill_horizontal"
android:orientation="horizontal">
<ImageView
android:id="#+id/left_icon"
android:background="#drawable/leftbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:adjustViewBounds="true"
android:scaleType="centerInside"
android:clickable="true"
android:onClick="leftButtonPressed"
android:hapticFeedbackEnabled="true"
/>
<ImageView
android:id="#+id/center_icon"
android:background="#drawable/centertitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:adjustViewBounds="true"
android:scaleType="centerInside"
/>
<ImageView
android:id="#+id/right_icon"
android:background="#drawable/rightbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:adjustViewBounds="true"
android:scaleType="centerInside"
android:clickable="true"
android:onClick="rightButtonPressed"
android:hapticFeedbackEnabled="true"
/>
</RelativeLayout>
Here is what it looks like on Gingerbread (2.3.5):
As you can see, its inflating correctly. And here's how it looks on Kitkat (4.4.2):
You can see that there's a slight gap on the right and the layout is not occupying the complete width.
I dont believe there's a problem with the layout (its inflating correctly on Android 2.3.5), but I could be wrong. Tell me if I'm crazy.
EDIT 2:
Thanks to Doctoror Drive and this post, I was able to get the result I wanted. I know its a bit strange: why use an Action Bar if you are not going to have any navigation or action items, but this is what I need for my particular problem. I have a TabHost with a stack of sliding Fragments in each tab, and the top LinearLayout (i.e. the Action Bar) needs to change its appearance when different Fragments are displayed.
EDIT 3:
Here are some more links for understanding this further:
https://groups.google.com/forum/#!msg/actionbarsherlock/i8JRUkBJjqk/ZzQV9xaM-lkJ
https://groups.google.com/forum/#!topic/android-developers/FGEi72thLzE
In my case I have solved the issue bypassing the invocation of the LayoutInflater.
So one should simply replace this code:
View actionBarView = LayoutInflater.from(this).inflate(R.layout.custom_actionbar, null);
actionBar.setCustomView(actionBarView, lp);
with this code:
actionBar.setCustomView(R.layout.custom_actionbar);
It's not a gap, it's a menu overflow button to the right. If you look closely you can see three dots button.
You've chosen the wrong theme because you can hardly see it.
Use Theme.Compat.Light.DarkActionBar if your ActionBar is dark and you need the light theme.
On devices that don't have physical menu button (mostly new ones) the menu overflow button is shown.
You can't remove it unless you disable menus.
You can use splitActionBarWhenNarrow uiOptions which will split the ActionBar so that the menu part will be at the bottom one, but that will be only for vertical orientation of medium screens and probably not what you need.
Take a look at the ActionBar Patterns doc and Menus UI doc

Positioning menu items to the left of the ActionBar in Honeycomb

I want to position some menu items to the left of Honycomb's ActionBar, but have found no documentation showing how to do this. It looks like it should be possible since the Contacts app has search to the immediate left of the navigation bar. Any idea as to how to set menu items to the left side?
The ActionBar has support for a custom layout between the application icon and the regular ActionBar icons. Example:
// Set the custom section of the ActionBar with Browse and Search.
ActionBar actionBar = getActionBar();
mActionBarView = getLayoutInflater().inflate(R.layout.action_bar_custom, null);
actionBar.setCustomView(mActionBarView);
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
here is what works for me like a dream: in the Activity I have this:
//hiding default app icon
ActionBar actionBar = getActionBar();
actionBar.setDisplayShowHomeEnabled(false);
//displaying custom ActionBar
View mActionBarView = getLayoutInflater().inflate(R.layout.my_action_bar, null);
actionBar.setCustomView(mActionBarView);
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM)
my_action_bar.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/turquoise">
<ImageButton
android:id="#+id/btn_slide"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="#null"
android:scaleType="centerInside"
android:src="#drawable/btn_slide"
android:paddingRight="50dp"
android:onClick="toggleMenu"
android:paddingTop="4dp"/>
</RelativeLayout>

Categories

Resources