Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EditText Label?

I want to have some layout a bit like this

[text tabel][edittext] 

i cant find an attribute like html esque label. It seems to be the case that i need to use

[TextView][EditText] 

but i cant get them to go on the same line this is my xml file.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content">" <TextView     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:text="@string/boat_1"/> <EditText     android:id="@+id/entry"     android:hint="@string/IRC"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:background="@android:drawable/editbox_background"/> <TextView     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:text="@string/boat_2"/> <EditText     android:id="@+id/entry"     android:hint="@string/IRC"     android:minWidth="100dip"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:background="@android:drawable/editbox_background"/> <Button android:id="@+id/close"     android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:layout_alignParentBottom="true"     android:text="@string/title_close" /> 

like image 235
Alex Edwards Avatar asked Sep 18 '10 13:09

Alex Edwards


People also ask

What is label in android?

Editable items in an app allow users to enter text. Each editable item should have a descriptive label stating its purpose. Android offers several ways for developers to label Views in an app's user interface. For editable items in an interface, some of these ways of labeling can improve accessibility.

What is TextInputLayout?

TextInputLayout is a view container that is used to add more features to an EditText. It acts as a wrapper for EditText and has some features like: Floating hint. Animation that can be disabled or enabled. Error labels that display error messages when an error occurs.

What is EditText?

A EditText is an overlay over TextView that configures itself to be editable. It is the predefined subclass of TextView that includes rich editing capabilities.


2 Answers

You have basically two options:

Option 1: Use nested LinearLayouts:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:orientation="vertical"  android:layout_width="fill_parent"  android:layout_height="wrap_content">  <LinearLayout android:orientation="horizontal"  android:layout_width="fill_parent"  android:layout_height="wrap_content"> <TextView      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:text="@string/boat_1"/>  <EditText      android:id="@+id/entry"      android:hint="@string/IRC"      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:background="@android:drawable/editbox_background"/>  </LinearLayout> <LinearLayout android:orientation="horizontal"  android:layout_width="fill_parent"  android:layout_height="wrap_content"> <TextView      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:text="@string/boat_2"/>  <EditText      android:id="@+id/entry"      android:hint="@string/IRC"      android:minWidth="100dip"      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:background="@android:drawable/editbox_background"/>  </LinearLayout> <Button android:id="@+id/close"      android:layout_width="fill_parent"      android:layout_height="wrap_content"      android:layout_alignParentBottom="true"      android:text="@string/title_close" />  

Notice that I'm using android:orientation="horizontal" for those nested layouts.

Option 2: you can use another content manager, like RelativeLayout.

The advantage of this, is that you can avoid nesting, thus your layout will be easier to read/maintain/inflate. This is a brief example:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"      android:orientation="vertical"      android:layout_width="fill_parent"      android:layout_height="wrap_content">     <TextView          android:id="@+id/text_view_boat1"         android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:text="@string/boat_1"         android:layout_alignParentLeft="true"         android:layout_alignParentTop="true"/>      <EditText          android:id="@+id/entry"          android:hint="@string/IRC"          android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:background="@android:drawable/editbox_background"         android:layout_toRightOf="@+id/text_view_boat1"         android:layout_alignParentTop="true"/>      <TextView          android:id="@+id/text_view_boat2"         android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:text="@string/boat_2"         android:layout_alignParentLeft="true"         android:layout_below="@id/text_view_boat1"/>      <EditText          android:id="@+id/entry2"          android:hint="@string/IRC"          android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:background="@android:drawable/editbox_background"         android:layout_toRightOf="@+id/text_view_boat2"         android:layout_below="@id/entry"/>  </RelativeLayout> 
like image 157
Cristian Avatar answered Sep 21 '22 14:09

Cristian


The design support library has the TextInputLayout which animates the hint text turning it into a label when the user starts typing and can also show a red error message.

https://developer.android.com/reference/android/support/design/widget/TextInputLayout.html

like image 42
miguel Avatar answered Sep 22 '22 14:09

miguel