I'm a begginer in android development and I have a problem. My first menu has no separator lines in portreit and landscape modes. Also I've no separator in action bar in portreit mode while I have it in landscape one. Separator for action bar is a pic 1x32 placed at ./drawable. At the same time under AVDs and Android-x86 (VirtualBox) is fine. Please, see the fig1: .
Thanks in advance!
Update 1:
Create and show menu:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.main_menu_sync:
Toast("Refresh menu");
return true;
case R.id.main_menu_more:
Toast("More menu");
return true;
// ...
default:
return super.onOptionsItemSelected(item);
}
}
Menu layout (./res/menu/main_menu.xml):
<item
android:id="#+id/main_menu_undo"
android:icon="#drawable/ic_menu_undo"
android:title="Undo"/>
<item
android:id="#+id/main_menu_multiselect"
android:icon="#drawable/ic_menu_multiselect"
android:title="Multiselect"/>
<item
android:id="#+id/main_menu_sync"
android:icon="#drawable/ic_menu_refresh"
android:title="Sync"/>
<item
android:id="#+id/main_menu_more"
android:icon="#drawable/ic_menu_more"
android:title="More">
<menu>
<item
android:id="#+id/main_menu_preferences"
android:icon="#drawable/ic_menu_preferences"
android:title="Preferences"/>
<item
android:id="#+id/main_menu_tab_order"
android:icon="#drawable/ic_menu_tab_order"
android:title="Tab order"/>
</menu>
</item>
If your device is QVGA and you set the anyDensity attribute to false, some separators on the UI will be missing.
See more details at
http://developer.android.com/guide/practices/screens_support.html
I think your screen is supporting the view that you are using. You should add the support screen tag to your manifest.xml file. It works for me. Or may be you can check the following link to get some more info.
Related
My app for that moment has 3 buttons, which allow for switches LED and read data from humidity sensor by bluetooth (each one sends another command to arduino e.g. '0' - LED OFF, '1' - LED ON, '2' - READ DATA).
Today I wanted add sending any messages to arduino, so I insert EditTextBox to my layout and I decided that the button to send message to Arduino from this EditTextBox will be on the action bar with this icon send icon
menu -> main.xml
`<!-- language: lang-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/action_send"
android:icon="#drawable/ic_send_black_48dp"
android:title="#string/action_send" // this string is empty ' '
android:orderInCategory="1"
app:showAsAction="always" />
<item
android:id="#+id/action_settings"
android:orderInCategory="2"
android:title="#string/action_settings"
app:showAsAction="never" />
</menu>`
MainActivity.java
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// COMPLETED (9) Within onCreateOptionsMenu, use getMenuInflater().inflate to inflate the menu
getMenuInflater().inflate(R.menu.main, menu);
// COMPLETED (10) Return true to display your menu
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_settings:
Context context = MainActivity.this;
String textToShow = "...";
Toast.makeText(context, textToShow, Toast.LENGTH_SHORT).show();
return true;
case R.id.action_send:
mConnectedThread.write(txtBluetooth.getText().toString());
return true;
default:
// If we got here, the user's action was not recognized.
// Invoke the superclass to handle it.
return super.onOptionsItemSelected(item);
}
}
How can you see, I set app:showAsAction="always" /> and despite this send icon missing on action bar and is sent to the overflow menu, as in the picture: app
So, how can i prevent this and make that send icon will appear on action bar no in overflow menu?
I saw many posts in SO but couldn't figure out why Overflow icon is not displayed. I am testing on real device Android 4.4 . I get the items displayed on bottom of the screen when options button is pressed . But I need it to appear as 3 dot menu on Top Right like in Gmail App. Pl help. Need something like attached pic of Tubemate app when options button clicked.
Menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<!--xmlns:com.app.MainActivity="http://schemas.android.com/apk/res-auto"-->
<item
android:id="#+id/action_search"
android:actionViewClass="android.widget.SearchView"
android:icon="#drawable/icon_search"
android:orderInCategory="1"
android:title="search"
android:showAsAction="never"/>
<item
android:id="#+id/action_end"
android:icon="#drawable/end"
android:menuCategory="container"
android:orderInCategory="2"
android:title="#string/end"
android:showAsAction="never"/>
</menu>
MainActivity:
#Override
public boolean onPrepareOptionsMenu(Menu menu)
{ menu.clear();
this.menu = menu;
getMenuInflater().inflate(R.menu.main, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
return super.onPrepareOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
switch (item.getItemId()) {
case R.id.action_end:
System.exit(0);
break;
case R.id.action_search:
break;
default:
break;
}
return false;
}
Overflow menu will only appear on devices that don't have physical menu button. In devices with physical menu button it will appear from bottom.
To force overflow menu, put this code inside onCreate:
try {
ViewConfiguration config = ViewConfiguration.get(this);
Field menuKeyField = ViewConfiguration.class
.getDeclaredField("sHasPermanentMenuKey");
if (menuKeyField != null) {
menuKeyField.setAccessible(true);
menuKeyField.setBoolean(config, false);
}
} catch (Exception e) {
e.printStackTrace();
}
}
Source
Please see below the menu.xml file I have created , which I have kept under res-> menu-> folder
<item android:id="#+id/menu_About"
android:icon="#drawable/icon_about"
android:title="#string/About" />
<item android:id="#+id/menu_Settings"
android:icon="#drawable/icon_settings"
android:title="#string/Settings" />
<item android:id="#+id/menu_Exit"
android:icon="#drawable/icon_exit"
android:title="#string/Exit" />
I have kept the icon_about.png , icon_settings.png , icon_exit.png files under res-> drawable-hdpi ,drawable-ldpi , drawable-mdpi , drawable-xhdpi, drawable-xxhdpi folders but when my application runs, these icons don't show up. Please tell me if I am missing something.
Thanks
Try to use this in your activity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
Inflate the menu; // this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
check if you have used the icons of correct sizes. for option menus the desired sizes are
ldpi 32 x 32 px
mdpi: 36 x 36 px
hdpi: 48 x 48 px
xhdpi: 64 x 64 px
I have kept the icon_about.png , icon_settings.png , icon_exit.png
files under res-> drawable-hdpi ,drawable-ldpi , drawable-mdpi ,
drawable-xhdpi, drawable-xxhdpi folders but when my application runs,
these icons don't show up. Please tell me if I am missing something.
=> You haven't included android:showAsAction attribute in all the menu items.
open your main Activity class file and type following code. In the following code each menu item is identified by its ID in switch case statement.
public class MenusActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
// Initiating Menu XML file (menu.xml)
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.layout.menu, menu);
return true;
}
/**
* Event Handling for Individual menu item selected
* Identify single menu item by it's id
* */
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.menu_About:
Toast.makeText(MenusActivity.this, "about is Selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_settings:
Toast.makeText(MenusActivity.this, "settings is Selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_Exit:
Toast.makeText(MenusActivity.this, "Exit is Selected", Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
. Finally run your project by right clicking on your project.On Android Emulator click on Menu Button to launch menu.
<item
android:id="#+id/web_link"
android:icon="#drawable/menu_About"
android:orderInCategory="100"
android:showAsAction="always"
android:title="Web link"/>
You have to add android:showAsAction="always" if you want always..there other option too. like "never", which used to never show the icon if you want they can add programatically,and other "ifroom" if there are place than this menu item will appear
I have an ActionBar using ActionBar Sherlock where I need it to display overflow because I have more actions than room. But, it doesn't show the overflow icon. Here is my configuration:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/menu_search"
android:icon="#drawable/action_search"
android:title="#string/menu_search"
android:showAsAction="ifRoom|withText"/>
<item android:id="#+id/menu_library"
android:icon="#drawable/hardware_headphones"
android:title="#string/my_music"
android:showAsAction="ifRoom|withText"/>
<item android:id="#+id/menu_downloads"
android:icon="#drawable/av_download"
android:title="#string/downloads"
android:showAsAction="ifRoom|withText"/>
</menu>
And here is code to set it up:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getSupportMenuInflater();
menuInflater.inflate(R.menu.shopping_menu, menu);
MenuItem searchMenuItem = menu.findItem(R.id.menu_search);
searchMenuItem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
startActivity(new Intent(ShopActivity.this, SearchDialog.class));
return false;
}
});
MenuItem downloadMenuItem = menu.findItem(R.id.menu_downloads);
downloadMenuItem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
startActivity( new Intent(ShopActivity.this, DownloadQueueActivity.class) );
return false;
}
});
MenuItem myMusicItem = menu.findItem(R.id.menu_library);
myMusicItem.setOnMenuItemClickListener( new MenuItem.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
startActivity(new Intent(ShopActivity.this, MyMusicActivity.class));
return false;
}
});
return true;
}
I've looked over the demos in ActionBar Sherlock, but I can't tell what they do differently to get the overflow than what I'm doing. So what's happening here why its not showing?
If you have a physical menu key, the overflow indicator does not show. That is a behaviour by design. See here for more details on another question asked.
Hmm I think there are two issues here. First, as t0mm13b states, if the device has a physical menu key, the overflow indicator does NOT show. This is by design. Although in my experience, it doesn't apply to every device (unfortunately...)
The second issue is that, if you want to force an item to the overflow, you need to set the showAsAction to "never". Otherwise, the only elements that appear in the overflow are ones that simply "don't fit" in the action bar. And given that you have 3 items that you want to display with text.. you're pretty much guranteed to have at least one overflow item, and therefore the overflow icon (with the caveat of the first paragraph)
Try by changing the android:showAsAction tag to app:showAsAction according to the Android guide in the menu_main.xml file as shown below
Add this line if not present
menu xmlns:app="http://schemas.android.com/apk/res-auto"
<item android:id="#+id/action_search"
android:icon="#drawable/search_icon"
android:title="#string/action_search"
app:showAsAction="always"/> <!--change here android: to app:-->
<item android:id="#+id/action_location"
android:icon="#drawable/location_icon"
android:title="#string/action_locate"
app:showAsAction="always"/>
</menu>
I try to create OptionMenu which application's target is 15 but minSDK is 8. I Have one Menu folder and mymenu.xml in it. I wanna use the default icon menu and wanna make it support since sdk 8. How should it do that? The problem when i test is, the option menu icon show only on sdkVersion 8 but, not show on sdkVersion 15
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/my_menu" android:title="Send Mail" android:icon="#android:drawable/ic_menu_send"></item>
</menu>
and in Activity
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.mymenu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.my_menu:
//Do something
return true;
default:
return super.onOptionsItemSelected(item);
}
}
The action overflow -- what you are thinking of as the options menu on API Level 11+ -- does not show icons.
#ohh: android:showAsAction="ifRoom", you can add it to your menu.xml