Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Dialog without title and border

Tags:

android

layout

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>
like image 297
Fugogugo Avatar asked Mar 23 '11 15:03

Fugogugo


3 Answers

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);
like image 76
Sidhanshu Avatar answered Oct 22 '22 07:10

Sidhanshu


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.

like image 17
Matthew Willis Avatar answered Oct 22 '22 07:10

Matthew Willis


  1. 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));
        }
    }
    
  2. 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>
    
  3. add the image above in your drawable folder with name bg_custom_dialog.9.png

bg_custom_dialog

  1. call in your activity:

    CustomDialog customDialog = new CustomDialog(this);
    customDialog.show();
    
like image 9
ademar111190 Avatar answered Oct 22 '22 07:10

ademar111190