drdrdr drdrdr -4 years ago 145
Android Question

Changing TextView text on button click (Android)

I am trying to change text of a textview on a button click. Below is the gist of my code but it doesnt seem to work. Am i doing something wrong. Thanks in Advance

<TextView android:id="@+id/textView2" android:text="blah blah blah"></TextView>
<Button android:text="Wrong answer." android:onClick="wrongAns" android:clickable="true"></Button>

TextView theCorrectAnsTextView = (TextView)findViewById(R.id.textView2);

public void wrongAns(View v)
theCorrectAnsTextView.setText("TextView text has changed!");

Answer Source
  1. First you give onclick event for Button like (buttonClick).
  2. In java file just write below code.

    public void buttonClick(View v)
    TextView tv = (TextView)findViewById(R.id.textView1);
    tv.setText("Welcome to android");

Hope this will help you.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download