Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - How can I set an item in a Toolbar to fill all of the available space?

I've tried assigning weights of course, but it appears that isn't supported by Toolbar.

like image 482
Noah Andrews Avatar asked Mar 11 '15 17:03

Noah Andrews


2 Answers

You can use the following to get rid of left margin in the tool bar

app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"

So finally your code can be like this-

<android.support.v7.widget.Toolbar
        app:contentInsetLeft="0dp"
        app:contentInsetStart="0dp"
        android:layout_width="match_parent"
        android:layout_height="48dp">

        <TextView
            android:id="@+id/progress"
            android:layout_width="match_parent"
            android:layout_height="48dp"
            android:background="@color/background_floating_material_dark" />
    </android.support.v7.widget.Toolbar>

Edit You can do in the code also as-

toolbar.setContentInsetsAbsolute(0,0);

Even you can modify it in style too-

<item name="toolbarStyle">@style/Widget.Toolbar</item>

<style name="Widget.Toolbar" parent="@style/Widget.AppCompat.Toolbar">
<item name="contentInsetStart">0dp</item>
</style>
like image 112
Sanjeet A Avatar answered Oct 19 '22 14:10

Sanjeet A


You can put a LinearLayout inside your Toolbar. The LinearLayout allows you to set an orientation and, once you've done that, you can use layout_weight.

So it would be like:

<android.support.v7.widget.Toolbar
        app:contentInsetLeft="0dp"
        app:contentInsetStart="0dp"
        android:layout_width="match_parent"
        android:layout_height="48dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">
            <TextView
                android:id="@+id/progress"
                android:layout_width="match_parent"
                android:layout_height="48dp"
                android:background="@color/background_floating_material_dark"
                android:layout_weight="1" />
        </LinearLayout>
    </android.support.v7.widget.Toolbar>
like image 23
Chelsea Troy Avatar answered Oct 19 '22 12:10

Chelsea Troy