Creating a button in Android Toolbar -


how can create button inside android's toolbar looks ios example?

ios example

toolbar button tutorial

1 - add library compatibility inside build.gradle

dependencies {     compile filetree(dir: 'libs', include: ['*.jar'])     compile 'com.android.support:appcompat-v7:21.0.3' } 

2 - create file name color.xml define toolbar colors

<?xml version="1.0" encoding="utf-8"?> <resources>     <color name="colorprimary">#ff5722</color>     <color name="colorprimarydark">#e64a19</color> </resources> 

3 - modify style.xml file

<resources>      <!-- base application theme. -->     <style name="apptheme" parent="theme.appcompat.light.noactionbar">          <item name="colorprimary">@color/colorprimary</item>         <item name="colorprimarydark">@color/colorprimarydark</item>         <!-- customize theme here. -->     </style>  </resources> 

4 - create xml file tool_bar.xml

<?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="@color/colorprimary"     android:elevation="4dp"     > </android.support.v7.widget.toolbar> 

5 - include toolbar main_activity.xml

<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/tool_bar"         layout="@layout/tool_bar"         ></include>      <textview         android:layout_below="@+id/tool_bar"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_margintop="@dimen/textdimtop"         android:text="@string/hello_world" />  </relativelayout> 

6 - then, put inside mainactivity class

package com.example.hp1.materialtoolbar;  import android.support.v4.widget.drawerlayout; import android.support.v7.app.actionbaractivity; import android.os.bundle; import android.support.v7.app.actionbardrawertoggle; import android.support.v7.widget.linearlayoutmanager; import android.support.v7.widget.recyclerview; import android.support.v7.widget.toolbar; import android.view.menu; import android.view.menuitem; import android.view.motionevent; import android.view.view; import android.widget.toast;   public class mainactivity extends actionbaractivity { /* when using appcombat support library                                                          need extend main activity                                                          actionbaractivity.                                                       */       private toolbar toolbar;                              // declaring toolbar object       @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.main_activity);          toolbar = (toolbar) findviewbyid(r.id.tool_bar); // attaching layout toolbar object         setsupportactionbar(toolbar);                   // setting toolbar actionbar setsupportactionbar() call      }       @override     public boolean oncreateoptionsmenu(menu menu) {         // inflate menu; adds items action bar if present.         getmenuinflater().inflate(r.menu.menu_main, menu);         return true;     }      @override     public boolean onoptionsitemselected(menuitem item) {         // handle action bar item clicks here. action bar         // automatically handle clicks on home/up button, long         // specify parent activity in androidmanifest.xml.         int id = item.getitemid();          //noinspection simplifiableifstatement         if (id == r.id.action_settings) {             return true;         }          return super.onoptionsitemselected(item);     } } 

7 - , finally, add "button items" menu_main.xml inside of /res/menu/ directory

<?xml version="1.0" encoding="utf-8"?>    <menu         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"         tools:context=".mainactivity">         <item             android:id="@+id/action_settings"             android:orderincategory="100"             android:title="@string/action_settings"             app:showasaction="never" />         <item             android:id="@+id/action_search"             android:orderincategory="200"             android:title="search"             android:icon="@drawable/ic_search"             app:showasaction="ifroom"             ></item>         <item             android:id="@+id/action_user"             android:orderincategory="300"             android:title="user"             android:icon="@drawable/ic_user"             app:showasaction="ifroom"></item>      </menu> 

Comments

Popular posts from this blog

c# - Better 64-bit byte array hash -

webrtc - Which ICE candidate am I using and why? -

php - Zend Framework / Skeleton-Application / Composer install issue -