Java – Android Material design error

Android Material design error… here is a solution to the problem.

Android Material design error

Hello, I am learning to create a Material design using this tutorial. https://www.youtube.com/watch?v=pMO8EVkhJO8&list=PLonJJ3BVjZW6CtAMbJz1XD8ELUs1KXaTD&index=3

In my design, all sides of the appbar have padding. Can anyone help me remove this padding and make the app look like a real app bar.

This is my screen
enter image description here

app_bar

    <?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="wrap_content"
    android:background="#DDDD">

</android.support.v7.widget.Toolbar>

Primary activity

<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=". MainActivity">

<include android:id="@+id/app_bar" layout="@layout/app_bar"/>

<TextView
        android:layout_below="@+id/app_bar"
        android:text="@string/hello_world" android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>

ma​​niActivity.java

private Toolbar toolbar;

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

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

Solution

In your relative layout, you placed padding. You need to delete them.

<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"  
 tools:context=". MainActivity">

<include android:id="@+id/app_bar" layout="@layout/app_bar"/>

<TextView
        android:layout_below="@+id/app_bar"
        android:text="@string/hello_world"  
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>

Check this. There will be no padding on the toolbar right now. If you want to populate other children, make a parent layout using LinearLayout and vertical orientation, and add two toolbars and relativelayouts to it.

Related Problems and Solutions