Latest News

Add Items/Menu In Actionbar/Toolbar (Kotlin) - Android Studio

In this tutorial we will add items in actionbar/toolbar , 3-dot hidangan in actionbar/toolbar and handle item clicks using Kotlin. There will be three items in hidangan you can add as many as you want. One item will be displayed on action kafe with icon , other two items will be displayed by clicking 3-dot menu...

Add items/menu in Actionbar/Toolbar (Kotlin) - Android Studio

Step 1: Create a new Project or open new project

Step 2: Create new "Android Resource Directory" by clicking "res>New>Android Resource Directory" , choose hidangan from Resource type

Add items/menu in Actionbar/Toolbar (Kotlin) - Android Studio


Step 3: Create menu.xml by clicking "menu>New>Menu resource file"

Add items/menu in Actionbar/Toolbar (Kotlin) - Android Studio

Step 4: Enter an icon in drawable folder by clicking "drawable>New>Image Asset" 

Add items/menu in Actionbar/Toolbar (Kotlin) - Android Studio

Step 5: Code

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/action_one"
android:icon="@drawable/ic_action_name"
app:showAsAction="always"
android:title="Item One"/>
  <item
android:id="@+id/action_two"
android:title="Item Two"/>
  <item
android:id="@+id/action_three"
android:title="Item Three"/>
</menu>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?><android.support.constraint.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"tools:context=".MainActivity"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Hello World!"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toRightOf="parent"app:layout_constraintTop_toTopOf="parent" /></android.support.constraint.ConstraintLayout>
MainActivity.kt
package com.blogspot.devofandroid.myapplicationimport android.support.v7.app.AppCompatActivityimport android.os.Bundleimport android.view.Menuimport android.view.MenuItemimport android.widget.Toastclass MainActivity : AppCompatActivity() {override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)setContentView(R.layout.activity_main)}override fun onCreateOptionsMenu(menu: Menu): Boolean {// Inflate the menu; this adds items to the action kafe if it is present.menuInflater.inflate(R.menu.menu , menu)return true}override fun onOptionsItemSelected(item: MenuItem): Boolean {// Handle action kafe item clicks here.val id = item.getItemId()if (id == R.id.action_one) {Toast.makeText(this , "Item One Clicked" , Toast.LENGTH_LONG).show()return true}if (id == R.id.action_two) {Toast.makeText(this , "Item Two Clicked" , Toast.LENGTH_LONG).show()return true}if (id == R.id.action_three) {Toast.makeText(this , "Item Three Clicked" , Toast.LENGTH_LONG).show()return true}return super.onOptionsItemSelected(item)}}

Step 6: Run Project

Output
Add items/menu in Actionbar/Toolbar (Kotlin) - Android Studio




0 Response to "Add Items/Menu In Actionbar/Toolbar (Kotlin) - Android Studio"