Friday, January 29, 2016

android - How to group Menu items

MainActivity.java

package com.cfsuman.androidtutorials;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;


public class MainActivity extends AppCompatActivity {
    private TextView mTextView;

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

        // Get the widgets reference from XML layout
        mTextView = findViewById(R.id.textView);
    }


    @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){
        int id  = item.getItemId();
        String title = item.getTitle().toString();

        if (id == R.id.text_bold){
            mTextView.append("\n" + title + " Clicked");
            return true;
        }else if (id == R.id.text_italic){
            mTextView.append("\n" + title + " Clicked");
            return true;
        }else {
            return super.onOptionsItemSelected(item);
        }
    }


    // To handle menu text color group items click event
    public void onColorGroupItemClick(MenuItem item){
        int id = item.getItemId();
        String title = item.getTitle().toString();

        if(id == R.id.red){
            // Checked the item
            item.setChecked(true);
            mTextView.append("\n" + title + " Checked");
        }else if (id == R.id.green){
            // Checked the item
            item.setChecked(true);
            mTextView.append("\n" + title + " Checked");
        }else if (id == R.id.blue){
            // Checked the item
            item.setChecked(true);
            mTextView.append("\n" + title + " Checked");
        }

    }


    // To handle menu text size group items click event
    public void onSizeGroupItemClick(MenuItem item){
        int id = item.getItemId();
        String title = item.getTitle().toString();

        if(id == R.id.large_text){
            // Check the item
            item.setChecked(true);
            mTextView.append("\n" + title + " Checked");
        }else if (id == R.id.medium_text){
            // Check the item
            item.setChecked(true);
            mTextView.append("\n" + title + " Checked");
        }else if (id == R.id.small_text){
            // Check the item
            item.setChecked(true);
            mTextView.append("\n" + title + " Checked");
        }
    }
}
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tool="http://schemas.android.com/tools"
    android:background="#DCDCDC"
    android:padding="24dp">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        tool:text="TextView"
        android:fontFamily="sans-serif"
        android:textSize="26sp"
        android:textStyle="italic"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
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_action_text_format"
        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_action_palette"
                    android:onClick="onColorGroupItemClick" />
                <item
                    android:id="@+id/green"
                    android:title="Green"
                    android:icon="@drawable/ic_action_palette"
                    android:onClick="onColorGroupItemClick" />
                <item
                    android:id="@+id/blue"
                    android:title="Blue"
                    android:icon="@drawable/ic_action_palette"
                    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_action_text_fields"
                    android:onClick="onSizeGroupItemClick" />
                <item
                    android:id="@+id/medium_text"
                    android:title="Medium"
                    android:icon="@drawable/ic_action_text_fields"
                    android:onClick="onSizeGroupItemClick" />
                <item
                    android:id="@+id/small_text"
                    android:title="Small"
                    android:icon="@drawable/ic_action_text_fields"
                    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_action_format_bold"
        android:orderInCategory="2" />
    <!-- italic text menu item -->
    <item
        android:id="@+id/text_italic"
        android:title="Italic Text"
        app:showAsAction="never|withText"
        android:icon="@drawable/ic_action_format_italic"
        android:orderInCategory="3" />
</menu>