Latest News

Options Sajian Fragment - Java

Options Menu Fragment - Android Studio - Java

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 Koltin

Video:


Step 1: Create a new project OR Open your 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"app:showAsAction="always"android:icon="@drawable/ic_sort_white"/></menu>

FragmentOne.java

package com.blogspot.atifsoftwares.myapplication;import android.os.Bundle;import android.support.annotation.Nullable;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.Menu;import android.view.MenuInflater;import android.view.MenuItem;import android.view.View;import android.view.ViewGroup;import android.widget.Toast;/** * A simple {@link Fragment} subclass. */public class FragmentOne extends Fragment {public FragmentOne() {// Required empty public constructor}@Overridepublic View onCreateView(LayoutInflater inflater , ViewGroup container , Bundle savedInstanceState) {// Inflate the layout for this fragmentreturn inflater.inflate(R.layout.fragment_fragment_one , container , false);}/*Enable options sajian in this fragment*/@Overridepublic void onCreate(@Nullable Bundle savedInstanceState) {setHasOptionsMenu(true);super.onCreate(savedInstanceState);}@Overridepublic void onCreateOptionsMenu(Menu menu , MenuInflater inflater) {//inflate menuinflater.inflate(R.menu.menu_main , menu);super.onCreateOptionsMenu(menu , inflater);}@Overridepublic boolean onOptionsItemSelected(MenuItem item) {//handle sajian item clicksint id = item.getItemId();if (id == R.id.action_settings) {//do your function hereToast.makeText(getActivity() , "Settings" , Toast.LENGTH_SHORT).show();}if (id == R.id.action_sort) {//do your function hereToast.makeText(getActivity() , "Sort" , Toast.LENGTH_SHORT).show();}return super.onOptionsItemSelected(item);}}

FragmentTwo.java

package com.blogspot.atifsoftwares.myapplication;import android.os.Bundle;import android.support.annotation.Nullable;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.Menu;import android.view.MenuInflater;import android.view.MenuItem;import android.view.View;import android.view.ViewGroup;import android.widget.Toast;/** * A simple {@link Fragment} subclass. */public class FragmentTwo extends Fragment {public FragmentTwo() {// Required empty public constructor}@Overridepublic View onCreateView(LayoutInflater inflater , ViewGroup container , Bundle savedInstanceState) {// Inflate the layout for this fragmentreturn inflater.inflate(R.layout.fragment_fragment_two , container , false);}/*Enable options sajian in this fragment*/@Overridepublic void onCreate(@Nullable Bundle savedInstanceState) {setHasOptionsMenu(true);super.onCreate(savedInstanceState);}@Overridepublic void onCreateOptionsMenu(Menu menu , MenuInflater inflater) {//inflate menuinflater.inflate(R.menu.menu_main , menu);//hide item (sort)menu.findItem(R.id.action_sort).setVisible(false);super.onCreateOptionsMenu(menu , inflater);}@Overridepublic boolean onOptionsItemSelected(MenuItem item) {//handle sajian item clicksint id = item.getItemId();if (id == R.id.action_settings) {//do your function hereToast.makeText(getActivity() , "Settings" , Toast.LENGTH_SHORT).show();}return super.onOptionsItemSelected(item);}}

FragmentThree.java

package com.blogspot.atifsoftwares.myapplication;import android.os.Bundle;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;/** * A simple {@link Fragment} subclass. */public class FragmentThree extends Fragment {public FragmentThree() {// Required empty public constructor}@Overridepublic View onCreateView(LayoutInflater inflater , ViewGroup container , Bundle savedInstanceState) {// Inflate the layout for this fragmentreturn inflater.inflate(R.layout.fragment_fragment_three , container , false);}}

Step 3: Run Project

Output
Options Menu Fragment - Android Studio - Java
Options Menu Fragment - Android Studio - Java

0 Response to "Options Sajian Fragment - Java"