Remove left padding on custom action bar - android

First I know this is a duplicate question, but none of the answers solved my issue.
Here is my current code:
final ActionBar actionBar = getSupportActionBar();
actionBar.setCustomView(R.layout.action_bar);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayUseLogoEnabled(false);
actionBar.setDisplayShowHomeEnabled(false);
XML:
<?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="fill_horizontal"
android:layout_gravity="fill_horizontal"
android:padding="0dp"
android:layout_margin="0dp"
android:background="#color/article_background">
<ImageButton android:src="#drawable/filter"
android:layout_width="36dp"
android:layout_height="36dp"
android:background="#00000000"
android:scaleType="centerInside"
android:id="#+id/filters"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"/>
<ImageView
android:src="#drawable/tc_logo"
android:layout_width="36dp"
android:layout_height="36dp"
android:scaleType="centerInside"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:id="#+id/logo" />
<ImageButton android:src="#drawable/menu_button"
android:background="#00000000"
android:layout_width="36dp"
android:layout_height="36dp"
android:scaleType="centerInside"
android:id="#+id/options"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"/>
</LinearLayout>
And there is a left padding showing.
I tried the following suggestions from Stack Overflow:
I tried setting a custom actionbar style in themes with zero padding.
I tried with relative and linear layout as the custom layout.
I tried setting custom actionbar icon in themes and enabling onHomeUp to display a one px image..
I tried layout gravity and gravity to fill_horizontal but didn't work.
No matter what I do, it still shows up.

I had the same issue, and nothing helped, until I found this SO question.
TL;DR
Have the Actionbar to display its up arrow, by calling setDisplayHomeAsUpEnabled(true).
Create a style based on your apps theme in "values-v14" (these will
apply to SDK Version 14 and above).
have this item in the new style: <item name="android:homeAsUpIndicator">#null</item>. This will cause the up to disappear, but the padding the icon had will be gone.

Related

Custom Action bar layout not getting proper width

I am trying to use a custom layout for action bar with navigation drawer but it seems like the width is not consistent.
Here's how it looks like right now:
ListView http://imageshack.com/a/img673/867/fTvd6X.png
For some reason the width is not occupied completely on the left of the action bar.
The items currently used in the custom action bar are : app name(Text View), Spinner(Some Text), ImageView on the right of Spinner.
This is how my layout xm looks like:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#000" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<ImageView
android:layout_width="35dp"
android:layout_height="35dp"
android:padding="10dp"
android:layout_gravity="center_vertical|left"
android:src="#drawable/someIcon" />
<TextView
android:id="#+id/appTextLabel"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:padding="10dp"
android:text="someAppName"
android:textColor="#555555"
android:textSize="24sp"
android:textStyle="bold" />
<Spinner
android:id="#+id/locationSpinner"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#000"
android:spinnerMode="dropdown" />
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="center_vertical|right"
android:src="#drawable/location_down_icon" />
</LinearLayout>
And sample code for Action bar :
mActionBar = getSupportActionBar();
mActionBar.setDisplayShowTitleEnabled(false);
mActionBar.setDisplayShowCustomEnabled(true);
mActionBar.setCustomView(actionBarLayout);
No idea whats going wrong with this.
Any help will be really appreciated. Thanks .. :)
Is this a space for icon (or logo)?
you might try
getActionBar().setDisplayShowHomeEnabled(false);
but you have flags ActionBar.DISPLAY_SHOW_HOME and ActionBar.DISPLAY_HOME_AS_UP, maybe they are important for you...
also you might try set transparent icon
getActionBar().setIcon(new ColorDrawable(getResources().getColor(android.R.color.transparent)));

Android ActionBar custom layout styling

I'm just starting android development coming from IOS and again stubbled onto a problem.
And after 1 day of trying i decided that i will ask the people of stack overflow.
So my problem:
I have an app and in the action bar (default no sherlock) i want 1 logo and 2 lines of text.
And trough the internet i fount the setCustomView for the action bar.
And the custom xml is loaded but i can't get the layout right.
So first i setup the action bar:
private void setupActionBar() {
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayUseLogoEnabled(false);
actionBar.setDisplayHomeAsUpEnabled(false);
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); // layout which contains your button.
actionBar.setCustomView(customNav, lp1);
}
Than the xml:
<?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:layout_gravity="fill_horizontal"
android:orientation="horizontal"
android:background="#color/Blue">
<TextView
android:id="#+id/text_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/title_menu"
android:textColor="#color/White"
android:paddingLeft="5dp"
android:layout_gravity="left"/>
<ImageView
android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/Icon"
android:layout_gravity="center"/>
<TextView
android:id="#+id/text_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/title_help"
android:layout_gravity="right"
android:textColor="#color/White"
android:paddingRight="5dp"/>
</LinearLayout>
But the result is something what i'm not looking for:
So what am i forgetting/doing wrong/or should i do?
try change the root layout into relative layout
<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">
<TextView
android:text="left text"
android:layout_toLeftOf="#+id/icon"
android:layout_alignParentLeft="true"
android:id="#+id/text_left"
android:gravity="left"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="5dp"/>
<ImageView
android:layout_centerHorizontal="true"
android:id="#+id/icon"
android:layout_width="10dp"
android:layout_height="wrap_content"
android:src="#drawable/bg_sunrise"
android:layout_gravity="center"/>
<TextView
android:text="Right txt"
android:layout_toRightOf="#+id/icon"
android:id="#+id/text_right"
android:layout_width="match_parent"
android:gravity="right"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:paddingRight="5dp"/> </RelativeLayout>
you're not getting your result because:
u need to:
if you use AppCompat theme
add to your activity layout
in code eg. in onCreate() set a toolbar to replace the action bar.
You could also use LinearLayout as a root and add android:weightSum and specify a layout_weight on the three child views.
What is android:weightSum in android, and how does it work?

Strange black line under my custom Action Bar

I made my custom Action Bar, but there is an ugly underline in under it.
You can see the black line under my green actionBar.
my Action Bar xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="3dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="0dp"
android:layout_marginTop="4dp" >
<ImageButton
android:id="#+id/menuToggleBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:adjustViewBounds="false"
android:background="#null"
android:scaleType="centerInside"
android:src="#drawable/menu_btn" />
<ImageView
android:id="#+id/loadingIconIv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:src="#drawable/flexilogo" />
<ImageButton
android:id="#+id/ImageButton01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="false"
android:background="#null"
android:scaleType="centerInside"
android:src="#drawable/menu_btn"
android:visibility="invisible" />
</LinearLayout>
<View
android:id="#+id/view1"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#6ea541" />
And my code::
ActionBar actionBar = getActionBar();
actionBar.setCustomView(R.layout.action_bar);
menuToggleBtn = (ImageButton) actionBar.getCustomView().findViewById(
R.id.menuToggleBtn);
menuToggleBtn.setOnClickListener(this);
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
Why is that black line is there ? I would like to get rid of it.
Please help if you can.
E D I T:
I tried to add:
<item name="android:windowContentOverlay">#null</item>.
to my app's style, but it does not work :(
android:layout_height="match_parent" on the LinearLayout tag
Should fix it
It can be a shadow... take a look at Remove shadow below actionbar
To remove it you have to make a style and use <item name="android:windowContentOverlay">#null</item>.
Use the hierarchyviewer perspective in Eclipse ADT and load your activity into it.
Then select different nodes to show their graphical representation.
Try to find the node that includes the black line.
You can also click the approximated spot on the layout-preview in the lower right to select a node that includes this spot.
Then examine the Layout- and Measurement-properties for the selected nodes and neighbouring (sibling) nodes and their graphics preview by right-clicking the node in the tree-view and pressing the box-icon above the small preview.
If you have found the node containing the black line, look at its layout-properties and fix them
If there isn't a node containg the black line for itself, it would likely be a gap in your layout, with the default-background shining through. Then you would have to presumably fix the layout-properties (layout_height) of your main contentview (the one containing the TextView saying "segedanyaok").
Hint:
if your app's activity isn't shown in bold in the Windows-View of the hierarchy-perspective, you need to include the ViewServer-component in your app's activity to enable introspection with hierarchyviewer: https://github.com/romainguy/ViewServer
If the hierarchyviewer-connection gets in trouble (maybe due to timeouts) and is non-responding giving errors, reset the adb-connection to your device from the ddms-perspective, Devices-view, menu-item "Reset adb".
Another hint:
You don't have to use a custom-view to show your logo in such a style. To have a drawer-appliance with a logo, look at how to include Google's navigation drawer http://developer.android.com/design/patterns/navigation-drawer.html and how to include your logo as the actionbar-up/drawer-icon http://developer.android.com/guide/topics/ui/actionbar.html#Logo.

Title not showing on the mainActivity

I have put up a spinner on Android Action Bar, i have placed the spinner in test.xml layout and i am calling it like this in the main activity in the OnCreate() method
ActionBar actionBar = getActionBar();
actionBar.setBackgroundDrawable(new ColorDrawable(Color.GREEN));
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setCustomView(R.layout.activity_test);
setContentView(R.layout.activity_main);
actionBar.setTitle("Home");
But my title is not showing. i want to show HOME as title on that action bar what should i do?
My activity_test
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Spinner
android:id="#+id/planets_spinner"
android:layout_width="100dp"
android:layout_height="50sp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="#drawable/arrow2"
android:layout_weight="0.08"
android:drawSelectorOnTop="true" />
</RelativeLayout>
I have solved the problem by adding a textView to my activity_test, as this whole activity is showing on the ActionBar so i have added a text view and problem is solved :)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<Spinner
android:id="#+id/planets_spinner"
android:layout_width="100dp"
android:layout_height="50sp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="#drawable/arrow2"
android:drawSelectorOnTop="true" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/planets_spinner"
android:layout_alignParentLeft="true"
android:layout_marginBottom="6dp"
android:layout_marginLeft="6dp"
android:textStyle="bold"
android:textSize="20sp"
android:textColor="#ffffff"
android:text="Scene" />
</RelativeLayout>
When you set android:layout_alignParentRight="true" , RelativeLayout uses all the space including the title space and title is not displayed. You can check the behaviour by drawing a border around the RelativeLayout. But RelativeLayout doesnt consume more space if you dont use android:layout_alignParentRight="true". It doesnt consume more space if you use android:layout_alignParentLeft="true", too. It works weird for only android:layout_alignParentRight="true". In order to show a view at the right of the parent, the following worked for me:
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/profile_image"
android:layout_width="40dp"
android:src="#drawable/ic_launcher"
android:layout_height="40dp"
android:background="?android:selectableItemBackground"
android:padding="3dp"
android:scaleType="centerCrop" />
Do not use RelativeLayout. Just the view you need.
Then you can align right with the LayoutParams shown as following:
actionBar.setCustomView(R.layout.actionbar_layout);
actionBar.setDisplayShowCustomEnabled(true);
ImageView imageProfile = (ImageView)actionBar.getCustomView();
int length = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 40, getResources().getDisplayMetrics());
Toolbar.LayoutParams layoutParams = new Toolbar.LayoutParams(
length,
length, Gravity.RIGHT
| Gravity.CENTER_VERTICAL);
imageProfile.setLayoutParams(layoutParams);
Now you should be able to view both ActionBar title and the ImageView.

Add custom title bar using ActionBar

I want to add a custom title bar in a child of FragmentActivity, I have a layout for title bar and this is how I do it.
final ActionBar actionBar = getActionBar();
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
actionBar.setCustomView(R.layout.title_bar);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.addTab(actionBar.newTab().setText("Tab0").setTabListener(this));
actionBar.addTab(actionBar.newTab().setText("Tab1").setTabListener(this));
actionBar.addTab(actionBar.newTab().setText("Tab2").setTabListener(this));
but the problem is title bar is showing below the tabs. I want the title bar above the navigation tabs.
this is my title_bar.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/titleBar"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/black" >
<ImageView
android:id="#+id/titleBarMenuBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/menu_btn" />
<TextView
android:id="#+id/titleBarLocation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/titleBarMenuBtn"
android:text="Location"
android:textColor="#color/white"
android:textSize="16dip" />
<TextView
android:id="#+id/titleBarUpdateDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/titleBarMenuBtn"
android:layout_alignLeft="#+id/titleBarLocation"
android:layout_alignParentRight="true"
android:text="Updated: 30/Jul/2013"
android:textColor="#color/white" />
</RelativeLayout>
I saw this post but my tabs are showing above title bar.
Thanks
Showing the tabs above your custom layout is the intended default behaviour. Though there are ways to override this. I found to post with possible solutions: #1 and #2
Further more the sherlock actionbar seems to behave the same way.

Categories

Resources