The problem is that you are trying to create an Android widget, which I suspect you are doing in the implementation of Libgdx-core. In the main implementation, there are no links to the Android SDK.
This is because it is an Android project that inherits the main project. As a result, the main project is not aware of any dependencies loaded into the Android implementation.
To overcome this, you need to create an interface between the Android project and the Core Project. This will allow you to call methods inside an Android project. The interface must be created inside the main project so that both projects have access to it.
For example, you create CrossPlatformInterface.java inside the main project. But first let me create a callback to get the feedback from the Ui stream inside the Libgdx stream. It is important to remember that Libgdx has a separate thread, the main Android thread. . If you try to launch Widgets of Android from Libgdx streams, the application will be crushed.
Let me make a callback for AlertDialog. I propose here an abstract class to be able to redefine only those methods that you need, because sometimes Alertdialog can have 1,2 or 3 buttons.
In the main project, create AlertDialogCallback.java:
public abstract class AlertDialogCallback{ public abstract void positiveButtonPressed(); public void negativeButtonPressed(){};
CrossPlatformInterface.java is also created in the Core Project:
public interface CrossPlatformInterface{ public void showAlertDialog(AlertDialogCallback callback); }
You have noticed that in the showAlertDialog method, we pass a callback to receive feedback when buttons are clicked.
Then you create a class project inside Android that will implement CrossPlatformInterface, for example:
public ClassInsideAndroidProject implements CrossPlatFormInterface{ private AndroidLauncher mActivity; // This is the main android activity public ClassInsideAndroidProject(AndroidLauncher mActivity){ this.mActivity = mActivity; } public void showAlertDialog(final AlertDialogCallback callback){ mainActivity.runOnUiThread(new Runnable(){ @Override public void run() { AlertDialog.Builder builder = new AlertDialog.Builder(mActivity); builder.setTitle("Test"); builder.setMessage("Testing"); builder.setPositiveButton("OKAY", new OnClickListener(){ @Override public void onClick(DialogInterface dialog, int which) { callback.positiveButtonPressed(); } }); builder.setNegativeButton(negativeButtonString, new OnClickListener(){ @Override public void onClick(DialogInterface dialog, int which) { callback.negativeButtonPressed(); } }); AlertDialog dialog = builder.create(); dialog.show(); } }); } }
Important notes
- CrossPlatformInterface will be created inside MainActivity (AndroidLauncher), as you will see below.
- AlertDialog will be created in the Android UI thread. Since we get the Libgdx thread to create the AlertDialog, we need to use runOnUiThread to ensure that the AlertDialog is created in the ui thread.
Finally, how to do it:
Create the CrossPlatform interface inside the main Android operation and pass the Activity instance to the interface that is passed inside MyGdxGame:
public class MainActivity extends AndroidApplication { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration(); cfg.useGL20 = false; initialize(new MyGdxGame(new ClassInsideAndroidProject(this)), cfg); } }
Finally, when MyGDxGame is created, we get an instance of the cross-platform interface, and we can call any functions we want to use for the android ui stream.
public class MyGdxGame extends Game { ClassInsideAndroidProject crossPlatformInterface; public MyGdxGame(ClassInsideAndroidProject crossPlatformInterface){ this.crossPlatformInterface = crossPlatformInterface; } @Override public void create() { crossPlatformInterface.showAlertDialog(new AlertDialogCallback(){ @Override public void positiveButtonPressed(){
I think it was a lot more letters that I first intended. It may seem complicated, but actually quite simple. Well, after you have done this, everything looks simpler :). The advantage of this effort is that after you make this interface, any call to the Android widget will be a very simple and safe thread.
Hope this gives a good picture.