Crelix Crelix - 2 months ago 6
Android Question

Converting code from Java to Android Studio (Displaying Values)

I'm trying to convert my game from java to Android Studio, so I could learn Android developing. But my biggest problem now are displaying values.

I got variables for example:

CarMain.main[0]


With button I'm adding values :
CarMain.main[0] += 1;


In Java I displayed values like :

S_MoneyLabel.setText("Money: " + CarMain.main[0]);


And output was for example : Money : 71 or Money :5000

And now in Android studio I don't know how to display it.
Im thinking about textview option, but can't figure out how to do it.

Answer

Add on text view in layout xml file like below :

    <RelativeLayout  
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

<Button
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/button"/>
 </RelativeLayout>

Now, in your activity class create instance for that button in onCreate() method :

    Button button = findViewById(R.id.button);

Now, set the result to text view

    button.setText("Money: " + CarMain.main[0]);