How to don't show menu item in Action Overflow? - android

My meny code is:
<item android:id="#+id/action_map"
android:icon="#android:drawable/ic_dialog_map"
android:title="Add"
android:showAsAction="ifRoom"/>
<item
android:id="#+id/action_settings"
android:showAsAction="never"
android:title="#string/action_settings" />
</menu>
My Fragment code is:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_where_am_i, container, false);
setHasOptionsMenu(true);
return root;
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.where_i_am_menu, menu);
super.onCreateOptionsMenu(menu, inflater);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.action_settings:
startActivity(new Intent(getActivity(), SettingsActivity.class));
return true;
case R.id.action_map:
return true;
}
return super.onOptionsItemSelected(item);
}
And i get next action bar. Why i have 2 settings in that list? And i don't want to add label "Add" to that list, hiw can i make it?

You should clear the menu:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.clear();
inflater.inflate(R.menu.where_i_am_menu, menu);
super.onCreateOptionsMenu(menu, inflater);
}
Hope it helps you :)

Related

android fragment action bar menu

I have created a fragment with action bar menu, that menu was shown but not working when its clicked.
Here is My Fragment:
public class ComposeFragment extends Fragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_compose, container, false);
userName = (TextView) view.findViewById(R.id.user_name);
subjectSpinner = (Spinner) view.findViewById(R.id.subject_spinner);
sendButton = (Button) view.findViewById(R.id.send_btn);
messageEditText = (EditText) view.findViewById(R.id.message);
userName.setText(Ezcation.getInstance().userName);
return view;
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.compose_menu, menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
Log.e("Menu","Before Switch");
switch (item.getItemId()){
case R.id.sent:
Log.e("Menu","Sent");
if (messageEditText.getText().toString().equals("")){
messageEditText.setError("Please Enter your Message");
}else {
sendMessage(messageEditText.getText().toString());
}
return true;
default:
return super.onOptionsItemSelected(item);
}
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
this.messageActivity = (MessageActivity) context;
SpannableString s = new SpannableString("Compose Message");
s.setSpan(new TypefaceSpan(messageActivity, "Miller-Text.ttf"), 0, s.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
messageActivity.setTitle(s);
}
}
When menu was clicked even Log.e("Menu","Before Switch"); not working.
My Menu xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/sent"
android:title="Sent"
android:orderInCategory="10"
android:icon="#drawable/sent"
app:showAsAction="ifRoom" />
</menu>
For Future visiters you should use this in Activity class:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}
To work properly hardcoding false isnt the right way
add this in your Activity.
#Override public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
break;
}
return super.onOptionsItemSelected(item);;
}
The super call is missing inside onCreateOptionsMenu
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.compose_menu, menu);
super.onCreateOptionsMenu(menu, inflater);
}

how to use Buttons on action bar With Fragments?

i want to add button on the actionbar of a fragment.But i'm getting error on this code given below.
this is my Fragment code where i want to add button on actionbar of this tab
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.credit_main, container, false);
return view;
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.search_menu, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// action with ID action_refresh was selected
case R.id.search:
Toast.makeText(getActivity(), "Refresh selected", Toast.LENGTH_SHORT)
.show();
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
}
here error is getMenuInflater(); cannot resolve
Step 1 : Make a xml of menu which you want to add.
<menu 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" >
<item
android:id="#+id/item1"
android:title="#string/filter"
android:orderInCategory="10"
android:icon="#drawable/ic_launcher"
app:showAsAction="ifRoom" />
</menu>
Step 2:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
Step 3:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.search_result, menu);
}
Step 4:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if(id == R.id.item1){
//What you want(Code Here)
return true;
}
return super.onOptionsItemSelected(item);
}
You have to call setHasOptionsMenu(true) to make it work.
Do something like:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
and then use the onCreateOptionsMenu() method with the inflater.
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// Inflate the menu items for use in the action bar
inflater.inflate(R.menu.search_menu, menu);
super.onCreateOptionsMenu(menu, inflater);
}
Take a look at setHasOptionsMenu() and onCreateOptionsMenu().

Menu setActionView(view) to xml

In onCreateOptionsMenu I'm setting an action view with:
MenuItem item = ..
item.setActionView(action_view)
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
and it works ok. But I want to move this to my xml menu and I can't seem to make it show. It only shows up the title. I tried many versions, like:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourApp="http://schemas.android.com/apk/lib/myPackage.myClass" >
<item
android:id="#+id/item"
yourApp:actionViewClass="action_view"
android:showAsAction="always" or youtApp:showAsAction="always"
android:title="title"
</item>
The problem must be it getting to the action_view variable, but I can't seem to figure it out
Did you inflate the menubar?
#Override
public void onCreateOptionsMenu (Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_name, menu);
mMenu = menu;
if(mMenu != null){
MenuItem item = mMenu.findItem(R.id.item_id);
if(item != null){
item.setVisible(false);
}
}
super.onCreateOptionsMenu(menu, inflater);
}
Also do you have setHasOptionsMenu(true); #onCreate() function?
Try this:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.item:
// Write your action_view here
break;
case R.id.your_other_item_id:
//Your other Menu item logic
break;
}
return super.onOptionsItemSelected(item);
}

OptionsMenu not working

I have this fragment connected with a menu.xml. I the menu opens when i click it, everything works besides the onOptionsItemSelected. I've tried to log it but nothing gives result. Why isn't my code working?
Activity
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// TODO Add your menu entries here
super.onCreateOptionsMenu(menu, inflater);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId())
{
case R.id.clear:
writetofile("0", "file1.txt");
writetofile("0", "file2.txt");
writetofile("0", "file3.txt");
break;
case R.id.add:
writetofile("1", "file1.txt");
writetofile("1", "file2.txt");
writetofile("1", "file3.txt");
break;
}
return true;
}
XML
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/clear"
android:orderInCategory="100"
android:title="Clean"/>
<item
android:id="#+id/add"
android:orderInCategory="100"
android:title="Add"/>
You didn't inflate the menu
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menu, menu);
}

adding menu bar in SherlockFragmentActivity

i want to add the menu in my class getting Error *Cannot override the final method from SherlockFragment * please tell me how to do this
my code is
public class FragementFirst extends SherlockFragment{
Button btn;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragementone, container, false);
btn = (Button) rootView.findViewById(R.id.butto);
setHasOptionsMenu(true);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), FirstActiviry.class);
getActivity().startActivity(intent);
}
});
return rootView;
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.first, menu);
}
}
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/action_task"
android:icon="#drawable/icon_xhdpi"
android:title="hello"
android:showAsAction="ifRoom" />
</menu>
please use
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
getSupportMenuInflater().inflate(R.menu.first, menu);
}
instead of
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.first, menu);
}
to understand a tutorial is there
please visit http://www.grokkingandroid.com/adding-action-items-from-within-fragments/

Categories

Resources