Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change a TextView's text by pressing a Button

I want to change the of a TextView by pressing a Button, but don't understand how to do it properly.

This is part of my layout:

<TextView android:id="@+id/counter"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Text" />
<Button android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Change text!" />

And this is my activity:

public class Click extends Activity {
    protected void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.main);
        final Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // Perform action on click
                // ???
            }
        });
    }
}

What should I put inside onClick() method?

like image 457
Roman Avatar asked Jul 16 '11 10:07

Roman


People also ask

How change TextView text from another activity?

You have to use Intent to make action in another activity. textView. setText(btn_text);

Can a TextView be clickable?

In Android, the most common way to show a text is by TextView element. The whole text in the TextView is easy to make clickable implementing the onClick attribute or by setting an onClickListener to the TextView.

Which method can set or change text in a button?

Answer: Use the jQuery prop() and html() Methods You can simply use the jQuery prop() method to change the text of the buttons built using the HTML <input> element, whereas to change the text of the buttons which are created using the <button> element you can use the html() method.


1 Answers

According to: http://developer.android.com/reference/android/widget/TextView.html

TextView view = (TextView) findViewById(R.id.counter);
view.setText("Do whatever");
like image 82
Gasim Avatar answered Nov 08 '22 08:11

Gasim