2

I want to place a TextView and an Editext in same line but it may looks like TextView is placing above EditText. I've tried all solutions on SO but it didn't work for me exactly.

enter image description here

Here is my code

       <RelativeLayout
        android:id="@+id/view2"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <TextView
            android:id="@+id/from"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:background="#ffffff"
            android:text="From"
            android:textColor="#000fff"
            android:textStyle="bold" />

        <EditText
            android:id="@+id/input"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:gravity="right"
            android:inputType="number"
            android:visibility="invisible" />
  </RelativeLayout>

It places the EditText above TextView.

Ibungo
  • 1,137
  • 12
  • 23
gbl
  • 178
  • 2
  • 12

4 Answers4

1

Try using LinearLayout with horizontal orientation

<LinearLayout
        android:id="@+id/view2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/from"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:text="From"
            android:textColor="#000fff"
            android:textStyle="bold" />

        <EditText
            android:id="@+id/input"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:gravity="right"
            android:inputType="number"
             />
  </LinearLayout>
Manishika
  • 5,478
  • 2
  • 22
  • 28
1

You can make use of the android:layout_weight. You can view example here. The android:layout_weight works only in Linearlayout. You can also define a LinearLayout inside the RelativeLayout you've defined.

<LinearLayout
    android:id="@+id/view2"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/from"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight=".60"
        android:layout_alignParentTop="true"
        android:background="#ffffff"
        android:text="From"
        android:textColor="#000fff"
        android:textStyle="bold" />

    <EditText
        android:id="@+id/input"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight=".40"
        android:layout_alignParentTop="true"
        android:gravity="right"
        android:inputType="number"
        android:visibility="invisible" />
</LinearLayout>
Community
  • 1
  • 1
Ibungo
  • 1,137
  • 12
  • 23
0

Can you check this solution

    <TextView
        android:id="@+id/from"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="From"
        android:textColor="#000fff"
        android:textStyle="bold" />

    <EditText
        android:id="@+id/input"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:gravity="right"
        android:inputType="number"
        android:textColor="#ffffff"
        android:text="sdjgsjgjb,,b,bm,m,sjksjkjjhks"
        android:layout_toRightOf="@+id/from"
       />

diordna
  • 550
  • 5
  • 14
0

its because you have given property fill_parent to edit text,update it as this:

<EditText
        android:id="@+id/input"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:gravity="right"
        android:inputType="number"
        android:visibility="invisible"
        android:layout_toRightOf="@id/from" />
Hamad
  • 5,096
  • 13
  • 37
  • 65