Aman Gupta -  ΔMΔN
Aman Gupta - ΔMΔN

Reputation: 3007

How to increase the menuitem width in android while using Actionbar

I have a problem to increase the width of MenuItem in Action bar, it take the fixed width. Below is screen shot,

enter image description here

and want to achieve the below one,

enter image description here

Below is the code:

search_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:background="@drawable/search_view_background"
    android:layout_width="match_parent"
    android:layout_marginTop="10dp"
    android:layout_marginBottom="10dp"
    android:layout_height="wrap_content">
    <TextView android:layout_width="0dp"
    android:padding="10dp"
    android:layout_weight="1"
    android:text="Search Product"
    android:textColor="@color/grey"
    android:gravity="center_vertical"
    android:drawableEnd="@drawable/ic_search"
    android:drawableRight="@drawable/ic_search"
    android:drawablePadding="20dp"
    android:textSize="@dimen/text_size_14sp"
    android:layout_height="match_parent"/>
</LinearLayout>

home_menu.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<item
android:id="@+id/search"
android:onClick="onClickSearchButton"
android:orderInCategory="1"
android:title="@string/Search"
app:actionLayout="@layout/search_layout"
app:showAsAction="always"/>
<item
android:id="@+id/action_cart"
android:icon="@drawable/ic_menu_cart"
android:orderInCategory="2"
android:title="@string/action_cart"
app:showAsAction="always"/>

<item
android:id="@+id/overflow"
android:icon="@drawable/ic_menu_notifications"
android:orderInCategory="3"
android:title="@string/overFlowMenu"
android:visible="false"
app:showAsAction="always"/>

Please give your suggestion, how to fix this problem.

Thanks

Upvotes: 2

Views: 3380

Answers (1)

aastha gupta
aastha gupta

Reputation: 121

Did it with a Hack.

I know it's not the best or even good solution but I did it with an hack.

searchItem.getActionView().findViewById(R.id.text).setMinimumWidth(width);

You can pass a particular width to the view so that it will take at-least that much space.

Upvotes: 2

Related Questions