11

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?

Roman
  • 5,358
  • 9
  • 34
  • 43

7 Answers7

15

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

TextView view = (TextView) findViewById(R.id.counter);
view.setText("Do whatever");
Gasim
  • 7,615
  • 14
  • 64
  • 131
12
  1. Find the textview by id
  2. Change the text by invoking yourTextView.setText("New text");

Refer findViewById and setText methods.

jszumski
  • 7,430
  • 11
  • 40
  • 53
chedine
  • 2,384
  • 3
  • 19
  • 24
  • One important thing: the field is updated only when you get out from the onClick block, you cannot update the same field twice. – onizukaek Jun 11 '14 at 12:47
9
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class Click extends Activity {
int i=0;
    protected void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.main);
        final TextView mTextView = (TextView) findViewById(R.id.counter) 
        mTextView.setText("hello "+i);

        final Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // Perform action on click
              i=i+1;  
              mTextView.setText("hello "+i);
            }
        });
    }
}

Hope this serve your need

Mohammed Azharuddin Shaikh
  • 41,633
  • 14
  • 96
  • 115
3
TextView tv = (TextView) v;
tv.setText("My new text");

Edit: Here is your handler:

button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            //TextView tv = (TextView) v; //code corrected
            TextView tv= (TextView) findViewById(R.id.counter);
            tv.setText("My new text");
        }
});

TextView view = (TextView) findViewById(R.id.counter);

Vikas
  • 24,082
  • 37
  • 117
  • 159
  • I hope you know that this is totally wrong. The `v` parameter in the `onClick()` method is the clicked on view. Now if he clicks on a `Button` with your code all that happens is that the text of your `Button` changes. – Octavian Helm Aug 16 '11 at 08:40
  • Oh! Button and TextView are different. Yes its my mistake. I am going to delete my answer. – Vikas Aug 16 '11 at 08:46
0

In onclick, take the object of the TextView and set the desired text like so:

tvOBJECT.setText("your text");
Ren
  • 1,111
  • 5
  • 15
  • 24
user2342517
  • 121
  • 1
  • 3
0

Java only no XML version

To make things more explicit:

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

public class Main extends Activity {
    private int i;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.i = 0;

        final TextView tv = new TextView(this);
        tv.setText(String.format("%d", this.i));

        final Button button = new Button(this);
        button.setText("click me");
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Main.this.i++;
                tv.setText(String.format("%d", Main.this.i));
            }
        });

        final LinearLayout linearLayout = new LinearLayout(this);
        linearLayout.addView(button);
        linearLayout.addView(tv);
        this.setContentView(linearLayout);
    }
}

Tested on Android 22.

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
0

You can do it with the setText("anything") method.

Zwiebel
  • 1,605
  • 4
  • 21
  • 37