Tuesday 2 July 2013

Android custom dialog example

// siddhu vydyabhushana // 38 comments
In this tutorial, we show you how to create a custom dialog in Android. See following steps :
  1. Create a custom dialog layout (XML file).
  2. Attach the layout to Dialog.
  3. Display the Dialog.
  4. Done.
P.S This project is developed in Eclipse 3.7, and tested with Android 2.3.3.
Note
You may also interest to read this custom AlertDialog example.

1 Android Layout Files

Two XML files, one for main screen, one for custom dialog.
File : res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
 
    <Button
        android:id="@+id/buttonShowCustomDialog"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show Custom Dialog" />
 
</LinearLayout>
File : res/layout/custom.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
 
    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="5dp" />
 
    <TextView
        android:id="@+id/text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="#FFF" 
        android:layout_toRightOf="@+id/image"/>/>
 
     <Button
        android:id="@+id/dialogButtonOK"
        android:layout_width="100px"
        android:layout_height="wrap_content"
        android:text=" Ok "
        android:layout_marginTop="5dp"
        android:layout_marginRight="5dp"
        android:layout_below="@+id/image"
        />
 
</RelativeLayout>

2. Activity

Read the comment and demo in next step, it should be self-explorary.
File : MainActivity.java
package com.mkyong.android;
 
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
 
public class MainActivity extends Activity {
 
	final Context context = this;
	private Button button;
 
	public void onCreate(Bundle savedInstanceState) {
 
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
 
		button = (Button) findViewById(R.id.buttonShowCustomDialog);
 
		// add button listener
		button.setOnClickListener(new OnClickListener() {
 
		  @Override
		  public void onClick(View arg0) {
 
			// custom dialog
			final Dialog dialog = new Dialog(context);
			dialog.setContentView(R.layout.custom);
			dialog.setTitle("Title...");
 
			// set the custom dialog components - text, image and button
			TextView text = (TextView) dialog.findViewById(R.id.text);
			text.setText("Android custom dialog example!");
			ImageView image = (ImageView) dialog.findViewById(R.id.image);
			image.setImageResource(R.drawable.ic_launcher);
 
			Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
			// if button is clicked, close the custom dialog
			dialogButton.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					dialog.dismiss();
				}
			});
 
			dialog.show();
		  }
		});
	}
}

3. Demo

Start it, the “main.xml” layout is display.
android custom dialog example
Click on the button, display custom dialog “custom.xml” layout, if you click on the “OK” button, dialog box will be closed.
android custom dialog example

Download Source Code

Download it – Android-Custom-Dialog-Example.zip (16 KB)

38 comments:

  1. Hi,

    Please don't steal images or content from other websites.
    It doesn't look good.

    ReplyDelete
  2. You steal it from here.
    http://www.mkyong.com/android/android-custom-dialog-example/

    ReplyDelete
  3. No Guarantor credits are formed for everyone who is having a couple of overheads that are capricious yet out of the arrangement of ordinary people having packages f needs to meet yet habitually fail to do so all around cash slacks. Explore more : http://www.noguarantorloanspro.co.uk/

    ReplyDelete
  4. Android Tutorial Basic to Advance: Click Here
    Android User Interface Tutorial: Click Here

    ReplyDelete
  5. Thanks for sharing such nice information. keep sharing live blogs

    ReplyDelete
  6. Nice article you might have carried out below. My business is truly happy to see that. This is the incredibly helpful matter. keep that you're selected it up.Services & Support Page

    ReplyDelete
  7. Well Said, you have furnished the right information that will be useful to anyone at all time. Thanks for sharing your Ideas.
    Hadoop Training Chennai | Hadoop course in Chennai

    ReplyDelete
  8. It is a very nice article including a lot of viral content. I am going to share it on social media. Get the fireworks online / crackers online in chennai.

    ReplyDelete
  9. I feel really happy to have seen your webpage and look forward to so many more entertaining times reading here. Thanks once more for all the details.

    Best RPA Training in Chennai

    ReplyDelete
  10. Nice looking sites and great work. Pretty nice information. it has a better understanding. thanks for spending time on it.

    Best Industrial Training in Noida
    Best Industrial Training in Noida

    ReplyDelete
  11. Needed to compose you a very little word to thank you yet again regarding the nice suggestions you’ve contributed here.
    Hadoop Training Institute In chennai

    ReplyDelete
  12. Great Article… I love to read your articles because your writing style is too good, its is very very helpful for all of us and I never get bored while reading your article because, they are becomes a more and more interesting from the starting lines until the end.
    Selenium Training in Chennai | Selenium Training in Bangalore |Selenium Training in Pune | Selenium online Training

    ReplyDelete
  13. I have visited this blog first time and i got a lot of informative data from here which is quiet helpful for me indeed.
    apple service center chennai | apple iphone service center chennai | apple ipad service center chennai | apple mac service center chennai | ipad service center

    ReplyDelete
  14. Effective blog with a lot of information. Ijust Shared you the link below for ACTE .They really provide good level of training and Placement,I just Had Data Science Classes in ACTE , Just Check This Link You can get it more information about the Data Science course.
    Java training in chennai | Java training in annanagar | Java training in omr | Java training in porur | Java training in tambaram | Java training in velachery

    ReplyDelete
  15. Hey guy's i have got something to share from my research work
    Louiz
    Redox-os
    https://diigo.com/0hmsvz

    ReplyDelete
  16. This comment has been removed by the author.

    ReplyDelete
  17. Great post. keep sharing such a worthy information.
    Salesforce Training in Chennai

    ReplyDelete