Android: Custom A Simple Dialog

ertget

 

Above is the result of this demo (A dialog with your own custom layout).

Step 1: Create new layout resource for the dialog we are going to custom next. As you see in the pic above, we have one TextView & one ImageView, we can use any image for this example, copy and paste to “Drawable” folder in your resource.

Step 2: In our example, when we click a button, then a new dialog appear. So first we add a new Button for main layout and set Click event for this button. In the “onClick” method, see below:

<pre>Dialog d = new Dialog(MainActivity.this);

Window w = d.getWindow();
w.setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND, 
        WindowManager.LayoutParams.FLAG_BLUR_BEHIND);

d.setTitle("Hello"); // set the title for dialog
d.setContentView(R.layout.custom_dialog);

// textview & imageview in your custom layout.
TextView tv = (TextView) d.findViewById(R.id.textView);
ImageView img = (ImageView) d.findViewById(R.id.img);

tv.setText("Hello world");
img.setImageResource(R.drawable.img);
d.show();</pre>

Run and see the result :)

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s