How can I send a integer from my Java file to my XML folder? (Android Studio)

296 Views Asked by At

I have an int called playerCount in my MainActivity Java file and would like to import it to my XML file, How would I go about doing that?

When I loaded an Java Method I wrote

android:onClick="playSound"

And then,

public void playSound(View view) {  
    MediaPlayer mediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.sound);  
    mediaPlayer.start();     
}

What I'm trying to say is how do I load an number stored in an integer in an java file to xml? For example

android:text="(the integer would go int here)"

I'm using Arduino Studio messing around and trying to make my first app, I have some experience with Java from before.

Thanks

1

There are 1 best solutions below

0
On BEST ANSWER

You don't actually want to put that integer inside your XML file.

Your XML file contains views right? Like <TextView></TextView>. These should also contain an id like android:id="@+id/myTextView"

You can give these views certain values you want with methods like setText(); First find the view by the ID and then set the integer as a text value for the TextView.

TextView textView = (TextView)findViewById(R.id.myTextView);
textView.setText(playerCount+"");