Custom dialog boxes in android

September 7, 2012 | Rishabh Agrawal | General

dialog is usually a small window that appears in front of the current Activity. The underlying Activity loses focus and the dialog accepts all user interaction. Dialogs are normally used for notifications that should interrupt the user and to perform short tasks that directly relate to the application in progress .
The Dialog class is the base class for creating dialogs.
Types of default dialog boxes are:
• AlertDialog
• ProgressDialog
• DatePickerDialog
• TimePickerDialog

If we wish to create a customized dialog box then we can do so in the following way:

Step 1. Create a xml layout for the dialog box and a transparent background is preferable in order to show the Activity screen underneath it.

dialog_screen.xml

<RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
android:background=”@drawable/bg_dialog” >

<ImageView
android:id=”@+id/dialogImgView_id”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignParentBottom=”true”
android:background=”@drawable/mid_box” />

<ImageButton
android:id=”@+id/ImgBtn2_id”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignTop=”@+id/ImgBtn1_id”
android:background=”@drawable/female_selector” />

</RelativeLayout>

Step2. Add the following code for dialog box in oncreate(),onclick() etc based on where you want the dialog box to pop. In the given example the dialog box appears as soon as the activity starts .Hence, the code is placed in oncreate() .

public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.play_menu_screen);//setting layout foractivity

//put your activity related code here

Dialog(); //function call for dialog box

}

/*code for creating a custom dialog box */

private void Dialog()
{
/* creating a dialog object and specifying the activity it pops on .
PlayActivity is the activity on which dialog box appears & R.style.FullHeightDialog
Specifies that dialog box has height equivalent to screen height */

final Dialog dialog = new Dialog(PlayActivity.this,R.style.FullHeightDialog);

dialog.setContentView(R.layout.dialog_screen);//setting the dialog xml layout

/* adding action when image buttons of dialog are clicked */

dialog.findViewById(R.id.ImgBtn_id).setOnClickListener(
new OnClickListener() {
public void onClick(View v) {

//put your code here

dialog.dismiss();//closes the dialog box
}
});

dialog.show();//pops the dialog box

}
This way we can create a customized dialog box .Why use the boring default dialog boxes when we can create our own.


THE AUTHORRishabh Agrawal

Rishabh Agrawal is the founder of Creatiosoft, a company focused on creating high-quality software for the iGaming industry, specialising in poker and card games. With years of experience, Rishabh is dedicated to delivering engaging and user-friendly gaming experiences. Through this blog, he shares his passion and insights to help readers understand the latest trends and advancements in iGaming.

Recent Posts

The Future of Mobile Gaming: What to Expect in the Next 5 Years

Over the years, mobile gaming has seen amazing development and innovation utilized by the game development company in India. It…
06 Jun 2023 Rishabh Agrawal

How Do I Get Started With NFT Gaming?

The popularity of the NFT game has created a different fan base. There was a time when games were a…
18 Jul 2022 Rishabh Agrawal

What Are The Top Trends in NFT Marketplace 2022?

The NFTs are the new engaging and revolutionary technology across the globe. Though, these non-fungible tokens appeared for the first…
09 Jun 2022 Rishabh Agrawal