Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android align textview and edittext

In the following xml file.i am trying to align the contents.i.e, first textview followed by the edittext and so on in the next line.But tight now the text is imposed one on another how to resolve this issue.

 <?xml version="1.0" encoding="utf-8"?>
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:orientation="vertical" >


      <RelativeLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_weight="0.70" >



          <TextView
              android:id="@+id/hostname"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_alignBaseline="@+id/ip"
              android:layout_alignParentLeft="true"
              android:text="hostname" />


          <EditText
              android:id="@+id/ip"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_alignParentLeft="true"
              android:layout_alignParentTop="true"
              android:layout_marginLeft="39dp"
              android:ems="10"
              android:inputType="textEmailAddress" >

          </EditText>


   <TextView
              android:id="@+id/username"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_alignBaseline="@+id/ip"
              android:layout_alignBottom="@+id/ip"
              android:layout_alignParentLeft="true"
              android:text="Hostname" />
          <EditText
              android:id="@+id/username"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_alignLeft="@+id/editText1"
              android:layout_below="@+id/ip"
              android:layout_marginLeft="45dp"
              android:layout_marginTop="42dp"
              android:ems="10" />

           <TextView
              android:id="@+id/password"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_alignBaseline="@+id/hostname"
              android:layout_alignBottom="@+id/username"
              android:layout_alignParentLeft="true"
              android:text="Password" />
          <EditText
              android:id="@+id/password"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_below="@+id/password"
              android:layout_centerHorizontal="true"
              android:layout_marginTop="47dp"
              android:ems="10"
              android:inputType="textPassword" />


      </RelativeLayout>

  </LinearLayout>
like image 207
Rajeev Avatar asked Dec 27 '22 18:12

Rajeev


1 Answers

Another way to do it, that worked well for me, is, in your xml file, to add a parent <LinearLayout> that specifies android:orientation="horizontal" to encompass your TextView and EditText.

This parent LinearLayout has to be inside your very first LinearLayout that has to specify a android:orientation="vertical" :

<!-- Root Layout -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" <!-- Vertical orientation -->
    android:paddingLeft="15sp"
    android:paddingRight="15sp"
    android:paddingTop="15sp"
    android:paddingBottom="15sp" >


    <!-- First Row -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" <!-- Horizontal orientation for the row -->
        android:paddingTop="15sp">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Poids :"
            android:textSize="15sp"
            android:textStyle="bold" />
        <EditText
            android:id="@+id/btn_poids"
            android:inputType="numberDecimal"
            android:hint="Entrez votre poids"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>


    <!-- Second Row -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" <!-- Horizontal orientation for the row -->
        android:paddingTop="15sp">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Taille :"
            android:textSize="15sp"
            android:textStyle="bold" />
        <EditText
            android:id="@+id/btn_poids"
            android:inputType="numberDecimal"
            android:hint="Entrez votre taille"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>


</LinearLayout>

Here's the result (the warning signs are because of the hardcoded strings for the hints and text...) :

Result of the above

like image 156
Pioute Avatar answered Jan 09 '23 06:01

Pioute