1. Creating New Project in Kotlin
- Open Android Studio.
- Go to File => New => New Project.Give Name To your application. Then, check Include Kotlin Support and click next button.
- Select minimum SDK you need. However, we have selected 21 as minimum SDK. Then, click next button.
- Then, select Empty Activity => click next => click finish.
2.Here we have one XML file activity_main.xml
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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:id="@+id/relative" tools:context="com.dharmendra.optionmenu.MainActivity"> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:textSize="20sp" android:textStyle="bold" android:textColor="@android:color/black" android:text="Click On 3 Dots on Action Bar " /> </RelativeLayout>
3.We need to create one more xml file inside res->menu folder.
First we have to Create folder name menu inside res folder. And inside menu folder we have to create xml file name option_menu.xml
option_menu Contain 5 Items first Two are to change Text Color and other 3 for change Background Color.
option_menu.xml
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/magenta" android:title="Magenta" /> <item android:id="@+id/yellow" android:title="Yellow" /> <item android:id="@+id/red" android:title="Red" /> <item android:id="@+id/blue" android:title="Blue" /> <item android:id="@+id/green" android:title="green" /> </menu>
Right Click on menu folder. New->menu resource file provide name to file and click Ok.
option_menu.xml file contain options which we want to provide.
4.MainActivity.kt
package com.dharmendra.optionmenu import android.graphics.Color import android.support.v7.app.AppCompatActivity import android.os.Bundle import android.view.Menu import android.view.MenuItem import android.widget.RelativeLayout import android.widget.TextView class MainActivity : AppCompatActivity() { //Define Objecct Of RelativeLayout lateinit var backgroud:RelativeLayout
//
Define Objecct Of TextView
lateinit var textView:TextView override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) //Find View By Id For TextView because We change the Color of text textView=findViewById<TextView>(R.id.textView) as TextView //Find View By Id For Main Layout because we chnage the Color of Backgroud backgroud=findViewById<RelativeLayout>(R.id.relative) as RelativeLayout } override fun onCreateOptionsMenu(menu: Menu?): Boolean { //Inflate menu menuInflater.inflate(R.menu.option_menu,menu) return true } //Method to Handle Option Item Click override fun onOptionsItemSelected(item: MenuItem?): Boolean { val selectedItem= item!!.itemId when(selectedItem){ R.id.magenta->{ //Change Text Color textView.setTextColor(Color.MAGENTA) } R.id.yellow->{ //Change Text Color textView.setTextColor(Color.YELLOW) } R.id.red->{ //Change Background Color backgroud.setBackgroundColor(Color.RED) } R.id.green->{ //Change Background Color backgroud.setBackgroundColor(Color.GREEN) } R.id.blue->{ //Change Background Color backgroud.setBackgroundColor(Color.BLUE) } } return super.onOptionsItemSelected(item) } }
Output:
No comments:
Post a Comment