OptionMenu does not show.... - android

#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// mEditor=(TextView)findViewById(R.id.text);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
Log.d("sayem", "onCreateOptionMEnu");
return true;
}
My XML file:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="schemas.android.com/apk/res/android"; >
<item android:id="#+id/settings"
android:title="#string/settings_label"
android:visible="true"
android:alphabeticShortcut="#string/settings_shortcut"
android:icon="#drawable/violet" />
</menu>

take a look link1 or link2.
Or if don't want to inflate menu.xml, you can do this :
Try the following one, you don't need to inflate xml in this.
package com.menusample;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
public class MenuSampleActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
//menu.add(int groupId,int itemId,int orderId, Charsequence title);
menu.add(0, 0, 0, "title1");
menu.add(0, 1, 1, "title2");
menu.add(0, 2,2, "title3");
menu.add(0, 3, 3, "title4");
return super.onCreateOptionsMenu(menu);
}
}

The only difference that I can see in the code from my code is that in your XML file, the line
<menu xmlns:android="schemas.android.com/apk/res/android"; >
has a semi-colon at the end.
Mine does not, and the example from Android, at http://developer.android.com/guide/topics/ui/menus.html, does not have a semi-colon either.
The line of code should look like this:
<menu xmlns:android="http://schemas.android.com/apk/res/android">

Related

OnCreateOptionMenu is never called

I'm not able to dispay MenĂ¹ in my main Activity and all the other Activities.
This is my main.xml:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:background="#color/cardview_dark_background">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
card_view:cardBackgroundColor="#color/cardview_light_background">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/articoli"
android:onClick="visualizzaArticoli"
android:background="#null"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Articoli"
android:layout_gravity="center_vertical"
android:layout_marginLeft="50dp"/>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
This is my main.java file:
package com.mcsolution.easymanagementandroid.main;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.widget.Toast;
import com.mcsolution.easymanagementandroid.R;
import com.mcsolution.easymanagementandroid.beans.Program;
import com.mcsolution.easymanagementandroid.connection.MyDatabase;
import com.mcsolution.easymanagementandroid.utility.SyncDati;
public class Main extends Activity {
public MyDatabase db;
public ProgressDialog dialog;
public String url="";
private static final Intent SCAN_INTENT = new Intent("com.google.zxing.client.android.SCAN");
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.care_home, menu);
return super.onCreateOptionsMenu(menu);
}
private void apriConnessioneDB(){
db=new MyDatabase(getApplicationContext());
db.open(); //apriamo il db
}
public void visualizzaArticoli(View view){
Intent intent = new Intent(this, com.mcsolution.easymanagementandroid.articoli.viewArticoli.class);
startActivity(intent);
}
}
This is my menu care_home.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="#string/action_settings"/>
<item
android:id="#+id/action_logout"
android:orderInCategory="100"
android:title="Logout"/>
</menu>
If I try to start my application, I don't have any error but I can't to see MenuBar. I have try to set a debug point into OnCreateOptionMenu method but, the method is never called.
How can I fixed it ?
You need to return true; in onCreateOptionsMenu() as shown in the code below
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.care_home, menu);
return true;
}
Simply use this:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.care_home, menu);
return true;
}
Your activity class extends Activity, not AppCompatActivity. The default theme for these activities does not include an Action Bar, which is where the menu would live. You will need to add a <Toolbar> to your activity's layout file, and then call setActionBar() from your onCreate() method.
For example, add this as the first child of your top-level <LinearLayout> tag:
<Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
And add this just after you call setContentView() in your onCreate():
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setActionBar(toolbar);
Now you will have an Action Bar in your activity, and so your menu will appear.
You should return true instead of super like this:
#SuppressLint("RestrictedApi")
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
if (menu != null) {
}
return true;
}
Hope it helps.
You have to use both methods on your activity
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}

I can't display icons in the menu/actionBar (minSDK 17 and targetSDK 25), what's wrong?

compileSdkVersion 25
minSdkVersion 17
dependencies compile 'com.android.support:appcompat-v7:25.2.0'
This is my menu in /res/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">
<item
android:id="#+id/create_order"
android:title="Create Order"
android:icon="#drawable/ic_playlist_add_black_24dp"
android:orderInCategory="1"
app:showAsAction="always"/>
<item
android:id="#+id/action_setting"
android:orderInCategory="100"
android:title="Settings"
app:showAsAction="never"/>
</menu>
And this is the MainActivity:
package com.example.fulvio.bitsandpizzas;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
}
I'm not getting where my code is wrong, neither I can see the icon directly in the ActionBar and in the overflow menu, but only text.
I've already read the other similar questions (and answers), but none helped me
Android does not show icon anymore by default in menu. You have to use retrospection to achieve this (in your activity):
#Override
protected boolean onPrepareOptionsPanel(View view, Menu menu) {
if(menu != null){
if("MenuBuilder".equals(menu.getClass().getSimpleName())){
try{
Method m = menu.getClass().getDeclaredMethod(
"setOptionalIconsVisible", Boolean.TYPE);
m.setAccessible(true);
m.invoke(menu, true);
}
catch(NoSuchMethodException e){
//
}
}
}
return super.onPrepareOptionsPanel(view, menu);
}
I'm not fond of this method, but I did not find any other solution for this issue
You need to change
app:showAsAction = "always"
to
android:showAsAction = "always"
Or
You can use AppCompatActivity instead of Activity :
package com.example.fulvio.bitsandpizzas;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}

SearchView in action bar is shifted to right

I added SearchView to my action bar, but it appears to be shifted to right if there is back button (or drawer toggle) in action bar.
main_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:title="123"
android:icon="#mipmap/ic_launcher"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="collapseActionView|ifRoom"/>
</menu>
MainActivity.java:
package org.example.myapplication;
import android.os.Bundle;
import android.view.Menu;
import android.support.v7.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.main_menu, menu);
return true;
}
}
If setDisplayHomeAsUpEnabled(false):
If setDisplayHomeAsUpEnabled(true):
How can I fix this?

Android menu not inflating resource XML file using support v7

The ActionBar does not change, I don't know why:
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuInflater;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.testing_menu, menu);
return true;
}
}
I have included Android support library v7.
The testing_menu.xml file:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/go_back"
android:icon="#drawable/back"
android:title="Go back"
/>
</menu>
The "Go Back" does not showing up.
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.testing_menu, menu);
return true;
}
why don't you call return super.onCreateOptionsMenu(menu); ?

ActionBarSherlock pulldown menu with tabs

I'm trying to implement ActionBarSherlock with tabs but allow a pulldown menu in the action bar on certain tabs. I've read that the only way to get both is to create a custom actionProvider for the pulldown. Here is the code I wrote to implement:
menu_with_dropdown.xml (with a separate overflow menu that is working fine)
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/motivate_picker"
android:showAsAction="always"
android:title="Right Here"
android:actionProviderClass=".ActionBarSpinner">
</item>
<item
android:id="#+id/menu_item"
android:showAsAction="always"
android:icon="#drawable/action_bar_plus_icon">
<menu>
<item android:id="#+id/menu_1"
android:title="#string/menu_1" />
<item android:id="#+id/menu_2"
android:title="#string/menu_2" />
<item android:id="#+id/menu_3"
android:title="#string/menu_3"/>
<item android:id="#+id/menu_4"
android:title="#string/menu_4"/>
</menu>
</item>
</menu>
The tab fragment that uses this pulldown inflates it here
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu,inflater);
menu.clear();
getSherlockActivity().getSupportActionBar().setDisplayShowTitleEnabled(false);
getSherlockActivity().getSupportMenuInflater().inflate(R.menu.menu_with_dropdown, menu);
}
ActionBarSpinner.java
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import com.actionbarsherlock.view.ActionProvider;
import com.actionbarsherlock.view.MenuItem;
import com.actionbarsherlock.view.SubMenu;
public class ActionBarSpinner extends ActionProvider implements
MenuItem.OnMenuItemClickListener {
Context context_;
public ActionBarSpinner(Context context) {
super(context);
context_ = context;
}
#Override
public View onCreateActionView() {
LayoutInflater layoutInflater = LayoutInflater.from(context_);
View view = layoutInflater.inflate(R.layout.actionbar_menu,null);
//return view; //Will have formatted pulldown but not register clicks for submenu
return null; //Will register clicks and expand submenu but not be formatted as desired
}
#Override
public boolean onPerformDefaultAction() {
return false;
}
#Override
public boolean hasSubMenu() {
return true;
}
#Override
public void onPrepareSubMenu(SubMenu subMenu) {
MenuItem item = subMenu.add("Test1");
item.setOnMenuItemClickListener(this);
item = subMenu.add("Test3");
item.setOnMenuItemClickListener(this);
item = subMenu.add("Test2");
item.setOnMenuItemClickListener(this);
}
#Override
public boolean onMenuItemClick(MenuItem item) {
return true;
}
}
actionbar_menu.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
style="#android:style/Widget.ActionButton"
android:clickable="true">
<TextView
android:id="#+id/motivate_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Friend Picker"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:src="#drawable/switcher_arrow"/>
</LinearLayout>
So the problem arises when I call onCreateActionView() in ActionBarSpinner. If I return the inflated View of actionbar_menu.xml, it never expands the subMenu that I created. If I return null in onCreateActionView(), the submenu expands when I click on it but it doesn't have the custom layout I made (it actually shows nothing). Any help would be appreciated.
you have to return null in order for the onPrepareSubMenu() being called.
Then, all these setups in onCreateActionView() need to go in to onCreateOptionsMenu() in probably MainActivity.
For example,
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity, menu);
// Get the SearchView and set the searchable configuration
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.menu_search)
.getActionView();
searchView.setSearchableInfo(searchManager
.getSearchableInfo(getComponentName()));
// Override the default language icon to the language is being used
MenuItem mI = menu.findItem(R.id.menu_locale);
Locale locale = Locale.getDefault();
String locLan = locale.getLanguage();
if (locLan.equals(Locale.CHINESE.getLanguage())) {
mI.setIcon(R.drawable.insready);
} else if (locLan.equals(Locale.ENGLISH.getLanguage())) {
mI.setIcon(R.drawable.ic_action_locale_en);
} else if (locLan.equals(Locale.FRENCH.getLanguage())) {
mI.setIcon(R.drawable.ic_action_search);
}
return true;
}

Categories

Resources