user1509702
user1509702

Reputation: 1

Adding TextView dynamically in Android

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.

Upvotes: 0

Views: 8888

Answers (4)

Waqas
Waqas

Reputation: 4489

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);

}

Upvotes: 1

Waqas
Waqas

Reputation: 4489

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);

}

Upvotes: 2

Dalmas
Dalmas

Reputation: 26547

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);

Upvotes: 0

Aqif Hamid
Aqif Hamid

Reputation: 3521

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);

Upvotes: 1

Related Questions