Wednesday, June 10, 2020

android kotlin - Menu item text color

MainActivity.kt

package com.example.jetpack

import android.graphics.Color
import android.os.Bundle
import android.text.SpannableString
import android.text.style.ForegroundColorSpan
import android.view.Menu
import android.view.MenuItem
import androidx.appcompat.app.AppCompatActivity


class MainActivity : AppCompatActivity() {

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

       val actionBar = supportActionBar
        actionBar?.apply {
            title = "Kotlin - Menu Text Color"
        }
    }


    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.toolbar_menu,menu)

        // change menu text color programmatically
        menu?.apply {
            for(index in 0 until this.size()){
                val item = this.getItem(index)
                val s = SpannableString(item.title)
                s.setSpan(ForegroundColorSpan(Color.GREEN),0,s.length,0)
                item.title = s
            }
        }
        return super.onCreateOptionsMenu(menu)
    }


    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when(item.itemId){
            R.id.download->{
                // do something here
                return true
            }R.id.upload->{
                // do something here
                return true
            }R.id.cancel->{
                // do something here
                return true
            }R.id.settings->{
                // do something here
                return true
            }
        }
        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"/>
res/menu/toolbar_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/download"
        android:title="Download"
        android:icon="@drawable/ic_arrow_download"
        app:showAsAction="withText|always" />
    <item
        android:id="@+id/upload"
        android:title="Upload"
        android:icon="@drawable/ic_cloud_upload"
        app:showAsAction="always|withText" />
    <item
        android:id="@+id/cancel"
        android:title="Cancel"
        android:icon="@drawable/ic_cancel"
        app:showAsAction="never" />
    <item
        android:id="@+id/settings"
        android:title="Settings"
        android:icon="@drawable/ic_settings"
        app:showAsAction="never" />
</menu>