0

A little more specific. I want to know if it is possible to update a number that is saved in a TextView or if I have to use two TextViews.
What I have atm in the XML-File:

<TextView
 android:id="@+id/UpInfo"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="Upgrade: 0 "/>

Now I want to change the 0 in android:text while the App is running. Is this possible? If yes how?

Thanks in advance

Insworn
  • 71
  • 5

3 Answers3

4

Use a variable and string concatenation:

int number = 1;
TextView textview = (TextView) findviewbyId(R.id.UpInfo);

textview.setText("Upgrade: " + number);

Or if you use a method like this:

public void updateText(int number){
  textview.setText("Upgrade: " + number);
}

Then you would call it like this:

updateText(2); 

Result... Upgrade: 2

updateText(3);

Result.... Upgrade: 3

MichaelStoddart
  • 5,571
  • 4
  • 27
  • 49
0

Yes,you can use Spannable string

final SpannableStringBuilder spannable = new SpannableStringBuilder(your string);

spannable.setSpan(span, 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
txtView.setText(spannable);
SaravInfern
  • 3,338
  • 1
  • 20
  • 44
0

Yes it can be achieved using Spannable strings.

Find relevant examples in the link below:

http://androidcocktail.blogspot.in/2014/03/android-spannablestring-example.html

MichaelStoddart
  • 5,571
  • 4
  • 27
  • 49
raasesh
  • 161
  • 11