Basically we use toolbar menu with icon, sometimes we also use icon/text together. Icon along with Text show horizontally like this image below
In normal case it happens but I want to show the menu icon/text vertically like the image below
I have done the xml layout with this xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:orientation="horizontal"
android:padding="@dimen/default_margin">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="@drawable/bg_timeline"
android:layout_weight="1"
android:gravity="center"
android:text="@string/label_timeline" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/default_margin"
android:drawableTop="@drawable/ic_about_large"
android:layout_weight="1"
android:gravity="center"
android:text="@string/label_about" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="@drawable/ic_achivement_active"
android:layout_marginLeft="@dimen/default_margin"
android:layout_weight="1"
android:gravity="center"
android:text="@string/label_achievements" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="@drawable/ic_more"
android:background="?selectableItemBackground"
android:gravity="center"
android:layout_marginLeft="@dimen/default_margin"
android:layout_weight="1"
android:text="@string/label_more" />
</LinearLayout>
My question is how can I implement a toolbar/actionbar when menu item placed like Text below each icon as a whole. Is there anyway to do this?
You can use a toolbar method inflateMenu to inflate a custom menu layout. Below is a small snippet:
<?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/your_id" android:title="@string/your_string" android:icon="@drawable/your_menu_icon" app:actionLayout="@layout/your_custom_menu_layout" app:showAsAction = "always" /> </menu>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical" android:paddingRight="10dp"> <ImageView android:layout_width="30dp" android:layout_height="30dp" android:gravity="center" android:paddingLeft="5dp" android:paddingRight="5dp" android:src="@drawable/your_menu_icon" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:paddingLeft="0dp" android:text="your_text" android:textColor="@color/white" android:textSize="9dp" /> </LinearLayout>
your_toolbar.inflateMenu(R.menu.your_menu);
Menu menu = your_toolbar.getMenu();
View your_menu_view = menu.findItem(R.id.your_id).getActionView(); your_menu_view.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { } });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With