Cactus
Cactus

Reputation: 27636

TextView with a single, editable line

I'd like to do textual back-and-forth interaction in an Android control. The idea is to have something like this:

This is some text output by the program.

What is your name? |

with the cursor at | (note that editing doesn't start at the beginning of the last line). The user is then free to enter text (using whatever Android input method, keyboard, etc.) but isn't allowed to change any of the output so far. Ideally, the user's input would be styled differently.

Then, as soon as newline is entered, I want the program to be notified and editing to stopped:

This is some text output by the program.

What is your name? Foo Bar

Hello, Foo Bar!

Note that this needs to be a proper control, i.e. one I can compose with other controls to make it just one part of the app's main layout.

Upvotes: 1

Views: 147

Answers (4)

Bhavesh Patadiya
Bhavesh Patadiya

Reputation: 25830

This is some text output by the program.

What is your name? |

with the cursor at | (note that editing doesn't start at the beginning of the last line). The user is then free to enter text (using whatever Android input method, keyboard, etc.) but isn't allowed to change any of the output so far. Ideally, the user's input would be styled differently.

I would strongly recommend to rethink about your design as the same thing can be done with the help of LinearLayout,Editext,TextView with very simple and more manageable way.

Upvotes: 2

Murali krishna
Murali krishna

Reputation: 823

Make a TextView and the EditText next to each other then your problem is solved and add the following line of code in EditText.

android:singleLine= 'true';

It allow only one line to be entered to the EditText. let me know whether this is what your expecting.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"     android:layout_height="match_parent">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="What is your Name?"
    android:id="@+id/textView"
    android:layout_alignParentTop="true"
    android:layout_alignParentStart="true" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:singleLine="true"
    android:id="@+id/editText"
    android:layout_alignParentTop="true"
    android:layout_toEndOf="@+id/textView" />
</RelativeLayout>

Upvotes: 2

Harpreet
Harpreet

Reputation: 3070

You need a ListView at top, to show your conversation & then below it, needs a horizontal view with a TextView (to show question) and EditText(with background transparent - to ask user to fill an answer).

Upvotes: 1

Attiq ur Rehman
Attiq ur Rehman

Reputation: 475

I would suggest you to create a new LinearLayout(TextView + EditText) and assign the background of layout like EditText and edittext's no background. Upon editText done, you could show a new TextView in the bottom

Upvotes: 1

Related Questions