Friday, January 29, 2016

android - How to group Menu items

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/rl"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="0dp"
    tools:context=".MainActivity"
    android:background="#fffde9"
    >
    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:elevation="4dp"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        />
    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Change This Text Style\nUsing Toolbar Menu"
        android:textSize="35dp"
        android:gravity="center"
        android:textStyle="bold"
        android:fontFamily="sans-serif-condensed"
        />
</RelativeLayout>
res/menu/toolbar_options_menu.xml

<?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"
    >
    <!-- complex menu item to set text style -->
    <item
        android:id="@+id/text_style"
        android:title="Text Style"
        app:showAsAction="always|withText"
        android:icon="@drawable/ic_palette_black_36dp"
        android:orderInCategory="1"
        >
        <!-- text style menu's sub menu start from here -->
        <menu>
            <!-- first group (text color) of text style sub menu -->
            <group
                android:id="@+id/text_color_group"
                android:checkableBehavior="single"
                >
                <!-- text color group's items -->
                <item
                    android:title="Select Text Color"
                    android:enabled="false"
                    android:checkable="false"
                    />
                <item
                    android:id="@+id/red"
                    android:title="Red"
                    android:icon="@drawable/ic_palette_red_36dp"
                    android:onClick="onColorGroupItemClick"
                    />
                <item
                    android:id="@+id/green"
                    android:title="Green"
                    android:icon="@drawable/ic_palette_green_36dp"
                    android:onClick="onColorGroupItemClick"
                    />
                <item
                    android:id="@+id/blue"
                    android:title="Blue"
                    android:icon="@drawable/ic_palette_blue_36dp"
                    android:onClick="onColorGroupItemClick"
                    />
            </group>
            <!-- second group (text size) of text style sub menu -->
            <group
                android:id="@+id/text_size_group"
                android:checkableBehavior="single"
                >
                <!-- text size group's items -->
                <item
                    android:title="Select Text Size"
                    android:enabled="false"
                    android:checkable="false"
                    />
                <item
                    android:id="@+id/large_text"
                    android:title="Large"
                    android:icon="@drawable/ic_text_fields_black_36dp"
                    android:onClick="onSizeGroupItemClick"
                    />
                <item
                    android:id="@+id/medium_text"
                    android:title="Medium"
                    android:icon="@drawable/ic_text_fields_black_36dp"
                    android:onClick="onSizeGroupItemClick"
                    />
                <item
                    android:id="@+id/small_text"
                    android:title="Small"
                    android:icon="@drawable/ic_text_fields_black_36dp"
                    android:onClick="onSizeGroupItemClick"
                    />
            </group>
        </menu>
    </item>
    <!-- bold text menu item -->
    <item
        android:id="@+id/text_bold"
        android:title="Bold Text"
        app:showAsAction="always|withText"
        android:icon="@drawable/ic_text_fields_black_36dp"
        android:orderInCategory="2"
        />
    <!-- normal text menu item -->
    <item
        android:id="@+id/text_normal"
        android:title="Normal Text"
        app:showAsAction="never|withText"
        android:icon="@drawable/ic_text_fields_black_36dp"
        android:orderInCategory="3"
        />
</menu>
MainActivity.java

package com.cfsuman.me.androidcodesnippets;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.support.v7.widget.Toolbar;


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

    private RelativeLayout mRelativeLayout;
    private TextView mTextView;
    private Toolbar mToolbar;


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

        // Get the application context
        mContext = getApplicationContext();

        // Get the activity
        mActivity = MainActivity.this;

        // Get the widgets reference from XML layout
        mRelativeLayout = (RelativeLayout) findViewById(R.id.rl);
        mTextView = (TextView) findViewById(R.id.tv);
        mToolbar = (Toolbar) findViewById(R.id.toolbar);

        // Set a title for toolbar
        mToolbar.setTitle("Android Menu Group Example");
        mToolbar.setTitleTextColor(Color.WHITE);

        // Set support actionbar with toolbar
        setSupportActionBar(mToolbar);

        // Change the toolbar background color
        mToolbar.setBackgroundColor(Color.parseColor("#FFB3AF8F"));
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu){
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.toolbar_options_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item){
        switch(item.getItemId()){
            // Handle the non group menu items here
            case R.id.text_bold:
                // Set the text to bold style
                mTextView.setTypeface(null, Typeface.BOLD);
                return true;
            case R.id.text_normal:
                // Set the text to normal style
                mTextView.setTypeface(null, Typeface.NORMAL);
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    // To handle menu text color group items click event
    public void onColorGroupItemClick(MenuItem item){
        // If red color selected
        if(item.getItemId() == R.id.red){
            // Checked the red color item
            item.setChecked(true);
            // Set the text view text color to red
            mTextView.setTextColor(Color.RED);
        }else if (item.getItemId() == R.id.green){
            // Checked the green color item
            item.setChecked(true);
            // Set the text view text color to green
            mTextView.setTextColor(Color.GREEN);
        }else if (item.getItemId() == R.id.blue){
            // Checked the blue color item
            item.setChecked(true);
            // Set the text view text color to blue
            mTextView.setTextColor(Color.BLUE);
        }else {
            // Do nothing
        }
    }

    // To handle menu text size group items click event
    public void onSizeGroupItemClick(MenuItem item){
        int id = item.getItemId();
        // if large text size selected
        if(id == R.id.large_text){
            // Check the item
            item.setChecked(true);
            // Set the text view text size to large
            mTextView.setTextSize(TypedValue.COMPLEX_UNIT_DIP,50);
        }else if (id == R.id.medium_text){
            // Check the item
            item.setChecked(true);
            // Set the text view text size to medium
            mTextView.setTextSize(TypedValue.COMPLEX_UNIT_DIP,30);
        }else if (id == R.id.small_text){
            // Check the item
            item.setChecked(true);
            // Set the text view text size to large
            mTextView.setTextSize(TypedValue.COMPLEX_UNIT_DIP,20);
        }else {
            // Do nothing
        }
    }
}