2

I am trying to stretch the EditText (on ActionBar) to "almost" entire screen's width.It should first leave space for a button to the right then the rest is EditText's pace. I can put about 300dp which could make it seem nice on my phone but it's not a preferable solution.

My code: action_menu

<item
    android:id="@+id/action_settings"
    android:actionLayout="@layout/action_item_edit_text"
    android:showAsAction="always"
    android:title="Search"/>
<item
    android:id="@+id/add"
    android:icon="@drawable/add"
    android:showAsAction="always"
    android:title="Add"/>

<!-- Settings, should always be in the overflow -->

action_item_edit_text

<EditText xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/editInput"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:hint="Write something"
android:padding="6dp"
android:singleLine="true"
android:textColor="@android:color/black"
android:textColorHint="#BBBBBB"
android:textSize="14sp" />    
Toan Le
  • 412
  • 1
  • 6
  • 17
  • See http://stackoverflow.com/questions/6316540/how-to-make-layout-with-view-fill-the-remaining-space . IMO the proper solution is layout_weight. The view that should take fixed space gets a weight of 0, the view that should fill remaining space gets a weight of 1. – Daniel Lubarov Dec 09 '14 at 01:39

1 Answers1

1
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <Button 
        android:id="@+id/button"
        android:layout_alignParentRight="true"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content" />

    <EditText
        android:layout_toLeftOf="@id/button"
        android:layout_alignParentLeft="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</RelativeLayout>

Something like that. I haven't tested this I just wrote it blindly, but the concept is sound.

James Lockhart
  • 1,030
  • 11
  • 17
  • action_menu only contain items so I am not very sure I should add a LinearLayout to it. It contains add button on the right and search field on left (which is in another XML file). I am trying to stretch the search "item" to entire ActionBar. – Toan Le Dec 09 '14 at 02:44
  • For menu items you can set layouts by using the android:actionLayout="@layout/my_layout" attribute – James Lockhart Dec 09 '14 at 09:30
  • I've already did that. However, I am not very sure but I think an item can only link to 1 object. What I wanted was the 1st item (after leaving space for 2nd item on the right) would stretch all the way to the left. I solved the problem by adding collapseActionView to the first item. Upon clicking, it will open the EditText and stretch all the way left. – Toan Le Dec 10 '14 at 01:33