2

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

Aman Gupta - ΔMΔN
  • 2,971
  • 2
  • 19
  • 39
  • Try this [first](https://stackoverflow.com/questions/38373481/remove-large-padding-after-hamburger-menu-icon-in-android-toolbar) and [second](https://stackoverflow.com/questions/9737101/padding-between-actionbars-home-icon-and-title) solutions! – Kishore Jethava Jun 20 '17 at 13:53

1 Answers1

2

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.

aastha gupta
  • 121
  • 1
  • 11