Settext Fails to Show a Number as Text in a Textview

setText fails to show a number as text in a TextView

The problem is with lines like this

addAmount.setText(aAmt);

where aAmt is an int. This looks for a resource with the id of whatever aAmt is. You need to cast it to a String first like

addAmount.setText(String.valueOf(aAmt));

There are overloaded setText() methods. See the docs

This post goes into some more detail as to how this actually works

setText not displaying the value to a TextView

just use

TextView days_int_remaining = (TextView) findViewById(R.id.days_int_remaining);
days_int_remaining.setText(String.valueOf(returning));

instead of

TextView days_int_remaining = (TextView) findViewById(R.id.days_int_remaining);
days_int_remaining.setText(returning);

why doesn't TextView set text work with int?

The problem is that you are calling setText(int resid) instead of setText(CharSequence text). You can take a look at the documentation to understand more.

To solve this, you can convert the int to String by using String.valueOf():

private void punteggio(){
pntUno.setText(String.valueOf(pnt1));
pntDue.setText(String.valueOf(pnt2));
}

Android - Set text to TextView

In your layout XML:

<TextView
android:id="@+id/myAwesomeTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="Escriba el mensaje y luego clickee el canal a ser enviado"
android:textSize="20sp" />

Then, in your activity class:

// globally 
TextView myAwesomeTextView = (TextView)findViewById(R.id.myAwesomeTextView);

//in your OnCreate() method
myAwesomeTextView.setText("My Awesome Text");


Related Topics



Leave a reply



Submit