3

I have an MenuItem that is composed of an Image and a textView:

Displays well on 4.3 but on 4.0/4.1 it does not (same issue with ActionBarCompat):

enter image description here

menu_action_cart.xml:

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/actionCart"
    android:actionLayout="@layout/menu_cart_button"
    android:title="@string/action_cart"
    android:showAsAction="always"/>
</menu>

menu_cart_button.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="48dp"
    android:layout_height="fill_parent"
    android:layout_margin="10dp">

        <ImageView
        android:id="@+id/actionIcon"
        android:layout_width="48dp"
        android:layout_height="fill_parent"
        android:clickable="true"
        android:src="@drawable/menu_bag" />


        <TextView
        android:id="@+id/actionbar_notification_text"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center"
        android:paddingTop="10dp"
        android:textColor="@color/red"/>
</RelativeLayout>

Java:

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    new MenuInflater(this).inflate(R.menu.menu_action_cart, menu);

    MenuItem menuItem = menu.findItem(R.id.actionCart);
    View badgeLayout = menuItem.getActionView();
    TextView textView = (TextView) badgeLayout.findViewById(R.id.actionbar_notification_text);
    ImageView imageView = (ImageView) badgeLayout.findViewById(R.id.actionIcon);

    textView.setText("1");
    imageView.setEnabled(true);
    textView.setTextColor(getResources().getColor(R.color.orogo_red));
    imageView.setImageResource(R.drawable.menu_bag);

    return super.onCreateOptionsMenu(menu);
}
nurealam11
  • 537
  • 4
  • 16
Mika
  • 5,807
  • 6
  • 38
  • 83

1 Answers1

1

There may not be an easy solution for this, It's not the first time Android has broken Menu Actions with updates. EX: Action bar icon size in Android 4.2 My suggestion is to find a layout that works for what you need to do in 4.0.3 and based on the android version, adjust the layout programatically.

if (Build.VERSION.SDK_INT <= 15)
{
   //Load alternate Layout
}
Community
  • 1
  • 1
Dave S
  • 3,378
  • 1
  • 20
  • 34