Ad

How To Change Textview Text With Button

- 1 answer

I have one textview and one button ,i coud change textview text with below code :

final Textview c_tv_matn;
Button c_btn_dokme;


c_btn_dokme = (button) findviewbyid(R.id.btn1);
c_tv_matn = (Textview) findviewbyid(R.id.txt1);

c_btn_dokme.setonclickListener(new OnclickListener() {
@Override
public void onClick(View v) {
c_tv_matn.SetText("this is second text");
});

But i wanna change text from String.xml and make Next Button Like this ; "matn_1","matn_2"matn_3"matn_4...

STRING.XML

<string name="matn_0">Hello world!</string>
 <string name="matn_1">You are hero john</string>
<string name="matn_2">you can change this world</string>
<string name="matn_3">You are so clever</string>

cAN YOU HELP ME TO GET RES FROM STRING AND CHANG TEXTVIEW TEXT WITH NUMBERS?

Ad

Answer

cAN YOU HELP ME TO GET RES FROM STRING AND CHANG TEXTVIEW TEXT WITH NUMBERS?

You can use either getString(int) or getText(int) to retrieve a string. getText(int) retains any rich text styling applied to the string. But in this case you'll use getString(int) that returns the string value associated with a particular resource ID. It will be stripped of any styled text information.

Sometimes you'll need a context like on this case that you want to do it inside a Button then you can get the context from your View, or if you have a global context in your Activity/Fragment you can use it also.

Example

If I did not misunderstood, what you want is to put the text from Strings.xml to TextView, so you can do it like this :

final Textview c_tv_matn;
Button c_btn_dokme;
int textNumber = 1;


c_btn_dokme = (button) findviewbyid(R.id.btn1);
c_tv_matn = (Textview) findviewbyid(R.id.txt1);

c_btn_dokme.setonclickListener(new OnclickListener() {
@Override
public void onClick(View v) {
switch(textNumber){
case 1:
   c_tv_matn.setText(v.getContext().getString(R.string.matn_1))
   textNumber++;
break;
case 2:
   c_tv_matn.setText(v.getContext().getString(R.string.matn_2))
   textNumber++;
break;
case 3:
   c_tv_matn.setText(v.getContext().getString(R.string.matn_3))
   textNumber++;
break;
default:
textNumber = 1;
  c_tv_matn.setText(v.getContext().getString(R.string.matn_1)
break;

});
Ad
source: stackoverflow.com
Ad