Skip to content Skip to sidebar Skip to footer

Android Edittext Align Text To Right And Label To Left

I want to have an edit text where a label 'Name' is displayed on left side of the edit text and the input is enterd from Right to left If i set android:gravity as right , input is

Solution 1:

take a linear layout with horizontal orientation and add two Linear Layout with vertical orientation the first layout will have "Left gravity" and Name label and the other one will have Edit box and "Right Gravity".

Solution 2:

Use Table Layout

<TableLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"android:stretchColumns="1" ><TableRowandroid:layout_marginTop="20dp" ><TextViewandroid:id="@+id/textView1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/lblUser" /><EditTextandroid:id="@+id/UserNameField"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_marginRight="30dp"android:layout_x="68dp"android:layout_y="40dip"android:inputType="textEmailAddress"android:lines="1"android:maxLines="1"android:maxWidth="150dip"android:minWidth="230dip" ></EditText></TableRow></TableLayout>

Solution 3:

I don't know everything about android but still I am thinking this is not possible the way you are asking cause You want to set Gravity in 1 single EditText ,different for Different Words(name to left and text to right)

My idea to get the same result is,

1) Ask your designer to make you one White Round Corner Rectangle of Screen width and your desired height,and one another same image with Orange border (as you see while selecting any edittext)

2) Now ,In xml file take one <LinearLayout> ,set that first image as background to this layout and in that layout Take 1 TextView(for Name:) and EditText(to input) and set TextView - EditText background color to Transparent,ohh and yes give right gravity to EditText as you wished

3) Write one xml selector file for EditText background and instead of default orange,make focused bg to transparent.(so no one will recognise that this edittext is inside a layout when focused)

4) and write selector xml for LinearLayout and set focused background drawable to your second image.

And you will see on screen what you wanted.

Notes: If this is your client's design then Its Ok,If yours own then I am not in favour of you. and please don't ask me to write example.

Post a Comment for "Android Edittext Align Text To Right And Label To Left"