Skip to content

Instantly share code, notes, and snippets.

@Kolyall
Last active November 27, 2018 05:23
Show Gist options
  • Save Kolyall/feb357db2afdd1b21198ffad4cfa6fd8 to your computer and use it in GitHub Desktop.
Save Kolyall/feb357db2afdd1b21198ffad4cfa6fd8 to your computer and use it in GitHub Desktop.
Custom view in menuItem

Custom view in menuItem

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:background="@color/white">
<ImageView
android:id="@+id/cartImage"
android:padding="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_catalog" />
<com.octanner.android.performance.view.CartCountView
android:id="@+id/cart_count_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="@+id/cartImage"
android:layout_alignTop="@+id/cartImage"
android:gravity="center"
android:padding="1dp"
android:layout_margin="10dp"
android:textColor="@color/white"
android:textSize="@dimen/cart_menu_value_text_size"
android:visibility="gone"
tools:text="1"
tools:visibility="visible" />
</RelativeLayout>
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
setHasOptionsMenu(true);
}
MenuItem menuItemCart;
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
if (menuItemCart == null) {
inflater.inflate(R.menu.menu_catalog_body, menu);
menuItemCart = menu.findItem(R.id.menu_item_cart);
View cartView = menuItemCart.getActionView();
mCartImage = (ImageView) cartView.findViewById(R.id.cartImage);
mCartCountView = (CartCountView) cartView.findViewById(R.id.cart_count_view);
cartView.setOnClickListener(v -> {
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/menu_item_cart"
android:title="@string/cart"
app:actionLayout="@layout/item_cart"
app:showAsAction="always" />
</menu>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment