-2

How can we embed an input text with a TextView in Android?

For example, I want to implement this:

"Your Name is ___ , This is the Level ____"

The "___" is the blank space which the user must fill with a specified input and I'll check it for validation.

Please note that it is different from android:hint.

How can I implement this?

Adinia
  • 3,722
  • 5
  • 40
  • 58
Shahin Ghasemi
  • 1,600
  • 1
  • 20
  • 38
  • 1
    what have you tried so far ? any code ? – Umair Aug 25 '17 at 10:40
  • I have a idea about set Textview and EditText horizontally , but i think this is not the solution @Umair – Shahin Ghasemi Aug 25 '17 at 10:41
  • 2
    it can be done with edittext but you have to do alot of work. So the good approach is to use textviews and edittext horizontally. – Umair Aug 25 '17 at 10:45
  • can you let me see your approach with edittext alonely @Umair – Shahin Ghasemi Aug 25 '17 at 10:48
  • Make a custom textView https://stackoverflow.com/questions/9477336/how-to-make-a-custom-textview – Red_Spark Aug 25 '17 at 11:12
  • Possible duplicate of [Put constant text inside EditText which should be non-editable - Android](https://stackoverflow.com/questions/14195207/put-constant-text-inside-edittext-which-should-be-non-editable-android) – Adinia Aug 25 '17 at 12:28

2 Answers2

0

You can try like this

     <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="30dp"
        android:textSize="16sp"
        android:textColor="#000000"
        android:text="Your Name is"
        android:id="@+id/textview1" />

    <EditText
        android:layout_width="60dp"
        android:textSize="16sp"
        android:layout_height="40dp"
        android:textColor="#000000"
        android:id="@+id/edittext1"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="30dp"
        android:textSize="16sp"
        android:textColor="#000000"
        android:text=", This is the Level"
        android:id="@+id/textview2" />

    <EditText
        android:layout_width="60dp"
        android:textSize="16sp"
        android:layout_height="40dp"
        android:textColor="#000000"
        android:id="@+id/edittext2"/>

</LinearLayout>
MKovac
  • 176
  • 1
  • 8
  • You can get text from edittext with EditText editText = (EditText) findViewById(R.id.edittext1); String string = editText.getText().toString(); – MKovac Aug 25 '17 at 10:53
  • thanks , but there is no other solution ? because i have a lot of inputting , and i think this makes me tired !!! :) this can not be done just using edittext ? – Shahin Ghasemi Aug 25 '17 at 10:54
0

You could make a fill in the blanks type format using constant text inside EditText. Do this by creating an EditText in XML.

<EditText
    android:id="@+id/editText"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"/>

In your Java class add the following code:

final EditText edittext = (EditText) findViewById(R.id.editText);
    edittext.setText("Something ");
    Selection.setSelection(edittext.getText(), edittext.getText().length());
    edittext.addTextChangedListener(new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // TODO Auto-generated method stub
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            // TODO Auto-generated method stub
        }

        @Override
        public void afterTextChanged(Editable s) { if(!s.toString().contains("Something ")){
                edittext.setText("Something ");
                Selection.setSelection(edittext.getText(), edittext.getText().length());

            }

        }
    });

This is output of the code. Here the string Something is static and cannot be removed.

enter image description here

You could hide the underline in Edittext using android:background="@android:color/transparent". I would also recommend you to disable enter in the EditText by using the code:

<EditText
    android:id="@+id/editText"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:maxLines="1"      <----- add this 
    android:inputType="text"/>    <------ and this to disable enter in edittext
Abhi
  • 3,431
  • 1
  • 17
  • 35