Wednesday, 11 January 2017

Android Option Menu in Fragment Example

Hello friend today i am going to share a code of option menu in Activity .Please follow me step by step 




Step 1: Create OptionMenuFragment and write this code



import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Toast;


/**
* Created by riteshsingh on 11/01/17.
*/

public class OptionMenuFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.level_new
, container, false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public void onPrepareOptionsMenu(Menu menu) {
menu.findItem(R.id.menu_1).setVisible(true).setEnabled(true).setTitle("Menu 1");
menu.findItem(R.id.menu_2).setVisible(true).setEnabled(true).setTitle("Menu 2");
menu.findItem(R.id.menu_3).setVisible(true).setEnabled(true).setTitle("Menu 3");
menu.findItem(R.id.menu_4).setVisible(true).setEnabled(true).setTitle("Menu 4");
menu.findItem(R.id.menu_5).setVisible(true).setEnabled(true).setTitle("Menu 5");

menu.findItem(R.id.action_settings).setVisible(true).setEnabled(true).setTitle("Settings").setIcon(R.drawable.ic_action_settings);
return;
}



@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.menu_1:
Toast.makeText(getActivity(),"Menu 1 Clicked",Toast.LENGTH_SHORT).show();
break;
case R.id.menu_2:
Toast.makeText(getActivity(),"Menu 2 Clicked",Toast.LENGTH_SHORT).show();
break;
case R.id.menu_3:
Toast.makeText(getActivity(),"Menu 3 Clicked",Toast.LENGTH_SHORT).show();
break;

case R.id.menu_4:
Toast.makeText(getActivity(),"Menu 4 Clicked",Toast.LENGTH_SHORT).show();
break;
case R.id.menu_5:
Toast.makeText(getActivity(),"Menu 5 Clicked",Toast.LENGTH_SHORT).show();
break;

case R.id.action_settings:
Toast.makeText(getActivity(),"Menu Settings Clicked",Toast.LENGTH_SHORT).show();
break;
}
return super.onOptionsItemSelected(item);
}
}

Step 2: write this code in your res>menu>main.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<item
android:id="@+id/menu_1"
android:orderInCategory="1"
android:title="Menu 1"
app:showAsAction="never" />
<item
android:id="@+id/menu_2"
android:orderInCategory="2"
android:title="Menu 2"

app:showAsAction="never" />
<item
android:id="@+id/menu_3"
android:orderInCategory="3"
android:title="Menu 3"
app:showAsAction="never" />
<item
android:id="@+id/menu_4"
android:orderInCategory="4"
android:title="Menu 4"
app:showAsAction="never" />
<item
android:id="@+id/menu_5"
android:orderInCategory="5"
android:title="Menu 5"
app:showAsAction="never" />
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:visible="false"
android:title="Settings"

app:showAsAction="ifRoom" />
</menu>

Step 3 : Setup your theme in manifest for material and toolbar and add appcompact in your build.gradle

 a.  Theme.AppCompat
 b.  compile 'com.android.support:appcompat-v7:25.0.0'








2 comments:

  1. It's interesting that many of the bloggers your tips helped to clarify a few things for me as well as giving... very specific nice content.Android Training|Android Training in chennai with placement

    ReplyDelete
  2. This information is impressive; I am inspired with your post writing style & how continuously you describe this topic. After reading your post, thanks for taking the time to discuss this, I feel happy about it and I love learning more about this topic.Android Training institute in chennai with placement | Best Android Training in velachery

    ReplyDelete