Android: Custom action bar,How to use entire width?
Asked Answered
G

5

12

Here is an image of my custom action bar:

enter image description here

I want to use the entire width, I have search the internet for a solution but nothing wotrked for me.

Here is my action bar .xml:

<?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">

    <ImageView
        android:layout_width="50dp"
        android:layout_height="match_parent"
        android:id="@+id/invActionBarSave"
        android:src="@drawable/ic_ok_b"
        android:nestedScrollingEnabled="true"
        android:layout_alignParentRight="true"
        android:background="#ffb0afaf"
        android:paddingLeft="8dp"
        android:paddingRight="8dp"
        android:layout_marginTop="4dp"
        android:layout_marginBottom="4dp" />

    <ImageView
        android:layout_width="50dp"
        android:layout_height="match_parent"
        android:id="@+id/invActionBarDelete"
        android:src="@drawable/ic_trash"
        android:nestedScrollingEnabled="true"
        android:layout_toLeftOf="@+id/invActionBarSave"
        android:layout_marginRight="5dp"
        android:background="#ffb0afaf"
        android:paddingLeft="8dp"
        android:paddingRight="8dp"
        android:layout_marginTop="4dp"
        android:layout_marginBottom="4dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:singleLine="true"
        android:text="אלסאדק אחמד אבו האני"
        android:textSize="18dp"
        android:textColor="#ff000000"
        android:id="@+id/invActionBarTitle"
        android:gravity="center|right"
        android:layout_toLeftOf="@+id/invActionBarDelete"
        android:layout_toRightOf="@+id/invActionBarHome"
        android:layout_marginRight="20dp"
        android:layout_marginLeft="10dp" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:id="@+id/invActionBarHome"
        android:src="@drawable/ic_back_b"
        android:nestedScrollingEnabled="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="false"
        android:paddingLeft="10dp" />

    <ProgressBar
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:indeterminateOnly="false"
        style="@style/ProgressBar.Horizontal"
        android:indeterminate="true"
        android:id="@+id/invActionBarProgressBar"
        android:visibility="gone"
        android:layout_below="@+id/invActionBarDelete"
        android:paddingLeft="10dp" />

</RelativeLayout>

And here is my java code:

getSupportActionBar().setDisplayShowHomeEnabled(false);
    getSupportActionBar().setDisplayShowTitleEnabled(false);
    LayoutInflater mInflater = LayoutInflater.from(this);
    //View mCustomView = mInflater.inflate(R.layout.actionbar_invoice, null);
    getSupportActionBar().setCustomView(R.layout.actionbar_invoice);
    getSupportActionBar().setDisplayShowCustomEnabled(true);
    setContentView(R.layout.cstmrinvoice);
    getWindow().
            getDecorView().
            setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);

[EDIT]

Here is a simple one:

enter image description here

And here is the .xml:

<?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:contentInsetEnd="0dp"
    android:contentInsetStart="0dp">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="Small Text"
        android:id="@+id/textView6"
        android:background="#ffff0000"
        android:textColor="#ffffffff" />
</RelativeLayout>

The same result, there still a space at right of the action bar !

Thank you

Granny answered 15/3, 2015 at 6:29 Comment(0)
K
26

Read this link1, link2 and set the contentInsets to 0dp like done below:

View mCustomView = mInflater.inflate(R.layout.actionbar_invoice, null);
getSupportActionBar().setCustomView(mCustomView);
Toolbar toolbar=(Toolbar)mCustomView.getParent();
toolbar.setContentInsetsAbsolute(0,0);

If you are using custom Toolbar instead of ActionBar then use below code:

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:minHeight="@dimen/action_bar_height"
    android:background="?attr/colorPrimary"
    android:contentInsetStart="0dp"//see this
    android:contentInsetLeft="0dp"//see this
    app:contentInsetLeft="0dp"
    app:contentInsetStart="0dp"
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light">

I Hope this might help you.

Kimon answered 15/3, 2015 at 7:50 Comment(4)
BOOOOOM, thaaaaank you very mutch, it works !! :-) I really appreciate your help, thank you againGranny
where should I put this toolbar so it put shadows on my Items?!Marcelina
Hey @Amrut Bidri ! I encountered the same problem on Android M and seems that workaroung with "setContentInsetsAbsolute(0, 0)" works only with Lolipop, but it's now working for Android M :( Do you know something about it?Coextensive
Addition to the answer: I'm using ActionBar (not support action bar) and it worked for me just with adding this code: Toolbar parent =(Toolbar)mCustomAB.getParent(); parent.setContentInsetsAbsolute(0,0);Eads
W
8

Follow few simple steps. 1. Make your own custom Action view action_view.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:orientation="horizontal" 
android:weightSum="3">

<TextView 
    android:id="@+id/bar_title1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:textColor="@color/White"
    android:text="title1"/>
<TextView 
    android:id="@+id/bar_title2"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:textColor="@color/White"
    android:text="title2"/>
<TextView 
    android:id="@+id/bar_title3"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:textColor="@color/White"
    android:text="title3"/>
    </LinearLayout>

Here is the code:

 final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.custom_action_bar, null);
actionBar.setCustomView(view, new ActionBar.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);

This will surely solve your purpose. Happy Coding !!!

Wiley answered 15/3, 2015 at 8:31 Comment(2)
You nailed it. Thanks :) actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); This code snippet did the trickAnselmi
I've wasted couple of hours just to try this one ... actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM). Thanks man!Nash
N
4
 ActionBar action = getSupportActionBar();
        action.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);

 Toolbar toolbar=(Toolbar)action.getCustomView().getParent();
        toolbar.setContentInsetsAbsolute(0, 0);
        toolbar.getContentInsetEnd();
        toolbar.setPadding(0, 0, 0, 0);
Norword answered 13/6, 2016 at 7:23 Comment(1)
toolbar.setPadding(0, 0, 0, 0); worked for me. thanksPolder
H
0

I solved this by using toolbar and adding the following properties to this.

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content" >

<View
    android:id="@+id/toolbar_blurrbg"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@drawable/toolbar_bg"
    android:visibility="gone" />

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@drawable/actionbar_bg"
    app:contentInsetEnd="0dp"
    app:contentInsetStart="0dp" />

app:contentInsetEnd and app:contentInsetStart ..

See if it helps you

Headrace answered 15/3, 2015 at 6:36 Comment(0)
E
0

I just wanted to tell everybody that IN SOME WAY having this code below does not work. A space between the edge of the screen and the Toolbar remains in my case.

app:contentInsetEnd="0dp"
app:contentInsetStart="0dp"

I spent ~2hours doing some manipulations with styles and this two attributes. Nothing helped me. All of a sudden I thought whether this "space" may be just a padding or margin of the Toolbar. But my Toolbar hadn't had any padding or margin. And I added 0dp padding to the Toolbar in my layout .xml file.

android:padding="0dp"

It worked. You can imagine my face. This space has gone away. Android lied to me.

Eighteen answered 6/11, 2018 at 16:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.