I want to put icon in my menu item but icon is not showing i tried app:showAsAction="always"
this but icon is showing on toolbar.But i want show icon with text.
And here is my code:-
<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_search"
android:title="@string/action_search"
android:orderInCategory="1"
android:icon="@drawable/search"
app:showAsAction="ifRoom" />
<item
android:id="@+id/action_cart"
android:title="@string/action_search"
android:orderInCategory="2"
android:icon="@drawable/shoppingcart"
android:actionLayout="@layout/feed_update_count"
app:showAsAction="ifRoom" />
<item
android:id="@+id/login"
android:title="@string/login"
android:orderInCategory="1"
android:icon="@drawable/ic"
/>
<item
android:id="@+id/my_order"
android:title="@string/my_order"
android:orderInCategory="2"
android:icon="@drawable/ic"/>
<item
android:id="@+id/wishlist"
android:title="@string/wishlist"
android:orderInCategory="3"
android:icon="@drawable/ic"/>
</menu>
Here My Code:-
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
LayoutInflater inflater = LayoutInflater.from(this);
View count = inflater.inflate(R.layout.feed_update_count, null);
notifCount = (Button) count.findViewById(R.id.notif_count);
notifCount.setText(String.valueOf(mNotifCount));
return super.onCreateOptionsMenu(menu);*/
getMenuInflater().inflate(R.menu.menu_main, menu);
MenuItem menuItem = menu.findItem(R.id.action_cart);
menuItem.setIcon(buildCounterDrawable(count, R.drawable.shoppingcart));
return true;
}
private Drawable buildCounterDrawable(int count, int backgroundImageId) {
LayoutInflater inflater = LayoutInflater.from(this);
View view = inflater.inflate(R.layout.feed_update_count, null);
view.setBackgroundResource(backgroundImageId);
if (count == 0) {
View counterTextPanel = view.findViewById(R.id.counterValuePanel);
counterTextPanel.setVisibility(View.GONE);
} else {
TextView textView = (TextView) view.findViewById(R.id.count);
textView.setText("" + count);
}
view.measure(
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
view.setDrawingCacheEnabled(true);
view.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
view.setDrawingCacheEnabled(false);
return new BitmapDrawable(getResources(), bitmap);
}
private void doIncrease() {
count++;
invalidateOptionsMenu();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case R.id.action_search:
Intent search = new Intent(MainActivity.this,SearchActivity.class);
startActivity(search);
return true;
case R.id.login:
if(session.checkLogin())
finish();
else {
Intent intent = new Intent(MainActivity.this,MyAccount.class);
startActivity(intent);
}
return true;
case R.id.my_order:
alert.showAlertDialog(MainActivity.this,"Alert","This Page Under Maintenance",false);
return true;
case R.id.wishlist:
if(session.checkLogin()){
finish();
}
else {
Intent wishlistIntent = new Intent(MainActivity.this,WishList.class);
startActivity(wishlistIntent);
}
return true;
case android.R.id.home:
Intent intent = new Intent(MainActivity.this,MainActivity.class);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
As Example:- My Icon show on left of MyAccount.
Any One can help me.For Solved this error.Thanks in advance!
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…