Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to create custom toolbar and use the same in different activity in android.

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 218
    Comment on it

    Here we will learn how to create custom toolbar and use it in different activity using <include> tag in xml file of the activity.

    1) We will design the layout for the toolbar in our project's layout directory, I have named it as mtoolbar.xml.

    mtoolbar.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#ff9999"
            android:minHeight="?attr/actionBarSize">
    
            <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <ImageView
            android:layout_alignParentRight="true"
            android:id="@+id/refresh"
            android:layout_marginTop="3dp"
            android:layout_width="25dp"
            android:layout_height="27dp"
            android:background="@drawable/refresh"/>
        <ImageView
            android:layout_toLeftOf="@+id/refresh"
            android:id="@+id/search"
            android:layout_marginTop="5dp"
            android:layout_width="25dp"
            android:layout_height="23dp"
            android:background="@drawable/srch"/>
    
    
    </RelativeLayout>
    
    
    
        </android.support.v7.widget.Toolbar>
    
    
    </LinearLayout>

    2)Now make use of the above toolbar in your activity using <include> tag in your activity xml file.

    activity_main.xml

    <LinearLayout
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:android="http://schemas.android.com/apk/res/android"
        tools:context=".MainActivity"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        
        <include layout="@layout/mtoolbar"/>
        
        
    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <FrameLayout
            android:id="@+id/main_fragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    
        </FrameLayout>
    
        <ListView
            android:id="@+id/list_slidermenu"
            android:layout_width="240dp"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:choiceMode="singleChoice"
            android:divider="@color/list_devider"
            android:dividerHeight="1dp"
            />
    </android.support.v4.widget.DrawerLayout>
    </LinearLayout>

    3) Finally add the toolbar in your MainActivity.java file as shown below in the code.

    MainActivity.java

    public class MainActivity extends AppCompatActivity
    {
       
         private Toolbar toptoolbar;
        
         @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
        
             toptoolbar=(Toolbar)findViewById(R.id.toolbar);
    
            setSupportActionBar(toptoolbar);
    
    
        }
    }

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: