I am complete newbie when it comes to Android applications. Please bear with me. I am trying to make an menu options bar but somehow the icons are not displaying.
I had looked for answers to this question on stackoverflow and the answer that I came across was "On Android 3.0+, the preferred approach for the options menu (a spillover menu in the action bar) will not show icons. If you have android targetSdkVersion of 11 or higher, icons will never show up in menus on Android 3.0+. The icons will show up if you promote an options menu item to be a toolbar button, and the icons will show up on Android 1.x/2.x devices." I am sorry but I am not sure what that means. Can someone please direct me in the right direction? Any help will be appreciated!
I am not sure what I am doing incorrectly. Here is my code so far.
MainActivity.java
package com.example.useoptionsmenu;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
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) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.about:
startActivity(new Intent(this, About.class));
return true;
case R.id.help:
startActivity(new Intent(this, Help.class));
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="#string/action_settings"/>
<item
android:id="#+id/about"
android:showAsAction="always"
android:orderInCategory="1"
android:icon="#drawable/ic_action_about"
android:title="About"/>
<item
android:id="#+id/help"
android:showAsAction="always"
android:orderInCategory="2"
android:icon="#drawable/ic_action_help"
android:title="Help"/>
</menu>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.useoptionsmenu"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.useoptionsmenu.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.useoptionsmenu.About"
android:label="#string/title_activity_about" >
</activity>
<activity
android:name="com.example.useoptionsmenu.Help"
android:label="#string/title_activity_help" >
</activity>
</application>
</manifest>
You have to add this to the item you want to display in the header:
android:showAsAction="ifRoom"
so that your menu will be:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:showAsAction="ifRoom"
android:title="#string/action_settings"/>
<item
android:id="#+id/about"
android:showAsAction="ifRoom"
android:orderInCategory="1"
android:icon="#drawable/ic_action_about"
android:title="About"/>
<item
android:id="#+id/help"
android:showAsAction="ifRoom"
android:orderInCategory="2"
android:icon="#drawable/ic_action_help"
android:title="Help"/>
</menu>
remove the line android:showAsAction="ifRoom" from the item you won't display in your header.
Related
UPDATE: The problem is only in the xml view. On running the app it works just fine. Some error with the rendering in android studio i think.
Im watching a video tutorial in which the action bar shows the overflow menu by default. When i create a new project the overflow menu isnt visible even though menu_main.xml contains an item. i tried using different minimum SDK settings while creating the project and also tried different app themes but it didnt work. here is the code->
menu_main.xml
<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_settings" android:title="#string/action_settings"
android:orderInCategory="100" app:showAsAction="never" />
</menu>
activity_main.xml
<RelativeLayout 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" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity">
<TextView android:text="#string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.shreyans.overflowmenu" >
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
MainActivity.java
package com.example.shreyans.overflowmenu;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
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) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Can anyone tell me a way to make the overflow menu visible?
You can try this
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/i1"
android:icon="#drawable/icon"
android:title="#string/title_i1"/>
<item
android:id="#+id/i2"
android:icon="#drawable/icon"
android:title="#string/title_i2"/>
<item
android:id="#+id/i3"
android:icon="#drawable/icon"
android:title="#string/title_i3"/>
<item
android:id="#+id/overflow"
android:icon="#drawable/ic_action_overflow"
android:orderInCategory="100"
android:showAsAction="always"
android:title="#string/title_menu">
<menu>
<item
android:id="#+id/i1"
android:icon="#drawable/icon"
android:title="#string/title_i1"/>
<item
android:id="#+id/i2"
android:icon="#drawable/icon"
android:title="#string/title_i2"/>
<item
android:id="#+id/i3"
android:icon="#drawable/icon"
android:title="#string/title_i3"/>
</menu>
</item>
here is my code
MainActivity
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) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
}
menu_main.xml
<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_settings" android:title="#string/action_settings"
android:orderInCategory="100" app:showAsAction="never" />
<item android:id="#+id/one" android:title="oneone"
android:orderInCategory="100" app:showAsAction="always" />
<item android:id="#+id/two" android:title="twotwo"
android:orderInCategory="100" app:showAsAction="ifRoom" />
<item android:id="#+id/three" android:title="threethree"
android:orderInCategory="100" app:showAsAction="ifRoom" />
<item android:id="#+id/four" android:title="fourfour"
android:orderInCategory="100" app:showAsAction="ifRoom" />
</menu>
manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sanjith.actionbartwo" >
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme"
>
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
This code gives me three buttons on the actionbar and one on the overflow.
I want to get all the buttons on the bottom actionbar.I have searched the internet for this but i haven't found a solution.
Please help.
Try this http://developer.android.com/guide/topics/ui/actionbar.html#SplitBar
<manifest ...>
<activity uiOptions="splitActionBarWhenNarrow" ... >
<meta-data android:name="android.support.UI_OPTIONS"
android:value="splitActionBarWhenNarrow" />
</activity>
</manifest>
I have encountered this problem before unfortunately the action bar doesn't split by code you need to fill all the room available so the action bar would split by itself with your permission of course but there are no possible way to split it if it still have space
I was following this tutorial on Android when i came across a problem. As mentioned in topic my search icon isn't showing up on the action bar.
I had problem with that step.
http://developer.android.com/training/basics/actionbar/adding-buttons.html
I think i have done everything the same but still nothing shows up.
I've seen other people having similar problem but their answers didn't work for me.
The worst thing is unlike others people problems related to search icon not showing up, mine isn't even showing up on overflow while theirs is. Maybe it can guide someone to what's my issue.
main_activity_actions.xml code below
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" >
<!-- Search, should appear as action button -->
<item
android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
app:showAsAction="ifRoom"/>
<!-- Settings, should always be in the overflow -->
<item
android:id="#+id/action_settings"
android:title="#string/action_settings"
app:showAsAction="never"/>
</menu>
main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" >
<!-- Search, should appear as action button -->
<item
android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
app:showAsAction="ifRoom"/>
<!-- Settings, should always be in the overflow -->
<item
android:id="#+id/action_settings"
android:title="#string/action_settings"
app:showAsAction="never"/>
</menu>
activity.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"
android:orientation="horizontal" >
<EditText
android:id="#+id/edit_message"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="#string/edit_message" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="#string/button_send"
android:onClick="sendMessage" />
</LinearLayout>
MainActivity.java
public class MainActivity extends ActionBarActivity {
public final static String EXTRA_MESSAGE = "com.pawel.androidapp.MESSAGE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#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_activity_actions, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here.
switch(item.getItemId())
{
case R.id.action_search:
openSearch();
return true;
case R.id.action_settings:
openSettings();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void openSearch() {
Toast.makeText(this, "Search pressed", Toast.LENGTH_SHORT).show();
}
private void openSettings() {
startActivity(new Intent(android.provider.Settings.ACTION_SETTINGS));
}
public void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}
androidapp manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.pawel.androidapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/Theme.AppCompat" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".DisplayMessageActivity"
android:label="#string/title_activity_display_message"
android:parentActivityName="com.pawel.myfirstapp.MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.pawel.androidapp.MainActivity" />
</activity>
</application>
</manifest>
I had the same problem. You're using minSdkVersion="11" which is when the ActionBar APIs were defined. (http://developer.android.com/guide/topics/ui/actionbar.html)
The line I had wrong was:
app:showAsAction="ifRoom"
I had looked at the development tutorials which are illustrating use of the Support Library and my line looked the same as yours. I changed it to this:
android:showAsAction="ifRoom"
and it worked. Use the "android" namespace instead of the "app" namespace.
(https://developer.android.com/training/basics/actionbar/adding-buttons.html)
I have same problem, I got the answer after exchanging below 3 "android" to "app":
<menu xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<!-- Search, should appear as action button -->
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
app:showAsAction="ifRoom"
android:orderInCategory="100" />
<!-- Settings, should always be in the overflow -->
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
app:showAsAction="always"
android:orderInCategory="101" />
</menu>
I had the ditto problem and i did not change anything in the code as others suggest above. I had downloaded the Action Pack icons from Android URL "http://developer.android.com/design/downloads/index.html#action-bar-icon-pack" and this has two folders named "Core Icons" and "Action Bar Icons". I used the icon ic_action_search from within the "Core Icons" folder and that caused the problem. I replaced the icon at "drawable" folder with the one from "Action Bar Icons/Holo Dark" and it was working the way intended. That was a moment of relief for a android developer. :)
It's more than likely down to
app:showAsAction="ifRoom"
You're basically saying only show the icon if there is room available to show it. So try :
app:showAsAction="always"
See http://developer.android.com/guide/topics/resources/menu-resource.html for more.
Edit :
Sorry, I've just noticed that you're using the namespace http://schemas.android.com/apk/res-auto for this attribute.
Does it work if you change it to :
android:showAsAction="always"
you have missed:
app:actionViewClass="android.support.v7.widget.SearchView"
from your item.
I tested your code in my project and I found that the issue was in the Manifest file.
android:theme="#style/Theme.AppCompat" >
Change it to:
android:theme="#style/Theme.AppCompat.Light" >
I am trying to implement action bar(appcompatv_7) on API version 8. Although there is no error in my code but the action bar won't show up, instead it will appear in text format by pressing menu button.
Here is the java code:
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) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
}
Code for menu:
<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="com.example.usingsherlockkk.MainActivity" >
<item
android:id="#+id/item1"
android:icon="#android:drawable/ic_menu_add"
android:showAsAction="ifRoom"
android:title="Title1">
</item>
<item
android:id="#+id/item2"
android:icon="#android:drawable/ic_menu_delete"
android:showAsAction="ifRoom"
android:title="Title2">
</item>
<item
android:id="#+id/reset"
android:icon="#android:drawable/ic_menu_revert"
android:showAsAction="ifRoom"
android:title="Revert">
</item>
<item
android:id="#+id/item4"
android:icon="#android:drawable/ic_menu_edit"
android:showAsAction="ifRoom"
android:title="Title4">
</item>
<item
android:id="#+id/item5"
android:icon="#android:drawable/ic_menu_help"
android:showAsAction="ifRoom"
android:title="Title5">
</item>
</menu>
Manifest file code:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.usingsherlockkk"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/Theme.AppCompat.Light"
android:uiOptions="splitActionBarWhenNarrow" >
<activity
android:name="com.example.usingsherlockkk.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Guys my problem is that all the menu items are displayed in overflow where only the title appears. I checked it with big/small screen devices but it remains the same.
I hope I have explained my problem well enough. Let me know if something is hazy
Try this code
#Override
protected void onCreate(Bundle arg0) {
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
super.onCreate(arg0);
setContentView(R.layout.activity_main);
}
Ok guys I got the solution to my very own problem.
The problem was in defining the action items in the "menu.xml".
Android documentation says you cannot use "android:showAsAction" on older devices as it does not exist in older versions. You have to use a custom namespace. Instead of saying "android:showAsAction", use "app:showAsAction" and you are good to go. Here is the attachment from official page:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
yourapp:showAsAction="ifRoom" />
...
</menu>
Using XML attributes from the support library:
Notice that the showAsAction attribute above uses a custom namespace defined in the tag. This is necessary when using any XML attributes defined by the support library, because these attributes do not exist in the Android framework on older devices. So you must use your own namespace as a prefix for all attributes defined by the support library.
I hope it helps someone out there.
The way I did it is as follows:
There was a line in AndroidManifest.xml file
android:theme="#style/AppTheme.NoActionBar"
I simply changed it to
android:theme="#style/AppTheme"
and problem was solved.
i need to have a menu or an action bar that must be display when the user use the application but the problem is that the menu items do not show only ofter i click the menu button on the emulator can anyone help me ????
test.java
package com.example.test;
import com.example.test.R;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class test extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
}
#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_activity_menu, menu);
return true;
}
}
mani_activity_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/menu_settings"
android:orderInCategory="1"
android:showAsAction="always"
android:title="settings"/>
<item
android:id="#+id/twitter"
android:orderInCategory="2"
android:showAsAction="always"
android:title="twitter"/>
<item
android:id="#+id/facebook"
android:orderInCategory="3"
android:showAsAction="always"
android:title="facebook"/>
<item
android:id="#+id/refresh"
android:title="refresh"
android:showAsAction="always"
android:orderInCategory="4"
/>
</menu>
Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.test"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".test"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
style.xml
<resources>
<!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Light">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
-->
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
</resources>
Tried your code . Its working without any problems
You have made spelling mistake at mani_activity_menu.xml ?
Add
setHasOptionsMenu(true); //in oncreate
item.setEnabled(false); //add this in oncreated option
package com.example.exammmm;
import android.os.Bundle;
import android.app.Activity;
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) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
(source: mediafire.com)
Read -----> http://developer.android.com/guide/topics/ui/actionbar.html