Evorlor
Evorlor

Reputation: 7560

Android: Displaying an integer

When I comment out counter and run it with message, it works fine. It will not display an integer though. During my research, I have found no difference between the example code to display a String versus displaying an integer. Perhaps there is a guide somewhere I can review? Or a simple answer would also be appreciated. Thanks.

package com.evorlor.testcode;

import android.app.Activity;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.widget.TextView;

public class SupWorld extends Activity {

    private String message;
    private int counter;

    /**
     * @param args
     */
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO Auto-generated method stub

        message = "Sup world.";
        counter = 123;

        TextView text = new TextView(this);

        text.setTextSize(100);
        text.setMovementMethod(new ScrollingMovementMethod());
        // text.setText(message);
        text.setText(counter);

        setContentView(text);

    }

}

Upvotes: 1

Views: 2698

Answers (3)

leslieboo369
leslieboo369

Reputation: 1

I had the same problem. In the end both of these worked fine.

text.setText("Remaining Attempts: " + String.valueOf(counter));
text.setText("Remaining Attempts: " + counter);

My issue was the text box wasn't long enough and the counter fell on the second line that was hidden.

Sharing just in case someone else makes the same mistake I did.

Upvotes: 0

Durairaj Packirisamy
Durairaj Packirisamy

Reputation: 4725

It is the right way to do it.

text.setText(Integer.toString(counter)); 

Upvotes: 4

ρяσѕρєя K
ρяσѕρєя K

Reputation: 132982

use

text.setText(""+counter);

instead of

text.setText(counter);

to display integer value in TextView becuase TextView.setText(CharSequence text) only accept Strings to display not any other datatype

Upvotes: 1

Related Questions