menu item won't show in action bar - java

I can't get an action button to appear in the action bar. I'm sure I'm just misunderstanding how this is supposed to be done. What am I doing wrong?
I'm going through the tutorial at developer.android.com and I've just added an item to the action bar. In the tutorial, the showAsAction for a Search button is set to ifRoom and it should show the button in the action bar, but it's only showing it in the overflow menu.
I thought there might not be enough room, so I've tried reducing the title of the app to a single letter. I've tried removing the title of the button. I've tried changing the showAsAction to every value except never.
Update 20150830_151538-0500: Changed android:showAsAction to app:showAsAction per recommendation in answers. The result is that the Search button doesn't show at all. Not even in the overflow. I also included the full MyActivity.java rather than just the snippet I included in the original question.
Update 20150831_222922-0500: There were two problems. The first was that I used android:showAsAction rather than app:showAsAction. This was pointed out right away in the first two answers. The second problem was that I was using an icon that was the wrong size. I was using one of the _48dp.png icons and that caused the item not to show at all because it was the wrong size for the action bar. This was pointed out in comments to the answer by #ci_.
Note: The original version of MyActivity.java, which was generated by Android Studio, inflated a menu_my.xml file for the menu. This menu file only included the Settings action using app:showAsAction="never". Following the tutorial, I replaced the content of the OnCreateOptionsMenu class. Unless I'm misunderstanding something this original menu_my.xml file isn't used at all, but since I started properly using app: instead of android: for the items that Settings item is the only one that's showing. I doubt it's related, but I wanted to mention it just in case.
MyActivity.java:
package com.ghodmode.myfirstapp;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
public class MyActivity extends AppCompatActivity {
public final static String EXTRA_MESSAGE = "com.ghodmode.myfirstapp.MESSAGE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
return super.onCreateOptionsMenu(menu);
}
#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);
}
/**
* Called when the user clicks the Send button
*/
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);
}
}
main_activity_actions.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">
<!-- Search, should appear as action button -->
<item
android:id="#+id/action_search"
android:icon="#drawable/ic_search_black_48dp"
app:showAsAction="ifRoom"
android:title="#string/action_search" />
<!-- Settings, should always be in the overflow -->
<item
android:id="#+id/action_settings"
app:showAsAction="never"
android:title="#string/action_settings" />
</menu>

This is a common mistake when using the ActionBarActivity or AppCompatActivity from the support library, which I'm assuming you're using here.
In this case a special appcompat namespace has to be used for showAsAction:
xmlns:app="http://schemas.android.com/apk/res-auto"
app:showAsAction="ifRoom"
If that still doesn't work for you, change the showAsAction back to never. If it now appears in the overflow menu, there is something wrong with your icon.

Try adding to your menu xml:
xmlns:app="http://schemas.android.com/apk/res-auto"
and then use:
app:showAsAction="always"

Related

Eclipse - Android - Invalid layout of java.lang.String at value

Good morning,
I have installed Eclipse and the plug in Android ADT.
I have created a new android project which is the classical :
package com.example.test_android_2;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
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.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_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
But when I run this code, there are the following things in the console window :
Invalid layout of java.lang.String at value
Error occurred during initialization of VM
Invalid layout of preloaded class: use -XX:+TraceClassLoading to see the origin of the problem class
There are no errors in the error window and nothing is happening. My connected device is not detected.
When i click on "Run as", there is no item (only "none applicable")
There is a problem of installation of Eclipse or ADT Android ?
Can you help me please ?
thanks
I faced this problem, and I think the best solutions as the following.
First, the versions that you want work with must be installed by SDK Manager.
Second,once you have installed the versions, then go to the run configuration and set the Android project that you have created. Third,
make sure that the SDK verison is the same as the target SDK as well as the Compile. From point of view if you are beginner with Android, I recommend you to start with API 14(version 4).
Thanks

share action provider is not showing

Here is my menu resource file
<item
android:id="#+id/action_share"
android:title="#string/action_share"
android:orderInCategory="2"
app:showAsAction="ifRoom"
android:actionProviderClass="android.widget.ShareActionProvider">
</item>
</menu>
And here is my java code
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.menu_main,menu);
MenuItem menuItem = menu.findItem(R.id.action_share);
shareActionProvider =(ShareActionProvider)menuItem.getActionProvider();
// setIntent("COOL NIKS");
return super.onCreateOptionsMenu(menu);
}
This is not showing share action provider. How do I fix it?
Here is how full working example should look like.This uses AppCompat library.
You need to add http://schemas.android.com/apk/res-auto schema to your menu items 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" >
<item
android:id="#+id/action_share"
app:actionProviderClass="android.support.v7.widget.ShareActionProvider"
app:showAsAction="always"
android:title="Share" />
</menu>
Now you will have to update your MainActivity to use ShareActionProvider
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.ShareActionProvider;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends ActionBarActivity {
private ShareActionProvider mShareActionProvider;
#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);
// Get the menu item.
MenuItem menuItem = menu.findItem(R.id.action_share);
// Get the provider and hold onto it to set/change the share intent.
mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(menuItem);
// Set share Intent.
// Note: You can set the share Intent afterwords if you don't want to set it right now.
mShareActionProvider.setShareIntent(createShareIntent());
return true;
}
// Create and return the Share Intent
private Intent createShareIntent() {
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT, "http://google.com");
return shareIntent;
}
// Sets new share Intent.
// Use this method to change or set Share Intent in your Activity Lifecycle.
private void changeShareIntent(Intent shareIntent) {
mShareActionProvider.setShareIntent(shareIntent);
}
}

Added action button on the toolbar isn't showing up

So I recently trying to add another action button beside the overflow icon on the toolbar:
But by following a tutorial on adding action buttons, I can't get it to show on my toolbar.
Here's my menu/menu_notify.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="com.example.jovie.canteen.MenuNotify">
<item
android:id="#+id/action_notify"
android:icon="#drawable/ic_notifications_black_24px"
android:title="#string/action_notify"
app:showAsAction="always" />
</menu>
I separated my supposed new action button on a new class which is MenuNotify from my main class, not sure if that's the correct way of adding new action buttons on the toolbar.
MenuNotify.java:
package com.example.jovie.canteen;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
/**
* Created by Jovie on 1/28/2016.
*/
public class MenuNotify extends AppCompatActivity {
#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_notify, 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.
switch (item.getItemId()) {
//noinspection SimplifiableIfStatement
case R.id.action_notify:
startActivity(new Intent(this, Home.class));
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
Btw, my filter icon button is in my main class.
EDIT:
Thanks g2o for the help!
I separated my supposed new action button on a new class which is MenuNotify from my main class, not sure if that's the correct way of adding new action buttons on the toolbar.
There is no need to create new class to add new action button.
Just add
<item
android:id="#+id/action_notify"
android:icon="#drawable/ic_notifications_black_24px"
android:title="#string/action_notify"
app:showAsAction="always" />
To your menu/main.xml and add
case R.id.action_notify:
startActivity(new Intent(this, Home.class));
return true;
to the switch of your Main class onOptionsItemSelected method.

Android Studio ERROR: Cannot resolve symbol 'View'

I'm trying to follow this tutorial from Google to create your own android app with Android Studio. But when I follow the 4th step on this page: http://developer.android.com/training/basics/firstapp/starting-activity.html Android Studio ends up with this error:
Cannot resolve symbol 'View'
This is what my code looks like at the moment:
public class MainActivity extends ActionBarActivity {
/** Called when the user clicks the Send button */
public void sendMessage(View view) { <--- (This line ends up with the error)
// Do something in response to button
}
#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);
}
}
What's wrong with this code? I'm not experienced in java and after looking at other questions I still couldn't find the right solution.
Thanks for helping!
I think you forget to include the import statement for View. Add the following import in your code
import android.view.View;
I am doing the same tutorial and ran into the same problem (that's why I found this question).
I see they explain this issue in the next paragraph named "Build an Intent":
Android Studio will display Cannot resolve symbol errors because this
code references classes that are not imported. You can solve some of
these with Android Studio's "import class" functionality by pressing
Alt + Enter (or Option + Return on Mac). Your imports should end up as
the following:
import android.content.Intent; import
android.support.v7.app.AppCompatActivity; import android.os.Bundle;
import android.view.View; import android.widget.EditText;
https://developer.android.com/training/basics/firstapp/starting-activity.html#BuildIntent
This problem can be resolved easily either by pressing alt + enter on the error to import android.view.View or by cross-checking that your method is outside protected void onCreate(Bundle savedInstanceState) and in the class parenthesis.

Type Error generating final archive: java.io.FileNotFoundException:xx\bin\resources.ap_ does not exist

I write my android project, when I run it, it has two problems that I can't solve it.. .
I read the other's same problem and answer of them, but my problem doesn't solve.
The problems are:
Type Error generating final archive java.io.FileNotFoundException:xx\bin\resources.ap_ does not exist.
Unparsed aapt error(s)! Check the console for output.
My project code is: src/com.divani.marzieh/ActionActivity:
package com.Divani.Marzieh;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
public class ActionActivity 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) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_main_actions, menu);
return super.onCreateOptionsMenu(menu);
}
}
res/menu/activity_main_actions.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- Refresh -->
<item android:id="#+id/action_refresh"
android:icon="#drawable/ic_action_refresh"
android:title="#string/action_refresh"
android:showAsAction="ifRoom" />
</menu>
What should I've done?
Try this! Go to Window ->Android SDK manager and update Tools and Android 4.0. The same problem has occured for me but after trying this, it has solved my issues.
If that doesnt work, try updating everything.

Categories