How do I show icon in the Bar action when item is in overflow?

Asked

Viewed 2,263 times

3

I wanted the item called also to appear on your side your image:

inserir a descrição da imagem aqui

The result I would expect would be this:

inserir a descrição da imagem aqui

Activity code with help from @Alexandre Strevenski:

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.widget.ShareActionProvider;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends Activity {

private ShareActionProvider mShareActionProvider;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main_activity_actions, menu);
        MenuItem shareItem = menu.findItem(R.id.acao4);
        mShareActionProvider = (ShareActionProvider)
                MenuItemCompat.getActionProvider(shareItem);
        mShareActionProvider.setShareIntent(getDefaultIntent());

        return super.onCreateOptionsMenu(menu);
    }

    private Intent getDefaultIntent() {
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("image/*");
        return intent;
    }
}

main_activity_actions.xml:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:yourapp="http://schemas.android.com/apk/res-auto" >

    <item
        android:id="@+id/acao1"
        android:icon="@android:drawable/ic_menu_delete"
        android:showAsAction="ifRoom|withText"
        android:title="deletar"/>
    <item
        android:id="@+id/acao2"
        android:icon="@android:drawable/ic_menu_add"
        android:showAsAction="ifRoom|withText"
        android:title="adicionar"/>
    <item
        android:id="@+id/acao3"
        android:icon="@android:drawable/ic_menu_camera"
        android:showAsAction="ifRoom|withText"
        android:title="camera"/>

    <item
        android:id="@+id/acao4"
        android:icon="@android:drawable/ic_menu_call"
        android:showAsAction="ifRoom|withText"
        android:title="chamada"
        yourapp:actionProviderClass="android.support.v7.widget.ShareActionProvider"/>

</menu>

Note: I already used android:actionProviderClass="android.support.v7.widget.Shareactionprovider" and also not sure.

Error log:

inserir a descrição da imagem aqui

  • You’re talking specifically about ShareActionProvider or item/sub whatever you are doing?

3 answers

3


If the idea and have icons in the menus of Overflow Menu and not with ActionProvider then you need to use Reflection to resolve, since icons are not displayed by default.

In this case, just implement the method onMenuOpened in his Activity and call the method setOptionalIconsVisible, which is privatedoMenuporReflection`:

@Override
public boolean onMenuOpened(int featureId, Menu menu) {
    if(featureId == Window.FEATURE_ACTION_BAR && menu != null){
        if(menu.getClass().getSimpleName().equals("MenuBuilder")){
            try{
                Method m = menu.getClass().getDeclaredMethod("setOptionalIconsVisible", Boolean.TYPE);
                m.setAccessible(true);
                m.invoke(menu, true);
            } catch(NoSuchMethodException e){
            } catch(Exception e){
                throw new RuntimeException(e);
            }
        }
    }

    return super.onMenuOpened(featureId, menu);
}

To be used in conjunction with ActionMode, the only change was to call the method for the Menu that is passed in the callback onCreateActionMode. The code would stand:

@Override
public boolean onCreateActionMode(ActionMode actionMode, Menu menu) {
    MenuInflater inflater = actionMode.getMenuInflater();

    inflater.inflate(R.menu.menu_cab, menu);

    // Reaproveitando o método para habilitar os ícones.
    onMenuOpened(Window.FEATURE_ACTION_BAR, menu);

    return true;
}

Of course I made a simplification to reuse the onMenuOpened, but the idea is to isolate the method and call in both methods, both in the onMenuOpened how much in the onCreateActionMode.

The configuration of MenuItem in xml remains the same:

<item android:id="@+id/menu_alarm"
      android:title="@string/action_alarm"
      android:icon="?attr/ic_action_alarm"
      appcompat:showAsAction="never"
      android:showAsAction="never"
/>

This is the solution that works, but in case in future versions of Android, come to change something in this class (Menu) or if you come to use the Toolbar, this solution will no longer work. Then you will have to adopt some contingency measure.

  • thanks very much worked but when I use action mode it has how to do the same effect?

  • 1

    I didn’t get to test with the ActionMode, I can test if it will work.

  • hum.. blz thank you even I had tested Aki and it didn’t work in reality and because I’m using Fragment and I can’t use this method .

  • 1

    I don’t think it makes sense, the Menu belongs to the Activity, even though the Fragment have menu’s. I tested with the ActionMode and it worked, I’ll include the change in the code.

  • Thank you very much it worked out You really saved me.

2

You can follow the example in this link:

http://developer.android.com/guide/topics/ui/actionbar.html#ActionProvider

XML would look like this:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
    <item android:id="@+id/action_share"
          android:title="@string/share"
          yourapp:showAsAction="ifRoom"
          yourapp:actionProviderClass="android.support.v7.widget.ShareActionProvider"
          />
    ...
</menu>

Code example using Shareactionprovider:

private ShareActionProvider mShareActionProvider;

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main_activity_actions, menu);

    // Set up ShareActionProvider's default share intent
    MenuItem shareItem = menu.findItem(R.id.action_share);
    mShareActionProvider = (ShareActionProvider)
            MenuItemCompat.getActionProvider(shareItem);
    mShareActionProvider.setShareIntent(getDefaultIntent());

    return super.onCreateOptionsMenu(menu);
}

/** Defines a default (dummy) share intent to initialize the action provider.
  * However, as soon as the actual content to be used in the intent
  * is known or changes, you must update the share intent by again calling
  * mShareActionProvider.setShareIntent()
  */
private Intent getDefaultIntent() {
    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setType("image/*");
    return intent;
}
  • this line yourapp:actionProviderClass="android.support.v7.widget.Shareactionprovider" error....

  • 1

    put the error image...

  • @Pedrorangel where this 'yourapp' puts 'android'

  • If not, try replacing yourapp:' with just app:'

  • with android worked, but the error at the time of running q talking does not exist this class.. I put the image in the post

  • Take a look at this link: http://stackoverflow.com/questions/19123543/classcastexception-android-support-v7-widget-shareactionprovider-to-action-view

  • Or this link: http://stackoverflow.com/questions/18120447/classcastexception-android-support-v7-widget-shareactionprovider

  • I’ve done it but it’s not right

Show 3 more comments

0

First add the line below in your menunovo.XML

xmlns:app="http://schemas.android.com/apk/res-auto"

second place the following attribute in your item

app:showAsAction="ifRoom"

beyond its icone.

in my test I did this: my menu2.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.teste.MainActivity" 
xmlns:yourapp="http://schemas.android.com/apk/res-auto"

>

<item
    android:id="@+id/action_settings"
    android:orderInCategory="100"
    android:title="@string/action_settings"
    app:showAsAction="ifRoom"
    android:icon="@drawable/ic_launcher"
    />

 </menu>

my main got like this:

package com.example.teste;

import android.app.Actionbar; import android.os.Bundle; import android.support.v7.app.Actionbaractivity; import android.view.Menu; import android.view.Menuinflater; 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 items for use in the action bar
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main2, menu);
    MenuInflater inflater2 = getMenuInflater();
    inflater2.inflate(R.menu.main, 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();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

}

If you need anything else yell at me

  • which xml attribute makes it look like my post?

  • calm now I understand what you want to do... let me see, already I answer if I get I will do my tests here

  • good vlw for trying to Gradeco

  • looking groceiramente I would take off > before error and take out / of /> after yourapp

  • I edited the answer, candle if it succeeds,anything shouts, I will be here until 17:40 then walk soon huahsuauhsa if not only tomorrow

  • guy I think you don’t understand yet.. type when one has many items.. has overflow aew appears only the names as in the image I posted.. wanted to appear next to the icon..

  • . I had the same problem, I haven’t solved it yet.

  • of good the bad thing is that in android little things(details) sometimes complicates more...

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.