-1

Well, I have a LinearLayout with two children, an EditText and a Button and they both have a weightSum of 3. All I want is to have a horizontal line with 2 layout_weight EditText and 1 layout_weight Button. Here is my code:

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:weightSum="3">

    <EditText
        android:id="@+id/edit1"
        android:inputType="textAutoComplete"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="2"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/DBLocations"
        android:layout_weight="1"/>
</LinearLayout>

But weights aren't working, here is the result: screenshot of the wrong result What I have to do in order to have 2 layout_weight in EditText and 1 for the Button?

ArgonOne
  • 11
  • 1
  • 9

2 Answers2

5

You should set android:orientation="horizontal" in your XML code .

And add android:layout_width="0dp" in your Button and EditText .

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal
    android:weightSum="3">

<EditText
    android:id="@+id/edit1"
    android:inputType="textAutoComplete"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="2"/>
<Button
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:text="@string/DBLocations"
    android:layout_weight="1"/>
</LinearLayout>
KeLiuyue
  • 8,149
  • 4
  • 25
  • 42
0

Make 'layout_width' as '0dp' for Button and set orientation for LinearLayout as horizontal as shown

<LinearLayout android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal 
    android:weightSum="3"> 

    <EditText android:id="@+id/edit1" 
        android:inputType="textAutoComplete" 
        android:layout_width="0dp" 
        android:layout_height="wrap_content" 
        android:layout_weight="2"/> 

    <Button android:layout_width="0dp" 
        android:layout_height="wrap_content" 
        android:text="@string/DBLocations" 
        android:layout_weight="1"/> 
</LinearLayout>
Mahesh Babariya
  • 4,560
  • 6
  • 39
  • 54
Sujeet
  • 1
  • 2