Skip to content

Rotating Canvas Games

Menu
  • Games
    • Balloon Shooter
    • Choozak
    • Polar
    • Tap Shoot Zombie!
    • Speedboat Santa
    • Rapid Car Rush
    • Turtle Leap
    • Crossword
  • HTML5 Games
    • Speedboat Santa HTML5
    • Choozak HTML5
  • Blog
  • Tutorials Index
  • About Us
  • Contact Us
  • Privacy Policy & Terms and Conditions
    • Privacy Policy
    • Terms and Conditions
    • Copyright Notice
    • Disclaimer
Menu

Showing Dialog in Libgdx Android

Posted on June 13, 2012December 4, 2023 by Rahul Srivastava

Hi,

In this small post we would write on how to show android dialogs in libgdx code. It is pretty simple. We used dialogs when we had some processing going on or while changing screens.

Create A Dialog:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:orientation="vertical" >
   <ImageView android:id="@+id/imageWaitDlg"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/pleasewait"
    android:contentDescription="@string/waitString"
   />
</LinearLayout>

Above is a sample xml for a dialog. android:src is pointing to an image which is located in the resources/drawable directory of the project. Below is the code to initialize the dialog which can be called in OnCreate function or anytime after that.

Dialog waitDialog;
private void InitDialog() {
   waitDialog = new Dialog(this);
   waitDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
   waitDialog.setContentView(R.layout.dialog);
   waitDialog.getWindow().setBackgroundDrawable(new ColorDrawable(0));

 }

Important Points:

  • We did not want any space for title in the dialog so the following line is there :-waitDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
  • We wanted a transparent background for the dialog which can be achieved by the following line of code :- waitDialog.getWindow().setBackgroundDrawable(new ColorDrawable(0));
Create A Interface:
Now the dialog is created we would work on calling this dialog to show up in the game. As this dialog is android specific we would be making an interface which would be implement by AndroidGame class so that it can be called from inside the code. If you are not familiar with this part then you can have a look at this post where we covered the topic on calling android specific code in libgdx.
public Interface WaitDialogInterface{
      public void ShowDialog();
      public void HideDialog();
}

Implement the Interface:

public class AndroidGame extends AndroidApplication implements WaitDialogInterface{
      .......
      Handler dialogHandler;
      private void InitDialog(){
          dialogHandler=new Handler();
          waitDialog = new Dialog(this);
          waitDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
          waitDialog.setContentView(R.layout.dialog);
          waitDialog.getWindow().setBackgroundDrawable(new ColorDrawable(0));
      } 
      public void ShowDialog(){
           dialogHandler.post(showDialogRunnable);
      }

      final Runnable showDialogRunnable=new Runnable(){
             @Override
             public void run() {
                // TODO Auto-generated method stub
                if(waitDialog!=null && !waitDialog.isShowing())
                      waitDialog.show();
                }

      };
      public void HideDialog(){
           dialogHandler.post(hideDialogRunnable);
      }
      final Runnable hideDialogRunnable=new Runnable(){
             @Override
             public void run() {
                // TODO Auto-generated method stub
                if(waitDialog!=null && !waitDialog.isShowing())
                      waitDialog.dismiss();
                }

      };
}

Note:

We need to attach a Runnable object because we are not calling these from the UI thread so we would get an error. So we create a handler so that the messages posted by it becomes part of the UI thread.

Call the functions from Game Code:

Now finally we just need to call these dialogs to show up in the game. For eg when you are disposing a screen and going to another screen then it can take sometime so call ShowDialog() in dispose function of a screen and call HideDialog() in the init function of the other screen.

SourceCode:

In the source code attached there is a “show dialog” image, which on clicking would show the dialog and on back press the dialog would disappear. Click on the image below to download. Rar contains eclipse project which can be imported into the workspace.

Dialog Example Source
Dialog Example Source

Thanks. If you have any queries/suggestions please post them in comments.

  • Tweet
©2025 Rotating Canvas Games | Built using WordPress and Responsive Blogily theme by Superb