How to Change ShareActionProvider Icon?
from the CommonsWare Community archivesAt September 4, 2018, 8:34am, hackzcorporation asked:
I’m adding a ShareActionProvider: note_menu.xml
<item
android:title="@string/share"
android:id="@+id/share"
android:icon="@drawable/share"
app:actionProviderClass="android.support.v7.widget.ShareActionProvider"
app:showAsAction="always" />
onCreateOptionsMenu.xml
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.note_menu,menu);
MenuItem menuItem= menu.findItem(R.id.share);
ShareActionProvider shareActionProvider= (ShareActionProvider) MenuItemCompat.getActionProvider(menuItem);
Intent intent= new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
shareActionProvider.setShareIntent(intent);
return super.onCreateOptionsMenu(menu);
}
I have supplied a white share icon using android:icon="drawable/share"
in my note_menu.xml but the icon didn’t seems to change and it shows the default black icon when I’m running the app on emulator.
One more question why these code are not working on ShareActionProvider(Both of them returning null o fragment while working on Activity):
ShareActionProvider shareActionProvider= (ShareActionProvider) MenuItemCompat.getActionProvider(menuItem);
or
ShareActionProvider shareActionProvider= (ShareActionProvider) menu.findItem(R.id.share).getActionProvider();
This is working on Fragment(It works but I don’t know why, needs some explanation):
ShareActionProvider shareActionProvider= new ShareActionProvider(getActivity());
Intent intent= new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
shareActionProvider.setShareIntent(intent);
At September 4, 2018, 11:07am, mmurphy replied:
I do not think that an <item>
with app:actionProviderClass
uses the android:icon
. I think that the action provider draws its own icon. Your results suggest that you have a theme issue, where ShareActionProvider
thinks that you have a light action bar (so it should render itself in a dark color).
why these code are not working on ShareActionProvider
I have no idea, sorry.
At September 4, 2018, 11:09am, hackzcorporation replied:
@mmurphy
I have solved this issue making a custom ShareActionProvider:
public class CusShareActionProvider extends ShareActionProvider {
private final Context mContext;
public CusShareActionProvider(Context context) {
super(context);
this.mContext=context;
}
@Override
public View onCreateActionView() {
View view = super.onCreateActionView();
if (view != null)
{
try
{
Drawable icon = mContext.getResources().getDrawable(R.drawable.share);
Method method = view.getClass().getMethod("setExpandActivityOverflowButtonDrawable", Drawable.class);
method.invoke(view, icon);
}
catch (Exception e)
{
Log.e("MyShareActionProvider", "onCreateActionView", e);
}
}
return view;
}
}
But I’m facing problem changing the icon of Action Overflow, Is there any way to change the Icon of ACtion Overflow or Overflow Menu?
At September 4, 2018, 11:21am, mmurphy replied:
No, you have implemented an ugly unreliable hack.
But I’m facing problem changing the icon of Action Overflow, Is there any way to change the Icon of ACtion Overflow or Overflow Menu?
Fix your theme to be correct, as I suggested in my earlier answer. If you want light icons, use a .DarkActionBar
theme.
At September 4, 2018, 11:21am, hackzcorporation replied:
@mmurphy
I tried using themes but that only works on API 21 and above
At September 4, 2018, 11:22am, mmurphy replied:
Having a theme that ends in .DarkActionBar
is available for API Level 11 and higher. This will give you white text and white icons for your action bar.
At September 4, 2018, 11:23am, hackzcorporation replied:
So what to do, Is there anything else?
At September 4, 2018, 11:23am, hackzcorporation replied:
yes this helps me
At September 4, 2018, 11:35am, hackzcorporation replied:
toolbar.setOverflowIcon(getResources().getDrawable(R.drawable.custom_icon));
Using Toolbar instead of Actionbar and calling method .setOverflowicon()
does the job and it works fine on all API levels…