1

I'm new to Android Studio and it is creating a lot of problem to me while setting my toolbar in and activity,the problem is whenever i change it layout width and height to the following

layout_width=match_parent
layout_height=attr/actionBarsize

it is getting back to change to the following

layout_width=368dp
layout_height=56dp

No matter how many times it tried to change it is giving me the same result again and again.

Atul kumar
  • 97
  • 3
  • 13

3 Answers3

1

Yeah this was really annoying me too. I presume you are trying to put the toolbar within a ConstraintLayout.

ConstraintLayout doesn't support match_parent and it expects every view to be constrained. When you click in the design mode of the Android Studio, the editor will kick in and try to 'fix' any issues with your xml. In this case it finds your toolbar is not constrained and is incorrectly using match_parent and so tries to fix this for you (and you should see a warning that the toolbar is not constrained).

I think you have 2 options:

1 Constrain the toolbar in your layout and replace 'match_parent' with '0dp'

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="0dp"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    android:elevation="4dp"
    android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    android:layout_marginLeft="0dp"
    app:layout_constraintLeft_toLeftOf="parent"
    android:layout_marginRight="0dp"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:layout_marginTop="0dp" />

2 Put the toolbar within a co-ordinator layout. You used to have to do this for scrolling functionality with the toolbar, but with the new ConstraintLayout I'm not sure if this still applies

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

</android.support.design.widget.CoordinatorLayout>
sam9046
  • 566
  • 1
  • 7
  • 14
0

XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay"
    android:id="@+id/coach_bar">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="#F4B702"
        app:popupTheme="@style/AppTheme.PopupOverlay" >

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

</android.support.design.widget.AppBarLayout>

 <!--<Your Design>-->

</RelativeLayout >

Java

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setLogo(R.drawable.my_icon);
setSupportActionBar(toolbar);
if(getSupportActionBar()!=null)
  getSupportActionBar().setDisplayHomeAsUpEnabled(true);

Manifest.xml

<activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:screenOrientation="portrait"
        android:theme="@style/AppTheme.NoActionBar"/>
Raj
  • 477
  • 5
  • 20
0

I had this problem and finally I found that I didn't saved style.xml after it changed.

Vishal Chhodwani
  • 2,567
  • 5
  • 27
  • 40