Search code examples
javaandroidxmlandroid-studiolibgdx

Access xml strings from java


I have seen several examples where people access .xml files like below from java.

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">YYYYYYYYY</string>
    <string name="app_id">999999999999</string>
    <string name="leaderboard_id">XXXXXXXXXXX</string>

</resources>

I have seen usage of android.R package claimed to be succesfull at fetching the strings within LibGDX setups. However, I keep running into error: cannot find symbol variable leaderboard_id, leaderbord_id is the only variable I currently trying.

I have tried cleaning up my project without errors but once building again I get the above error for every time I try to access it.

My strings.xml resides in ../android/res/values/ and I am trying to access it like getString(R.string.leaderboard_id) without success. Furthermore I tried Sync project with gradle and different API's and builds.

I am using the official Google BaseGameUtils which gives a bootstrap warning about classpath not being set but I don't see any issues with that. I'm at a point starting to doubt if this is possible or changed since the posts about this I found are old.

Here is my complete code:

AndroidLauncher.java

package net.madmenyo.multiplayertest.android;

import android.content.Intent;
import android.os.Bundle;
import android.R;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.backends.android.AndroidApplication;
import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration;
import com.google.android.gms.games.Games;
import com.google.example.games.basegameutils.GameHelper;

import net.madmenyo.multiplayertest.IGoogleServices;
import net.madmenyo.multiplayertest.MultiplayerTest;


public class AndroidLauncher extends AndroidApplication implements IGoogleServices {
    private GameHelper _gameHelper;
    private final static int REQUEST_CODE_UNUSED = 9002;

    @Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        // Create game helper
        _gameHelper = new GameHelper(this, GameHelper.CLIENT_GAMES);
        _gameHelper.enableDebugLog(false);

        AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
        initialize(new MultiplayerTest(this), config);
    }

    @Override
    protected void onStart()
    {
        super.onStart();
        _gameHelper.onStart(this);
    }

    @Override
    protected void  onStop()
    {
        super.onStop();
        _gameHelper.onStop();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        super.onActivityResult(requestCode, resultCode, data);
        _gameHelper.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void signIn() {
        try {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    _gameHelper.beginUserInitiatedSignIn();
                }
            });
        }
        catch (Exception e)
        {
            Gdx.app.log("MainActivity", "Log in failed: " + e.getMessage());
        }
    }

    @Override
    public void signOut() {
        try
        {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    _gameHelper.signOut();
                }
            });
        }
        catch (Exception e)
        {
            Gdx.app.log("MainActivity", "Log out failed: " + e.getMessage());
        }
    }

    @Override
    public void submitScore(long score) {
        if (isSignedIn() == true)
        {
            Games.Leaderboards.submitScore(_gameHelper.getApiClient(), getString(R.string.leaderboard_id), score); //Error cannot find symbol variable leaderboard_id
getApplicationContext().getResources().getString(R.string.leaderboard_id); //Error
            startActivityForResult(Games.Leaderboards.getLeaderboardIntent(_gameHelper.getApiClient(), getString(R.string.leaderboard_id)), REQUEST_CODE_UNUSED); //Error
        }
        else
        {
        }
    }

    @Override
    public void showScores() {
        if (isSignedIn() == true)
            startActivityForResult(Games.Leaderboards.getLeaderboardIntent(_gameHelper.getApiClient(), getString(R.string.leaderboard_id)), REQUEST_CODE_UNUSED); //Error
        else
        {
        }
    }

    @Override
    public boolean isSignedIn() {
        return _gameHelper.isSignedIn();
    }
}

Solution

  • Looks like you've imported wrong R class.

    import android.R;
    

    You need the R file pointing to your own resources, not the resources defined within Android SDK. What you need is (assuming that your application's package called net.madmenyo.multiplayertest.android):

    import net.madmenyo.multiplayertest.android.R;