0

I have a EditText and a Button in my activity xml. What i wanted to do is that on clicking the button the text from the EditText should get displayed in a TextView above the EditText.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom"
android:orientation="vertical" >

<EditText 
    android:id="@+id/edit_message"
    android:hint="@string/hint_message"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"/>

<Button 
    android:id="@+id/button_send"
    android:text="@string/button_text"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:onClick="sendMessage"/>

and this is my sendMessage()

public void sendMessage(View view){
    EditText editText=(EditText)findViewById(R.id.edit_message);
    String message=editText.getText().toString();       
    LinearLayout layout=(LinearLayout)findViewById(R.id.layout01);
    TextView text=new TextView(this);
    text.setText(message);
    layout.addView(text);
}

When i click the button, nothing happens. Nothing at all. Any idea why this is not working? I'm pretty new to Android development.

4 Answers 4

2

Add this line to your LinearLayout tag.

android:id="@+id/linear"

And change your sendMessage method to following

public void sendMessage(View view){
EditText editText=(EditText)findViewById(R.id.edit_message);
String message=editText.getText().toString();       
LinearLayout layout=(LinearLayout)findViewById(R.id.linear);
TextView text=new TextView(this);
text.setText(message);
text.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
layout.addView(text);

}

Sign up to request clarification or add additional context in comments.

Comments

1

You must have a TextView above your EditText and set its property android:visibility="gone".

Now, when you want to show your text in that TextView, at that time. do getText() to get text from EditText and set that text using setText() for your TextView and at the same time for your TextView, do setVisibility(View.VISIBLE).

like,

String text = editText.getText();
textView.setText(text);
textView.setVisibility(View.VISIBLE);

Comments

1

Change your XML to following.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom"
android:id="@+id/linear"
android:orientation="vertical" >

<TextView
android:id="@+id/tv" 
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
/>

<EditText 
android:id="@+id/edit_message"
android:hint="@string/hint_message"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>

<Button 
android:id="@+id/button_send"
android:text="@string/button_text"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:onClick="sendMessage"/>

And now your sendMessage() should look like this one.

public void sendMessage(View view){
EditText editText=(EditText)findViewById(R.id.edit_message);
String message=editText.getText().toString();       
LinearLayout layout=(LinearLayout)findViewById(R.id.linear);
TextView text=(TextView)findViewById(R.id.tv);
text.setText(message);

}

2 Comments

will this work if i wanted to display new TextView every time I click the button??
for new TextView every time, My first answer will work in that way. this will set new text on previous text view every time.
0

You have to set the LayoutParams (wrap_content/match_parent) for your new View :

TextView text = new TextView(this);

text.setLayoutParams(new LinearLayout.LayoutParams(
        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));    

text.setText(message);
layout.addView(text);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.