How do I display an alert dialog on Android?

后端 未结 30 3069
清歌不尽
清歌不尽 2020-11-22 03:02

I want to display a dialog/popup window with a message to the user that shows \"Are you sure you want to delete this entry?\" with one button that says \'Delete\'. When

30条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-22 03:19

    Just a simple one! Create a dialog method, something like this anywhere in your Java class:

    public void openDialog() {
        final Dialog dialog = new Dialog(context); // Context, this, etc.
        dialog.setContentView(R.layout.dialog_demo);
        dialog.setTitle(R.string.dialog_title);
        dialog.show();
    }
    

    Now create Layout XML dialog_demo.xml and create your UI/design. Here is a sample one I created for demo purposes:

    
    
    
        
    
        
    
            

    Now you can call openDialog() from anywhere you like :) Here is the screenshot of above code.

    Enter image description here

    Note that text and color are used from strings.xml and colors.xml. You can define your own.

提交回复
热议问题