How to show icon in action Bar when item is in overflow?

2

I wanted the called item to appear on your side as well:

Theresultyou'dexpectwouldbethis:

Activity code with the help of @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 have already used android: actionProviderClass="android.support.v7.widget.ShareActionProvider" and it is not working either.

Error Log:

    
asked by anonymous 23.10.2014 / 12:09

3 answers

3

If the idea is to have icons in the Overflow Menu and not% with% menus then you need to use ActionProvider to solve, since no icons are displayed by default.

In this case, just implement the Reflection method on your onMenuOpened and call the Activity method, which is private setOptionalIconsVisible Menu do Reflection ':

@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 use in conjunction with por , the only change was to call the method for the ActionMode that is passed in the Menu callback. The code would look like:

@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 did a simplification to reuse onCreateActionMode , but the idea is to isolate the method and call in both methods, both onMenuOpened and onMenuOpened .

The onCreateActionMode setting 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 future versions of Android, something will change in this class ( MenuItem ) or if you use Menu , this solution will no longer work. There you will have to adopt some one contingency measure.

    
24.10.2014 / 02:37
2

You can follow the example in this link:

link

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

Sample code 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;
}
    
23.10.2014 / 12:48
0

First add the line below in your menunovo.XML

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

Second place the following attribute on your item

app:showAsAction="ifRoom"

besides your icon.

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 looks 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, scream it and

    
23.10.2014 / 20:16