This is how I achieved to run android platform specific code with libGDX, this example is about running a Toast.
Add an ActionResolver interface under the core project.
1 2 3
Here you can add the methods you’re going to need, this example only
handles a toast, so for showing a toast I have made a
method which takes a text, this text is the text the toast will show.
Action Resolver for Android
Add an ActionResolverAndroid class under the android project.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
Here we define the
showToast method, to be able to show a toast we
handler, be sure you import the
and not other one (I committed that mistake).
The handler is just needed if you’re going to show some GUI stuff.
Cannot resolve method error
Some methods will require you to prepend the context, so for example
trying to invoke the method
startActivity(intent) will lead you
to an error, to solve this prepend the context as follows.
Under your core project there is a class which extends for the
class, in this example that game class is called
YourGame, add the
1 2 3
Add the following in the constructor.
Under your android project there is a class named
we need to edit that class, It should look like this.
1 2 3 4 5 6 7 8 9 10 11 12
In this case you will commonly try to use a Toast in a
Create a new screen like follows.
1 2 3
YourScreen class constructor like follows.
YourGame class you’ll set the new screen like this:
Invoking the showToast method
Screen you can invoke it in the
To invoke it use the following.
Send data back to game
Sometimes we’ll need to inform about data changes back to the game. We do this as follows.
1 2 3 4 5 6 7