android - How to get a custom toolbar aligned with the action bar? -


in order action-buttons toolbar, have implemented custom-toolbar in layout button images can defined. in activity_main layout file, refer custom-toolbar layout via include instruction.

it works extend in 1 line @ top of action bar both contents appear: program title , overflow menu icon plus button custom-toolbar. problem below line second empty row appears no content. means, height of action bar doubled , unwanted area overlapping canvas. here screenshot of phenomena:

on device

i have searched documents addressing did not find mapping case. furthermore have tried kind of constellations in layout file (e.g. in respect postion of include instruction) did not achieve change. tried different sizes of image file without change. current size 3k 80x49 pixel.

here code reduced show case relevant lines:

import android.os.bundle; import android.support.v7.app.appcompatactivity; import android.support.v7.widget.toolbar; import android.view.menu; import android.view.view;  public class mainactivity extends appcompatactivity {      private toolbar toolbar;      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);         toolbar toolbar = (toolbar) findviewbyid(r.id.toolbar);         setsupportactionbar(toolbar);      }      @override     public boolean oncreateoptionsmenu(menu menu) {         getmenuinflater().inflate(r.menu.menu_main, menu);         return true;     }      public void on_distance_waiting_click(view view) {      }  } 

the main layout:

    <android.support.design.widget.coordinatorlayout     xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:app="http://schemas.android.com/apk/res-auto"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"      tools:context="com.pm.pmactionbaricon.mainactivity">      <android.support.design.widget.appbarlayout         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:theme="@style/apptheme.appbaroverlay"         android:orientation="vertical">          <include layout="@layout/custom_toolbar" />         <android.support.v7.widget.toolbar              android:id="@+id/toolbar"             android:layout_width="match_parent"             android:layout_height="?attr/actionbarsize"             android:background="?attr/colorprimary"             app:popuptheme="@style/apptheme.popupoverlay"  />      </android.support.design.widget.appbarlayout> </android.support.design.widget.coordinatorlayout> 

the custom-toolbar layout:

    <android.support.v7.widget.toolbar xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:app="http://schemas.android.com/apk/res-auto"     app:layout_collapsemode="pin"     android:id="@+id/toolbar"     android:layout_height="?attr/actionbarsize"     android:padding="0dp"     android:layout_width="match_parent"     android:background="@android:color/transparent"     android:fitssystemwindows="true"     android:orientation="horizontal">      <relativelayout         android:layout_width="310dp"         android:layout_height="wrap_content"        >               <button                 android:id="@+id/button1"                 android:background="@drawable/distance_waiting_small"                 android:layout_width="100dp"                 android:layout_height="wrap_content"                 style="?android:attr/buttonbarbuttonstyle"                 android:title="@string/distance"                 android:layout_alignparenttop="true"                 android:layout_alignparentleft="true"                 android:layout_alignparentstart="true"                 android:onclick="on_distance_waiting_click"/>       </relativelayout> </android.support.v7.widget.toolbar> 

the style file:

    <resources>      <!-- base application theme. -->     <style name="apptheme" parent="theme.appcompat.light.darkactionbar">         <!-- customize theme here. -->         <item name="colorprimary">@color/colorprimary</item>         <item name="colorprimarydark">@color/colorprimarydark</item>         <item name="coloraccent">@color/coloraccent</item>     </style>      <style name="apptheme.noactionbar">         <item name="windowactionbar">false</item>         <item name="windownotitle">true</item>     </style>      <style name="apptheme.appbaroverlay" parent="themeoverlay.appcompat.dark.actionbar" />     <style name="apptheme.popupoverlay" parent="themeoverlay.appcompat.light" />       <style name="toolbartheme" parent="@style/themeoverlay.appcompat.dark.actionbar">         <item name="android:textcolorprimary">@android:color/holo_blue_light</item>         <item name="actionmenutextcolor">@android:color/holo_green_light</item>     </style>  </resources> 

here used image:enter image description here

when work layouts in design mode, can recognize regular toolbar element (not custom-toolbar) seems responsible unwanted area. following 3 screenshots might highlight this. strange appearance here different result on device shown @ beginning.

the activity layout (consider selected in right window): enter image description here

the activity layout (standard) toolbar selected: enter image description here

the custom toolbar: enter image description here

so, causing unwanted area below actionbar?

in activity_main.xml remove toolbar element:

 <android.support.design.widget.coordinatorlayout     xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:app="http://schemas.android.com/apk/res-auto"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"      tools:context="com.pm.pmactionbaricon.mainactivity">      <android.support.design.widget.appbarlayout         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:theme="@style/apptheme.appbaroverlay"         android:orientation="vertical">          <include layout="@layout/custom_toolbar" />     </android.support.design.widget.appbarlayout> </android.support.design.widget.coordinatorlayout> 

in custom_toolbar.xml layout make changes shown below:

<android.support.v7.widget.toolbar xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:app="http://schemas.android.com/apk/res-auto"     app:layout_collapsemode="pin"     android:id="@+id/toolbar"     android:layout_height="?attr/actionbarsize"     android:padding="0dp"     app:popuptheme="@style/apptheme.popupoverlay"      android:layout_width="match_parent"     android:background="?attr/colorprimary"     android:orientation="horizontal">      <relativelayout         android:layout_width="310dp"         android:layout_height="wrap_content">              <button                 android:id="@+id/button1"                 android:background="@drawable/distance_waiting_small"                 android:layout_width="100dp"                 android:layout_height="wrap_content"                 style="?android:attr/buttonbarbuttonstyle"                 android:title="@string/distance"                 android:layout_alignparenttop="true"                 android:layout_alignparentleft="true"                 android:layout_alignparentstart="true"                 android:onclick="on_distance_waiting_click"/>       </relativelayout> </android.support.v7.widget.toolbar> 

also, make sure activity tag has attribute set in manifest file.

android:theme="@style/apptheme.noactionbar" 

Comments

Popular posts from this blog

javascript - Thinglink image not visible until browser resize -

firebird - Error "invalid transaction handle (expecting explicit transaction start)" executing script from Delphi -

mongodb - How to keep track of users making Stripe Payments -