Friday, June 12, 2020

android kotlin - Checkable menu item

MainActivity.kt

package com.example.jetpack

import android.annotation.SuppressLint
import android.graphics.*
import android.os.Build
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.view.menu.MenuBuilder
import kotlinx.android.synthetic.main.activity_main.*


class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }


    @SuppressLint("RestrictedApi")
    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.options_menu,menu)

        // To display icon on overflow menu
        if (menu is MenuBuilder) {
            menu.setOptionalIconsVisible(true)
        }

        // change menu icon color programmatically
        menu?.apply {
            for(index in 0 until this.size()){
                val item = this.getItem(index)

                var color = Color.parseColor("#4F42B5")
                when (item.itemId) {
                    R.id.red -> {
                        color = Color.RED
                    }
                    R.id.green -> {
                        color = Color.GREEN
                    }
                    R.id.yellow -> {
                        color = Color.YELLOW
                    }
                }

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                    item.icon.colorFilter = BlendModeColorFilter(
                        color, BlendMode.SRC_IN
                    )
                }else{
                    item.icon.setColorFilter(color, PorterDuff.Mode.SRC_IN)
                }
            }
        }

        return super.onCreateOptionsMenu(menu)
    }


    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when(item.itemId){
            R.id.red->{
                textView.setTextColor(Color.RED)
            }R.id.green->{
                textView.setTextColor(Color.GREEN)
            }R.id.yellow->{
                textView.setTextColor(Color.YELLOW)
            }

            R.id.bold->{
                item.isChecked = !item.isChecked
                if (item.isChecked){
                    if (textView.typeface.isItalic){
                        textView.setTypeface(textView.typeface,Typeface.BOLD_ITALIC)
                    }else{
                        textView.setTypeface(textView.typeface,Typeface.BOLD)
                    }
                }else{
                    if (textView.typeface.isItalic){
                        textView.setTypeface(textView.typeface,Typeface.ITALIC)
                    }else{
                        textView.setTypeface(textView.typeface,Typeface.NORMAL)
                    }
                }
            }

            R.id.italic->{
                item.isChecked = !item.isChecked
                if (item.isChecked){
                    if (textView.typeface.isBold){
                        textView.setTypeface(textView.typeface,Typeface.BOLD_ITALIC)
                    }else{
                        textView.setTypeface(textView.typeface,Typeface.ITALIC)
                    }
                }else{
                    if (textView.typeface.isBold){
                        textView.setTypeface(textView.typeface,Typeface.BOLD)
                    }else{
                        textView.setTypeface(textView.typeface,Typeface.NORMAL)
                    }
                }
            }

            R.id.underline->{
                item.isChecked = !item.isChecked
                if (item.isChecked){
                    textView.paintFlags = textView.paintFlags or
                            Paint.UNDERLINE_TEXT_FLAG
                }else{
                    textView.paintFlags = 0
                }
            }
        }

        return super.onOptionsItemSelected(item)
    }
}
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:id="@+id/constraintLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <com.google.android.material.textview.MaterialTextView
        android:id="@+id/textView"
        style="@style/TextAppearance.MaterialComponents.Headline4"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginEnd="16dp"
        android:background="#F6F6F2"
        android:padding="5dp"
        android:text="Kotlin Checkable Menu Item Example"
        android:typeface="monospace"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
res/menu/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/red"
        android:title="Red"
        android:icon="@drawable/ic_color_lens"
        app:showAsAction="always"
        />
    <item
        android:id="@+id/green"
        android:title="Green"
        android:icon="@drawable/ic_color_lens"
        app:showAsAction="always"
        />
    <item
        android:id="@+id/yellow"
        android:title="Yellow"
        android:icon="@drawable/ic_color_lens"
        app:showAsAction="always"
        />
    <item
        android:id="@+id/bold"
        android:title="Bold"
        android:icon="@drawable/ic_format_bold"
        app:showAsAction="never"
        android:checkable="true"
        android:checked="false"
        />
    <item
        android:id="@+id/italic"
        android:title="Italic"
        android:icon="@drawable/ic_format_italic"
        app:showAsAction="never"
        android:checkable="true"
        />
    <item
        android:id="@+id/underline"
        android:title="Underline"
        android:icon="@drawable/ic_format_underlined"
        app:showAsAction="never"
        android:checkable="true"
        />
</menu>