6

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?

ataulm
  • 15,195
  • 7
  • 50
  • 92
An SO User
  • 24,612
  • 35
  • 133
  • 221
  • Try not to mix up `TextView` and `EditText`. An `EditText` is analogous to `JTextField`/`JTextArea`, and `TextView` with `JLabel` (and the equivalent larger not-user-editable box) – ataulm Jul 04 '13 at 07:51
  • @ataulm Thanks for mentioning that !! :-) – An SO User Jul 04 '13 at 09:41

5 Answers5

14

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("");
    }
    }
  }
No_Rulz
  • 2,679
  • 1
  • 20
  • 33
  • hmm.. is it a good practice to make an `Activity` a listener or should I make a class of my own and make it a listener ? :-) – An SO User Jul 04 '13 at 09:36
4

To answer your question, I use this method in my apps. Instead of setting the text to (""), it clears the stored value using the getText().clear(); method. Personally, I use this because it looks more professional and makes proper use of the clear command in android.

clearButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    editText.getText().clear();
                }
            });

EDIT: The original post had the tag 'EditText', so I answered the question with that in mind. This works with EditText, With a TextView, just call textview.setText("");

A P
  • 2,131
  • 2
  • 24
  • 36
3

Yes it is. Your problem is related to the onClickListener.

 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("");
                }
            }
        });

in this case if(v == clear) is always false. You have to register a View.OnClickListener() for the clear view/button, or make your Activity implements View.OnClickListener and set for both the button this as setOnClickListener

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
2

You're going wrong with your onClick method. You are adding it for 1 button, and then checking which button it is. It is never going to be your 'clear' button, since it only listens to your 'add' button onclick.

Stefan de Bruijn
  • 6,289
  • 2
  • 23
  • 31
  • Is it possible to have a general listener which can handle all the buttons by using `if-elseif ladder` ?? – An SO User Jul 04 '13 at 07:46
  • @LittleChild yes, your Activity could be this general listener (see No_Rulz's answer) But you still need to add it to each button. – ataulm Jul 04 '13 at 07:48
  • Yes, check out No_Rulz his answer. You make your Activity extend OnClickListener (be careful not to import the Dialog one, but the View one!) which will give you 1 general onClick method. You can subscribe all your buttons with something such as button.setOnClickListener(this) then. In the onClick method, just switch/case. – Stefan de Bruijn Jul 04 '13 at 07:48
  • Sure. so I can define an `onClick(View v)` in the `Actiity` and add it to all buttons. Then it can handle both of them ? :) – An SO User Jul 04 '13 at 07:48
  • Or add `android:onClick` to your button xml – Stefan de Bruijn Jul 04 '13 at 07:49
1

Use

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

        final TextView tv=(TextView)findViewById(R.id.textView);
        Button btnclear=(Button)findViewById(R.id.btnClear);

 btnclear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                tv.setText("");
            }
        });
}
Saleem Kalro
  • 1,046
  • 9
  • 12