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
//xml
<TextView android:id="@+id/textView2" android:text="blah blah blah"></TextView>
<Button android:text="Wrong answer." android:onClick="wrongAns" android:clickable="true"></Button>
//code
TextView theCorrectAnsTextView = (TextView)findViewById(R.id.textView2);
public void wrongAns(View v)
{
theCorrectAnsTextView.setText("TextView text has changed!");
}
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.
Maybe you need to initialize the button. And if it doesn't work, just do it in java code :
Button btn = (Button) findViewById(YourId);
btn.setonClickListener(listener);
public onClickListener listener = new View.OnclickListener{
onclick(View v){
// do your thing
}
}
Something like that, i don't remember without eclipse to correct me.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With