MrCloister
MrCloister

Reputation: 462

How to display the value of a variable on the screen?

I know this is very basic question, but I need to know, how I can display the contents of a variable on the screen.

Do I use a textview in my layout?

I have a textview box and I can set it to say something in the editor but I need to write the contents of a variable so I can do some error checking.

Anyone help?

Upvotes: 22

Views: 101374

Answers (5)

JobHunter69
JobHunter69

Reputation: 2290

In addition, if you want to concatenate a string and a variable, you can use the "+" operator as you would do in System.out.print

Upvotes: 0

Aklank Jain
Aklank Jain

Reputation: 1062

If you have a list on the screen , then to not lose the list and still show change use , taking @Matt's example.

TextView textView = (TextView) findViewById(R.id.textViewName);
textView.setText("text you want to display");

It worked for me.

Upvotes: 4

srk
srk

Reputation: 435

int count=7;
     TextView tv = (TextView) findViewById(R.id.my_text_view);
tv.setText("you have entered"+count+"as the integer");

As you can see,you can include other data types like integers also in the setText block

Upvotes: 2

Thane Anthem
Thane Anthem

Reputation: 4093

In the Activity...

  public void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    int myValue = deriveMyValue();
    String message =
        myValue == -1 ?
            "The value is invalid." :
            "The value is " + myValue;
    TextView tv = (TextView) findViewById(R.id.my_text_view);
    tv.setText(message);
  }

Upvotes: 3

Matt
Matt

Reputation: 4989

If you have a TextView named textViewName defined in your layout XML file, you can just do something like this in your Activity class:

setContentView(R.layout.layoutName);
TextView textView = (TextView) findViewById(R.id.textViewName);
textView.setText("text you want to display");

Is this what you're looking for? If you don't need to display it to the screen, and just want to debug, just use Log() and logcat to view the messages.

Upvotes: 25

Related Questions