Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make toolbar on Android takes a full width?

Here is the result: http://i.stack.imgur.com/VgbZT.png. I want it to be right below status bar and full width, just like this one: http://i.stack.imgur.com/FFQJN.png.

<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" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MyActivity">

    <!--<android.support.v7.widget.Toolbar-->
        <!--android:id="@+id/toolbar"-->
        <!--android:layout_height="wrap_content"-->
        <!--android:layout_width="match_parent"-->
        <!--android:minHeight="?attr/actionBarSize"-->
        <!--android:background="?attr/colorPrimary" />-->

    <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:contentInsetEnd="0dp"
        app:contentInsetStart="0dp"
        android:minHeight="?attr/actionBarSize"
        android:background="?attr/colorPrimary" >
    </android.support.v7.widget.Toolbar>

</RelativeLayout>

public class MyActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_card_list);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
    }

I already read this one, but it still didn't work.

like image 855
stackex Avatar asked Mar 23 '15 02:03

stackex


1 Answers

Remove the padding on the root view.

Change:

<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" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MyActivity">

    <!--<android.support.v7.widget.Toolbar-->
        <!--android:id="@+id/toolbar"-->
        <!--android:layout_height="wrap_content"-->
        <!--android:layout_width="match_parent"-->
        <!--android:minHeight="?attr/actionBarSize"-->
        <!--android:background="?attr/colorPrimary" />-->

    <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:contentInsetEnd="0dp"
        app:contentInsetStart="0dp"
        android:minHeight="?attr/actionBarSize"
        android:background="?attr/colorPrimary" >
    </android.support.v7.widget.Toolbar>

</RelativeLayout>

To:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
    android:layout_height="match_parent" 
    tools:context=".MyActivity">

    <!--<android.support.v7.widget.Toolbar-->
        <!--android:id="@+id/toolbar"-->
        <!--android:layout_height="wrap_content"-->
        <!--android:layout_width="match_parent"-->
        <!--android:minHeight="?attr/actionBarSize"-->
        <!--android:background="?attr/colorPrimary" />-->

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

</RelativeLayout>
like image 153
Jared Burrows Avatar answered Sep 28 '22 01:09

Jared Burrows