Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I clear the text in Android TextView?

So I have an Android program like so:

package com.example.androiddemo;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

public class AndroidDemo extends Activity {
    String[] messages = {"Short Text",
                         "I want to show some really long text" +
                         "on the display of the phone. " +
                         "Having run out of ideas on what to type, " +
                         "I am adding this text which makes absolutely " +
                         "no sense."};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        final Button add = (Button) findViewById(R.id.addBtn);
        final Button clear = (Button) findViewById(R.id.clrBtn);
        final EditText text = (EditText) findViewById(R.id.display);

        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(v == add){
                    text.setText(messages[new java.util.Random().nextInt(messages.length)]);
                }else if(v == clear){
                    text.setText("");
                }
            }
        });
    }
}  

The button to add text to the TextView works perfectly fine however the text never clears.
My belief was that the operation of TextView would be analogous to JTextField or JTextArea where setting the text to "" clears it.

How do I clear the text?

like image 930
An SO User Avatar asked Jul 04 '13 07:07

An SO User


People also ask

How do you delete text in TextView?

The button to add text to the TextView works perfectly fine however the text never clears. My belief was that the operation of TextView would be analogous to JTextField or JTextArea where setting the text to "" clears it.

Can we change the text in TextView?

Using Spans to Style Sections of Text Spans come in really handy when we want to apply styles to portions of text within the same TextView. We can change the text color, change the typeface, add an underline, etc, and apply these to only certain portions of the text.


1 Answers

Try this,

public class AndroidDemo extends Activity implements OnClickListener{
Button add, clear;
EditText text;
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        add = (Button) findViewById(R.id.addBtn);
        clear = (Button) findViewById(R.id.clrBtn);
        text = (EditText) findViewById(R.id.display);
        add.setOnClickListener(this);
        clear.setOnClickListener(this);
        }
@Override
public void onClick(View v){
    if(v == add){
        text.setText(messages[new java.util.Random().nextInt(messages.length)]);
    }else if(v == clear){
        text.setText("");
    }
    }
  }
like image 118
No_Rulz Avatar answered Sep 23 '22 02:09

No_Rulz