p.tanaev
p.tanaev

Reputation: 53

Put TextView inside EditText

Please tell me, how i can to put text inside a EditText like this

image

And only right text can be edit.

Upvotes: 4

Views: 11399

Answers (6)

Silambarasan Poonguti
Silambarasan Poonguti

Reputation: 9442

Try this...

Layout:

<FrameLayout
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:layout_marginEnd="8dp"
     android:layout_marginStart="8dp" >

        <EditText
            android:id="@+id/editText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="@string/mobile"
            android:inputType="phone"
            android:background="@drawable/some_edittext_background"
            android:labelFor="@+id/editText"
            android:maxLength="9"
            android:paddingBottom="8dp"
            android:paddingStart="48dp"
            android:paddingEnd="8dp"
            android:paddingTop="8dp" >
                <requestFocus />
            </EditText>

        <TextView
            android:id="@+id/prefix"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:paddingStart="8dp"
            android:text="+971" />
</FrameLayout>

Result:

Enable

Focus

Upvotes: 4

Pragnani
Pragnani

Reputation: 20155

Keep TextView and EditText inside Relative Layout and Adjust them according to your requirement.

Example:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <EditText
        android:id="@+id/Edittext"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="20dp"
        android:ems="10"
        android:hint="To my first character" >

        <requestFocus />
    </EditText>

    <TextView
        android:id="@+id/text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/Edittext"
        android:layout_alignBottom="@+id/Edittext"
        android:layout_alignParentRight="true"
        android:text="123123" />

</RelativeLayout>

Change it if you have any changes

Upvotes: 9

ben75
ben75

Reputation: 28706

You need to combine 3 components:

  • a container : use a RelativeLayout since it allows views overlapping.
  • a TextView align to the right edge of the RelativeLayout (width = wrap_content)
  • an EditText taking the full width of the RelativeLayout (width = match_parent)

To put a text in the EditText: myEditText.setText("myText")

Upvotes: 1

SceLus
SceLus

Reputation: 1107

EditText editText = (EditText)findViewById(R.id.edit_text);
editText.setText("Количество", TextView.BufferType.EDITABLE);

Upvotes: 3

Liquid Ink
Liquid Ink

Reputation: 521

Im assuming your asking how to put text inside a TextView, programatiacally it is done this way

textView.setText("anything you want..."); //enter a string into parameters

where textView is a instance of the TextView class.

Upvotes: -2

Droidman
Droidman

Reputation: 11608

why not just setting the text using setText() and keeping the left part constant?

Upvotes: 0

Related Questions