Action bar not showing overflow menu - android

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>

Related

Android: Missing overflow menu in Material Design

My overflow menu is not appearing in my action bar/App bar (even running emulator) but it only appears in menu_main.xml. What can I do?
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mo.thesis">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
app_bar.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
android:background="#ccc">
</android.support.v7.widget.Toolbar>
MainActivity.java
package com.mo.thesis;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
private Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
}
protected boolean onCreateOptionMenu(Menu menu) {
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);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context="com.mo.thesis.MainActivity">
<include
android:id="#+id/app_bar"
layout="#layout/app_bar" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="#+id/textView"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_marginStart="78dp"
android:layout_marginTop="119dp" />
</RelativeLayout>
activity_main.xml Screen Preview:
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>
menu_main.xml Screen Preview:
styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="AppTheme.Base">
<!-- Customize your theme here. -->
</style>
<style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#color/primaryColor</item>
<item name="colorPrimaryDark">#color/primaryColorDark</item>
<item name="colorAccent">#color/accentColor</item>
</style>
</resources>
Hope this helps, From your code did not found any issue.
There might be other issue ---
The on-screen overflow menu button only appears in the action bar on devices that lack an off-screen MENU button. Try pressing the MENU button on your device or emulator.
please read this Menu Button - Missing answer.
Your menu is set to never show
<item
android:id="#+id/action_settings"
android:title="#string/action_settings"
android:orderInCategory="100"
app:showAsAction="never" />
Change last line there to "always"

How to add a split ActionBar in android.I have got everything right but it still doesn't work.

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

Android - ActionBar icon not showhing

I have trouble seeing the icons in the ActionBar.
I also read around here in StackOverFlow, but nothing...
Here is my action.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" >
<item
android:id="#+id/addAct"
android:icon="#android:drawable/ic_input_add"
android:background="#null"
app:showAsAction="always"
android:title="#string/add"
/>
<item
android:id="#+id/searchAct"
android:icon="#android:drawable/ic_menu_search"
app:showAsAction="ifRoom"
android:title="Cerca"
android:actionViewClass="android.widget.SearchView"
/>
<item
android:id="#+id/visualizza"
android:title="#string/txtVisualizza"
app:showAsAction="always"
>
</item>
Here, is AndroiManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<uses-sdk
android:minSdkVersion="11"
android:maxSdkVersion="21"/>
<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>
<activity
android:name=".WriteActivity"
android:label="#string/title_activity_write" >
</activity>
</application>
Here, MainActivity:
public class MainActivity extends 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.action, menu);
return super.onCreateOptionsMenu(menu);
}
In the xml code, I have used the app for ShowAsAction, but this not seems be the problem.
Thanks for any help :)
You are developing app for material design ( Target SDK = 21).
It does not support Activity.
So change extends Activity to extends ActionBarActivity.
Now your menu will be visible.
And you can edit action bar using getSupportActionBar().

Android Search icon not showing up on Action Bar

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" >

Icons are not displaying in Android application

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.

Categories

Resources