Thursday, January 28, 2016

android - How to use CheckBox in Options Menu

MainActivity.java

package com.cfsuman.androidtutorials;

import android.graphics.Color;
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;
    private boolean mIsBold = false;
    private boolean mIsItalic = false;
    private boolean mIsUnderlined = false;

    @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);

        // Update textView
        updateTextView();
    }


    @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();

        if (id == R.id.blue){
            mTextView.setTextColor(Color.BLUE);
            updateTextView();
            return true;
        }else if (id == R.id.bold){
            // Toggle item is checked state
            item.setChecked(!item.isChecked());
            mIsBold = item.isChecked();
            updateTextView();
            return true;
        }else if (id == R.id.italic){
            item.setChecked(!item.isChecked());
            mIsItalic = item.isChecked();
            updateTextView();
            return true;
        }else if (id == R.id.underline){
            item.setChecked(!item.isChecked());
            mIsUnderlined = item.isChecked();
            updateTextView();
            return true;
        }else {
            return super.onOptionsItemSelected(item);
        }
    }



    // Method to update text view text
    private void updateTextView(){
        mTextView.setText(
                "Bold Text Checked ?: " + mIsBold
                        + "\n\nItalic Text Checked ?: " + mIsItalic
                        + "\n\nUnderlined Text Checked ?: "
                        + mIsUnderlined
        );
    }
}
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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#DCDCDC"
    android:padding="24dp">

    <TextView
        android:id="@+id/textView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="24dp"
        android:fontFamily="sans-serif"
        android:textSize="24sp"
        android:textStyle="italic"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:text="TextView" />

</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">
    <item
        android:id="@+id/blue"
        android:title="Blue"
        android:icon="@drawable/ic_action_palette"
        app:showAsAction="always|withText"
        android:orderInCategory="1"
        />
    <item
        android:id="@+id/bold"
        android:title="Bold Text"
        app:showAsAction="never"
        android:orderInCategory="2"
        android:checkable="true"
        />
    <item
        android:id="@+id/italic"
        android:title="Italic Text"
        app:showAsAction="never"
        android:orderInCategory="3"
        android:checkable="true"
        />
    <item
        android:id="@+id/underline"
        android:title="Underlined Text"
        app:showAsAction="never"
        android:orderInCategory="4"
        android:checkable="true"
        />
</menu>