santosh nani
santosh nani

Reputation: 147

How to set text in edittext which should not editable

I am using an EditText displaying TextView on the left. This shouldn't be editable.

My Question is how to put TextView in an EditText? How it is possible?

Actually I want like this:

TextView in EditeText

Below is xml code.

<EditText
        android:textAlignment="textEnd"
        android:text="[email protected]"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="*Mobile number"
        android:inputType="number"
        android:gravity="end" />

Upvotes: 2

Views: 2238

Answers (3)

MrVasilev
MrVasilev

Reputation: 1563

I am totally agree with @Nilesh Rathod just want to propose that to move this LinearLayout in a separate file and reuse it in multiple places like Fragment

Upvotes: 1

Rishav Singla
Rishav Singla

Reputation: 483

Make your edittext non-editable using below properties:

android:inputType="none"

if you want to do it programmatically

EditText editText= (EditText) findViewById(R.id.yourid);
editText.setEnabled(false);
editText.setKeyListener(null);

Upvotes: 1

AskNilesh
AskNilesh

Reputation: 69709

My Question is how to put TextView in EditText?

Not Possible, because This is not Built in behavior of EditText

How is it possible?

You can use a TextView and EditText in a LinearLayout with android:orientation="horizontal"

SAMPLE CODE

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="?attr/colorBackgroundFloating"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:gravity="center"
        android:orientation="horizontal">

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:padding="5dp"
            android:text="Enter Name" />

        <EditText
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="right"
            android:text="123"
            android:padding="5dp" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:gravity="center"
        android:orientation="horizontal">

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:padding="5dp"
            android:text="Enter Name" />

        <EditText
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="right"
            android:text="123"
            android:padding="5dp" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:gravity="center"
        android:orientation="horizontal">

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:padding="5dp"
            android:text="Enter Name" />

        <EditText
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="right"
            android:text="123"
            android:padding="5dp" />

    </LinearLayout>

</LinearLayout>

OUTPUT

enter image description here

Upvotes: 0

Related Questions