Options Menu Fragment - Android Studio - Kotlin
This will cover the followings:✓Use Options Menu / Actionbar menu
✓Use Options Menu in fragments
✓Add items with icons in options menu
✓Show different options of an options sajian in each fragment
✓Handle options menu's item clicks
>>Watch For Java
Video:
Step 1: Create a new Project or open new project
Step 2: Code
menu_main.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"><itemandroid:id="@+id/action_settings"android:title="Settings"/><item android:id="@+id/action_sort"android:title="Sort"android:icon="@drawable/ic_sort_white"app:showAsAction="always"/></menu>
FragmentOne.kt
package com.blogspot.atifsoftwares.myapplicationimport android.os.Bundleimport android.support.v4.app.Fragmentimport android.view.LayoutInflaterimport android.view.Menuimport android.view.MenuInflaterimport android.view.MenuItemimport android.view.Viewimport android.view.ViewGroupimport android.widget.Toast/** * A simple [Fragment] subclass. */class FragmentOne : Fragment() {override fun onCreateView(inflater: LayoutInflater , container: ViewGroup? ,savedInstanceState: Bundle?): View? {// Inflate the layout for this fragmentreturn inflater.inflate(R.layout.fragment_fragment_one , container , false)}//enable options sajian in this fragmentoverride fun onCreate(savedInstanceState: Bundle?) {setHasOptionsMenu(true)super.onCreate(savedInstanceState)}//inflate the menuoverride fun onCreateOptionsMenu(menu: Menu? , inflater: MenuInflater?) {inflater!!.inflate(R.menu.menu_main , menu)super.onCreateOptionsMenu(menu , inflater)}//handle item clicks of menuoverride fun onOptionsItemSelected(item: MenuItem?): Boolean {//get item id to handle item clicksval id = item!!.itemId//handle item clicksif (id == R.id.action_settings){//do your action here , im just showing toastToast.makeText(activity , "Settings" , Toast.LENGTH_SHORT).show()}if (id == R.id.action_sort){//do your action here , im just showing toastToast.makeText(activity , "Sort" , Toast.LENGTH_SHORT).show()}return super.onOptionsItemSelected(item)}}
FragmentTwo.kt
package com.blogspot.atifsoftwares.myapplicationimport android.os.Bundleimport android.support.v4.app.Fragmentimport android.view.LayoutInflaterimport android.view.Menuimport android.view.MenuInflaterimport android.view.MenuItemimport android.view.Viewimport android.view.ViewGroupimport android.widget.Toast/** * A simple [Fragment] subclass. */class FragmentTwo : Fragment() {override fun onCreateView(inflater: LayoutInflater , container: ViewGroup? ,savedInstanceState: Bundle?): View? {// Inflate the layout for this fragmentreturn inflater.inflate(R.layout.fragment_fragment_two , container , false)}//enable options sajian in this fragmentoverride fun onCreate(savedInstanceState: Bundle?) {setHasOptionsMenu(true)super.onCreate(savedInstanceState)}//inflate the menuoverride fun onCreateOptionsMenu(menu: Menu? , inflater: MenuInflater?) {inflater!!.inflate(R.menu.menu_main , menu)//hide some items from this fragment (e.g. sort)menu!!.findItem(R.id.action_sort).isVisible = falsesuper.onCreateOptionsMenu(menu , inflater)}//handle item clicks of menuoverride fun onOptionsItemSelected(item: MenuItem?): Boolean {//get item id to handle item clicksval id = item!!.itemId//handle item clicksif (id == R.id.action_settings){//do your action here , im just showing toastToast.makeText(activity , "Settings" , Toast.LENGTH_SHORT).show()}return super.onOptionsItemSelected(item)}}
FragmentThree.kt
package com.blogspot.atifsoftwares.myapplicationimport android.os.Bundleimport android.support.v4.app.Fragmentimport android.view.LayoutInflaterimport android.view.Viewimport android.view.ViewGroup/** * A simple [Fragment] subclass. */class FragmentThree : Fragment() {override fun onCreateView(inflater: LayoutInflater , container: ViewGroup? ,savedInstanceState: Bundle?): View? {// Inflate the layout for this fragmentreturn inflater.inflate(R.layout.fragment_fragment_three , container , false)}/*Leaving this fragment as it is , because to show example of fragment having no options menu*/}
Step 3: Run Project
Output:Options Menu Fragment - Android Studio - Kotlin |
0 Response to "Options Hidangan Fragment - Kotlin"