0

I'm trying to program an android App with AndroidStudio. It is like an calculator and i don't know how to print the answer on my screen. I tried it with System.out.println() but it doesn't work Log._() doesn't work too.

1 Answers1

0

You can display a temporary pop-up message to the screen using a Toast as below:

Toast.makeText(context,"<Message here>", Toast.LENGTH_LONG).show();

If you want the text to persist, then use a TextView object. Add it to your .xml for that activity:

<TextView
    android:id="@+id/myTextView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

Then declare it in your Activity screen:

TextView myTextView = (TextView) findViewById(R.id.myTextView);

Then set the text using .setText(), probably in the onClick() method of whatever button you're using to calculate the result for the user:

myTextView.setText(calculatorResult);
Mihai Chelaru
  • 7,614
  • 14
  • 45
  • 51
  • thanks does it stay on the screen or is it just temporary on the screen ? – Nightingale Apr 26 '18 at 16:55
  • No, a toast only appears for a certain duration, either LENGTH_SHORT or LENGTH_LONG. If you want to have it persist on the screen you'd have to use something else like a [TextView](https://developer.android.com/reference/android/widget/TextView) that displays the answer. – Mihai Chelaru Apr 26 '18 at 16:56
  • 1
    No problem. Consider [accepting the answer](https://meta.stackexchange.com/a/5235) if you found it worked for you so that people know the question has been solved. – Mihai Chelaru Apr 26 '18 at 17:25