Thursday, January 28, 2016

android - How to use CheckBox in Options Menu

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="#f1f7fa"
    >
    <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="50dp"
        android:gravity="center"
        />
</RelativeLayout>
res/menu/toolbar_options_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<!--
    Menu
        A menu resource defines an application menu (Options Menu, Context Menu, or submenu) that
        can be inflated with MenuInflater.
-->
<menu
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    >
    <!--
        android:checkable
            Boolean. "true" if the item is checkable.

        android:checked
            Boolean. "true" if the item is checked by default.
    -->
    <item
        android:id="@+id/red"
        android:title="Red"
        android:icon="@drawable/ic_palette_red_36dp"
        app:showAsAction="always|withText"
        android:orderInCategory="1"
        />
    <item
        android:id="@+id/green"
        android:title="Green"
        android:icon="@drawable/ic_palette_green_36dp"
        app:showAsAction="ifRoom|withText"
        android:orderInCategory="2"
        />
    <item
        android:id="@+id/blue"
        android:title="Blue"
        android:icon="@drawable/ic_palette_blue_36dp"
        app:showAsAction="ifRoom|withText"
        android:orderInCategory="3"
        />
    <item
        android:id="@+id/bold"
        android:title="Bold Text"
        app:showAsAction="never"
        android:orderInCategory="4"
        android:checkable="true"
        />
    <item
        android:id="@+id/italic"
        android:title="Italic Text"
        app:showAsAction="never"
        android:orderInCategory="5"
        android:checkable="true"
        />
</menu>
res/values/styles.xml

<resources>

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

</resources>
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.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;

    private boolean mBold = false;
    private boolean mItalic = false;


    @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 Options Menu CheckBox");
        mToolbar.setTitleTextColor(Color.WHITE);

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

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

    @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()){
            case R.id.red:
                // Set the text color to red
                mTextView.setTextColor(Color.RED);
                return true;
            case R.id.green:
                // Set the text color to green
                mTextView.setTextColor(Color.GREEN);
                return true;
            case R.id.blue:
                // Set the text color to blue
                mTextView.setTextColor(Color.BLUE);
                return true;
            case R.id.bold:
                if(item.isChecked()){
                    // If item already checked then unchecked it
                    item.setChecked(false);
                    mBold = false;
                }else{
                    // If item is unchecked then checked it
                    item.setChecked(true);
                    mBold = true;
                }
                // Update the text view text style
                updateTextView();
                return true;
            case R.id.italic:
                if(item.isChecked()){
                    // If item already checked then unchecked it
                    item.setChecked(false);
                    mItalic = false;
                }else {
                    // If item is unchecked then checked it
                    item.setChecked(true);
                    mItalic = true;
                }
                // Update the text view text style
                updateTextView();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    // Custom method to update the text view text style
    protected void updateTextView(){
        if(mBold && mItalic){
            mTextView.setTypeface(null, Typeface.BOLD_ITALIC);
        }else if(mBold && !mItalic){
            mTextView.setTypeface(null,Typeface.BOLD);
        }else if(!mBold && mItalic){
            mTextView.setTypeface(null,Typeface.ITALIC);
        }else {
            mTextView.setTypeface(null,Typeface.NORMAL);
        }
    }
}
build.gradle [dependencies]

compile 'com.android.support:appcompat-v7:23.1.1'