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?

7 Answers 7

15

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

TextView view = (TextView) findViewById(R.id.counter); view.setText("Do whatever"); 
Sign up to request clarification or add additional context in comments.

Comments

12
  1. Find the textview by id
  2. Change the text by invoking yourTextView.setText("New text");

Refer findViewById and setText methods.

1 Comment

One important thing: the field is updated only when you get out from the onClick block, you cannot update the same field twice.
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

1 Comment

I tried this,and i modify something like this mTextView.setText(Integer.toString(i));, and it's worked. Thank you. +1
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);

2 Comments

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.
Oh! Button and TextView are different. Yes its my mistake. I am going to delete my answer.
0

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

Comments

0

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

tvOBJECT.setText("your text"); 

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.