Shirwa Mohamed
Shirwa Mohamed

Reputation: 63

How to display typed text in text view Android

I am having trouble displaying the text in a text view. I want to make a chat/texting style app. Also, how would I have a border around my text View field so that it looks nice. Thank You! and I apologize for my troubles ahed of time.

    <?xml version="1.0" encoding="utf-8"?>
<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:orientation="horizontal">
     <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="400dp"
        android:text="" />
    <EditText android:id="@+id/edit_message"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:hint="@string/edit_message" 
        android:layout_gravity="bottom" />

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

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }



  @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

  Button button = (Button)findViewById(R.id.button_send);

    };

Upvotes: 0

Views: 35328

Answers (4)

captaindroid
captaindroid

Reputation: 2928

This wil work for sure

public class MainActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

public void sendMessage(View v){

    EditText editMessage=(EditText)findViewById(R.id.edit_message);
    TextView textView = (TextView) findViewById(R.id.textView1);

    //get text from edittext and convert it to string
    String messageString=editMessage.getText().toString();

    //set string from edittext to textview
    textView.setText(messageString);

    //clear edittext after sending text to message
    editMessage.setText("");


}

}

Upvotes: 4

GunJack
GunJack

Reputation: 1968

Probably you need this.

 EditText edt = (EditText)findViewById(R.id.edit_message);
 TextView txt= (TextView)findViewById(R.id.textview1);
 String value = edt.getText().toString();

button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                 txt.append(value+"\n");
                      edt.setText("");
            }
     });

Upvotes: 1

Syn3sthete
Syn3sthete

Reputation: 4171

<TextView android:text="" android:background="@drawable/border"/>

include the above line in your xml and create border.xml inside res/drawable folder and write the following code inside border.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
   <solid android:color="#dfdfdf" />
   <stroke android:width="1dip" android:color="#aa0000"/>
</shape>

this will set the border for the textView

for the later part use this in java

button.setOnClickListener(new OnClickListener() {
       void onClick() {
         textView.setText(editText.getText());
        }
    });

hope that was useful for you :) let know if any problems

Upvotes: 1

Danny
Danny

Reputation: 1050

If u want to display from the strings then use

 android:text="@strings/text"

if u want to get text from edittext and append to a textview then

  EditText edit = (EditText) findViewById(R.id.editText);
  TextView text = (TextView) findViewById(R.id.Text);
  String str=edit.getText().toString();
  text.setText(str);

Upvotes: 0

Related Questions