Based on the code here, http://developer.android.com/guide/topics/ui/dialogs.html#CustomDialog . I am successfully able to create a custom dialog with background and buttons inside, but there's still something not right. there still a space for title bar, and there are border around the view. how to get rid of these title and border?
here is my dialog
Dialog pauseMenu = new Dialog(this,R.xml.pause_dialog);
pauseMenu.setContentView(R.layout.pause_menu);
return pauseMenu;
and here is my pause layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="wrap_content" android:id="@+id/linearLayout1" android:layout_width="wrap_content" android:background="@drawable/pause_menu_cropped" android:layout_gravity="center" android:gravity="center|center_horizontal">
<TableLayout android:layout_width="wrap_content" android:id="@+id/tableLayout1" android:layout_height="wrap_content">
<ImageButton android:src="@drawable/pause_button_quit" android:layout_width="wrap_content" android:background="@drawable/pause_button_quit" android:id="@+id/imageButton2" android:layout_height="wrap_content"></ImageButton>
<ImageButton android:src="@drawable/pause_button_option" android:layout_width="wrap_content" android:background="@drawable/pause_button_option" android:id="@+id/imageButton1" android:layout_height="wrap_content"></ImageButton>
</TableLayout>
</LinearLayout>
I think this will help you out
gameOver would be the dialog name and in setContentView it would be ur custom dialog layout
gameOver = new Dialog(Main.this);
gameOver.requestWindowFeature(Window.FEATURE_NO_TITLE);
gameOver.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
gameOver.setCancelable(false);
gameOver.setContentView(R.layout.gameover);
A Dialog cannot be created without a title. Further down in that tutorial it mentions:
A dialog made with the base Dialog class must have a title. If you don't call setTitle(), then the space used for the title remains empty, but still visible. If you don't want a title at all, then you should create your custom dialog using the AlertDialog class. However, because an AlertDialog is created easiest with the AlertDialog.Builder class, you do not have access to the setContentView(int) method used above. Instead, you must use setView(View). This method accepts a View object, so you need to inflate the layout's root View object from XML.
This answer solves both the title and the border problem using a custom style.
Make a class like this:
public class CustomDialog extends Dialog {
public AlertFinishiView(Context context) {
super(context);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.dialog);
getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
}
}
make a xml in layut folder with this name dialog
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent">
<RelativeLayout
android:layout_width="220dp"
android:layout_height="140dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="@drawable/bg_custom_dialog" >
<Button android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Button" />
</RelativeLayout>
</RelativeLayout>
add the image above in your drawable folder with name bg_custom_dialog.9.png
call in your activity:
CustomDialog customDialog = new CustomDialog(this);
customDialog.show();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With