Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to show text below icon in toolbar menu?

Basically we use toolbar menu with icon, sometimes we also use icon/text together. Icon along with Text show horizontally like this image below

enter image description here

In normal case it happens but I want to show the menu icon/text vertically like the image below

enter image description here

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?

like image 860
androidcodehunter Avatar asked Mar 24 '16 11:03

androidcodehunter


1 Answers

You can use a toolbar method inflateMenu to inflate a custom menu layout. Below is a small snippet:

  1. Define your custom menu your_menu inside the menu folder:
<?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>
  1. Now define the your_custom_menu_layout:
<?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>
  1. Thirdly, inflate your menu item in your java code using your toolbar:

your_toolbar.inflateMenu(R.menu.your_menu);

  1. Reference and click listeners:

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) { } });

like image 181
Kaveesh Kanwal Avatar answered Oct 14 '22 03:10

Kaveesh Kanwal