3

enter image description here

I want to display text at the end of EditText as shown in the image above for URLName.How could I acheive this ?The text cannot be edited.I am using this editText inside TextInputLayout from the design library. thanks in advance

Nabeel K
  • 5,938
  • 11
  • 38
  • 68

2 Answers2

3
  • Create a LinearLayout. An example of a row inside this layout would be a title (i.e. URL name) or a text entry field.

  • Set the background of the LinearLayout for the current row to a custom drawable to create a thin red line. Follow the answers on this post for ideas.

  • For the URL entry field mentioned in your question, create a TextView and an EditText inside another LinearLayout inside the current row.

The TextView will contain ".sitename.com". Set it to wrap_content and give it a weight of 0. For your EditText, set its width to 0dp and its weight to 1. It should now fill all remaining space. Set the background to @null for both.

Community
  • 1
  • 1
PPartisan
  • 8,173
  • 4
  • 29
  • 48
  • I want to make use of latest TextInputLayout for this EditText. – Nabeel K Nov 02 '15 at 10:53
  • This is normal method right ? Apart from the above solution is there a way I could achieve this ? – Nabeel K Nov 02 '15 at 10:55
  • @beardedbeast I've never used it, but isn't `TextInputLayout` interchangeable with `EditText`? – PPartisan Nov 02 '15 at 10:55
  • @beardedbeast You could also, possibly, achieve it with `TextWatcher`, but given that you want one element off to the right and another to the left, this is a more straightforward and robust approach, as near as I can tell. – PPartisan Nov 02 '15 at 10:56
  • no its just a layout inside which we give the editText. – Nabeel K Nov 02 '15 at 10:57
  • @beardedbeast Sure, so you should be able to just apply anything said above for `EditText` to your `TextInputLayout` wrapper. – PPartisan Nov 02 '15 at 13:57
  • but the problem here is animation for hint is not working now – Nabeel K Nov 02 '15 at 14:03
  • @beardedbeast Just because it's inside a `LinearLayout`? This is a common situation, so I'm surprised. Under what conditions does the label animate correctly? You could swap the `LinearLayout` on that row for a `RelativeLayout`, for instance. – PPartisan Nov 02 '15 at 14:13
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/93998/discussion-between-ppartisan-and-bearded-beast). – PPartisan Nov 02 '15 at 14:57
2

This worked for me :

<RelativeLayout

        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
    <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text=""
            android:layout_alignParentLeft="true"
            android:id="@+id/youeeditid"
            android:layout_gravity="center"
            />
    <TextView
            android:id="@+id/text_hint"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:text="HintText"
            android:textSize="18sp"
            android:layout_marginRight="10dp"
            android:textColor="#808080"
            />
</RelativeLayout>

It's little bit tricky but it's works for me.

BUT

If you want to set only EditText then you can do below :

1) Set Gravity of EditText to "Right" and Ellipsize to "End" .

2) Now In onCreate() method write onclicklistener of EditText and set Gravity as "Left".

3) Or you can also set programatically on OnKeyListener , where check if lenght of edittext is equal to Zero set EditText's gravity as Left.

Reference : Setting cursor to the right on an EditText with HINT Gravity to center

Community
  • 1
  • 1
KishuDroid
  • 5,411
  • 4
  • 30
  • 47