Unable to display menu icons in android application - android

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

Related

Android studio - icons on action bar are always listed in overflow menu, why?

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?

Display icon at top of the screen - Android

I am working on android UX design. I am using RelativeLayout. I want to do the below screen in android.
I want to know how to show an icon at the top of the screen (red marked).
When i click that icon, i want to show the information about the app with a black background.
I am new to android and i don't know how to do this. Need some help to do.
That's an ActionBar (Toolbar) Menu icon:
You need to add a menu file inside res/menu, like: main.xml formed this way:
<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"
tools:context=".MainActivity">
<item
android:id="#+id/action_main"
android:title="#string/action_main"
android:orderInCategory="100"
android:icon="#drawable/ic_launcher"
app:showAsAction="ifRoom" />
</menu>
Over "android:icon" goes the one you want, "android:title" the one you want when the user long press the icon.
EDIT Check the updated code with comments
To have listener (onClick actions) do this over the 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;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if(id == R.id.action_main){
Toast.makeText(getApplicationContext(), "Main action is selected!", Toast.LENGTH_SHORT).show();
// Toast only have a small duration to show something,
// even when you long press the item, the Title is also a
// Toast. You can literally do anything here. Show stuffs,
// hide it, open activities, close the app, etc
return true;
}
return super.onOptionsItemSelected(item);
}
add in menul
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/phone"
android:title="#string/phone"
android:icon="#drawable/phone"
android:showAsAction="always"
/>
</menu
After that in java
#Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
switch(item.getItemId()){
case R.id.phone:
Toast.makeText(getBaseContext(), "You selected Phone", Toast.LENGTH_SHORT).show();
break;
}
return true;

Is it possible to using voice commands for navigating instead of using swipe gesture?

Hi, there
Currently, I'm develop an immersion app to provide a text on screen and user can swipe_right to go another one.
Actually, It adapt from sample immersion pattern called charades(google development site).
My objective is, I want to using voice commands, instead of SWIPE gesture.
for example;
User open the immersion demo, the screen will show first TEXT.
User want to go next TEXT by using voice "GO NEXT".
The screen will show another Text.
Considering this post!
Is there any way to do this?
or any suggestion?
Here, It is my solution. Hope this might helped someone who looking for.
I used Contextual voice commands to provide 'Next', 'Save' and 'Exit' commands for an user. you can go to this document from google dev site to see the ideas of doing this.
I have my layout activity to show some TEXT, so I put this code structure. in my layout activity
//contextual voice command
import com.google.android.glass.view.WindowUtils;
import android.view.Menu;
import android.view.MenuItem;
#Override
public boolean onCreatePanelMenu(int featureId, Menu menu) {
if (featureId == WindowUtils.FEATURE_VOICE_COMMANDS) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
// Pass through to super to setup touch menu.
return super.onCreatePanelMenu(featureId, menu);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
if (featureId == WindowUtils.FEATURE_VOICE_COMMANDS) {
switch (item.getItemId()) {
case R.id.save_menu_item:
Log.d("Contextual", "go save checks");
break;
case R.id.next_menu_item:
Log.d("Contextual", "go next checks");
break;
case R.id.exit_menu_item:
Log.d("Contextual", "go exit checks");
break;
default:
return true;
}
return true;
}
return super.onMenuItemSelected(featureId, item);
}
Don't forget to declare this line getWindow().requestFeature(WindowUtils.FEATURE_VOICE_COMMANDS); to your onCreate(); before your setContentView().
Next thing, I created 'menu folder' and main.xml inside its to provide my item selection. Like this
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/next_menu_item"
android:title="#string/next">
</item>
<item
android:id="#+id/save_menu_item"
android:title="#string/save_this">
</item>
<item
android:id="#+id/exit_menu_item"
android:title="#string/exit">
</item>
and my strings.xml file.
<resources>
<string name="next">next</string>
<string name="save_this">save</string>
<string name="exit">exit</string>
</resources>
put this line
<uses-permission android:name="com.google.android.glass.permission.DEVELOPMENT" />
to your AndroidMenifest.xml.
and it works fine for me !

Why OptionMenu not show icon menu in 4.0 but work in 2.x.x

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

Android: menu's separators is disappeared

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.

Categories

Resources