Wednesday, October 4, 2017

android - Drawer menu item action view

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:background="#ecebff"
        >
        <android.support.design.widget.AppBarLayout
            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="#62beff"
                >
            </android.support.v7.widget.Toolbar>
        </android.support.design.widget.AppBarLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:background="#ecebff"
            android:padding="16dp"
            >
            <!-- content goes here -->
        </LinearLayout>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_gravity="start"
        android:background="#f0f9fa"
        >
        <TextView
            android:id="@+id/drawer_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Sample Drawer Layout"
            android:background="#9c8c9c"
            android:textSize="20sp"
            android:padding="16dp"
            />
        <android.support.design.widget.NavigationView
            android:id="@+id/navigation_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:menu="@menu/navigation_view_menu"
            />
    </LinearLayout>
</android.support.v4.widget.DrawerLayout>
MainActivity.java

package com.cfsuman.me.androidcodesnippets;

import android.app.Activity;
import android.content.Context;
import android.support.design.widget.NavigationView;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageButton;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity {
    private Context mContext;
    private Activity mActivity;

    private Toolbar mToolbar;
    private DrawerLayout mDrawerLayout;
    private NavigationView mNavigationView;
    private Menu mNavigationMenu;
    private ActionBarDrawerToggle mDrawerToggle;

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

        // Get the application context
        mContext = getApplicationContext();
        mActivity = MainActivity.this;

        // Get the widget reference from xml layout
        mDrawerLayout = findViewById(R.id.drawer_layout);
        mNavigationView = findViewById(R.id.navigation_view);
        mToolbar = findViewById(R.id.toolbar);

        // Get the menu from navigation view
        mNavigationMenu = mNavigationView.getMenu();

        // Set a title for toolbar
        mToolbar.setTitle("Sample toolbar");

        // Set the support action bar
        setSupportActionBar(mToolbar);

        // Initialize a drawer toggle instance
        mDrawerToggle = new ActionBarDrawerToggle(
                mActivity,
                mDrawerLayout,
                mToolbar,
                R.string.drawer_open,
                R.string.drawer_close
        ){
            public void onDrawerClosed(View view){
                super.onDrawerClosed(view);
                // Do something
            }

            public void onDrawerOpened(View drawerView){
                super.onDrawerOpened(drawerView);
                // Do something
            }
        };
        mDrawerToggle.setDrawerIndicatorEnabled(true);
        mDrawerLayout.addDrawerListener(mDrawerToggle);
        mDrawerToggle.syncState();

        // Get the share menu item from drawer navigation menu
        MenuItem itemShare = mNavigationMenu.findItem(R.id.share);
        itemShare.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem menuItem) {
                mDrawerLayout.closeDrawers();
                // do something
                Toast.makeText(mContext,"Share item clicked.",Toast.LENGTH_SHORT).show();
                return false;
            }
        });

        // Get the custom view for menu item action view
        View v = View.inflate(mContext,R.layout.custom_layout_menu_item_action,null);

        // Get the edit and delete image button reference from custom view
        ImageButton editButton = v.findViewById(R.id.ib_edit);
        ImageButton deleteButton = v.findViewById(R.id.ib_delete);

        // Set the action view edit button click listener
        editButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mDrawerLayout.closeDrawers();
                // Do something
                Toast.makeText(mContext,"Edit button clicked.",Toast.LENGTH_SHORT).show();
            }
        });

        // Set the action view delete button click listener
        deleteButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mDrawerLayout.closeDrawers();
                // Do something
                Toast.makeText(mContext,"Delete button clicked.",Toast.LENGTH_SHORT).show();
            }
        });

        // Finally, set the action view
        itemShare.setActionView(v);
    }
}
res/values/styles.xml

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>

        <!-- Important for toolbar  -->
        <item name="windowNoTitle">true</item>
    </style>

</resources>
res/menu/navigation_view_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    >
    <item
        android:id="@+id/add_now"
        android:title="Add now"
        android:icon="@drawable/ic_add_circle_black_24dp"
        />
    <item
        android:id="@+id/set_alarm"
        android:title="Set alarm"
        android:icon="@drawable/ic_access_alarm_black_24dp"
        />
    <item
        android:id="@+id/share"
        android:title="Share"
        android:icon="@drawable/ic_share_black_24dp"
        />
</menu>
res/layout/custom_layout_menu_item_action.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    >
    <ImageButton
        android:id="@+id/ib_delete"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_delete_black_18dp"
        android:tint="#ff0000"
        android:background="@null"
        android:layout_marginRight="5dp"
        android:layout_marginEnd="5dp"
        />
    <ImageButton
        android:id="@+id/ib_edit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_edit_black_18dp"
        android:tint="#19cf71"
        android:background="@null"
        />
</LinearLayout>
res/values/strings.xml

<resources>
    <string name="app_name">DrawerLayout example</string>

    <string name="drawer_open">Open</string>
    <string name="drawer_close">Close</string>
</resources>