Custom Dialog Tutorial for Android

Purpose

We can also make custom dialog in Android by extending class from Dialog class. In custom dialog we can make a dialog as required layout which is not possible in AlertDialog.

You can download the complete source code from here.

InfoDialog.java

public class InfoDialog extends Dialog implements android.view.View.OnClickListener{
	private TextView tvTitle;
	private TextView tvDescription;
	private ImageView ivEnterMain;
	private Context context;
	private String title;
	private String description;
	private String URL;
	private Intent intent;

	public InfoDialog(Context context, String title, String description,String URL, Intent intent){
	    super(context);
    	    this.context = context;
	    this.title = title;
	    this.description = description;
	    this.URL = URL;
	    this.intent = intent;
	}

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.infodialog);
	tvTitle = (TextView) findViewById(R.id.tv_titleMain);
	tvDescription = (TextView) findViewById(R.id.tv_descriptionMain);
	ivEnterMain = (ImageView) findViewById(R.id.iv_EnterMain);
	tvTitle.setText(title);
        tvDescription.setText(description);
        ivEnterMain.setOnClickListener(this);
    }

    public void onClick(View v) {
        context.startActivity(intent);
        dismiss();
    }
}

infodialog.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/LinearLayoutMain"
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_height="wrap_content" android:layout_width="wrap_content">
	<LinearLayout android:layout_width="wrap_content"
		android:layout_height="wrap_content" android:id="@+id/llh_dialogbox_infoMain"
		android:orientation="horizontal" android:background="#808080">
		<LinearLayout android:layout_width="wrap_content"
			android:layout_height="wrap_content" android:id="@+id/llv_tvTitleDetailsMain"
			android:orientation="vertical">
			<TextView android:text="TextView01" android:layout_width="wrap_content"
				android:layout_height="wrap_content" android:id="@+id/tv_titleMain"></TextView>
			<TextView android:text="TextView02" android:layout_width="wrap_content"
				android:layout_height="wrap_content" android:id="@+id/tv_descriptionMain"
				android:textSize="10px"></TextView>
		</LinearLayout>
		<ImageView android:layout_width="wrap_content"
			android:layout_height="wrap_content" android:id="@+id/iv_EnterMain"
			android:layout_gravity="center_vertical" android:src="@drawable/forward_arrow"></ImageView>
	</LinearLayout>
</LinearLayout>

Splash.java

public class Splash extends Activity implements OnClickListener{
    private InfoDialog infoDialog;
    private Button btnShowDialog;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        btnShowDialog = (Button) findViewById(R.id.btn_dialog);
        btnShowDialog.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
 	Intent myIntent = new Intent(this, Activity2.class);
	infoDialog = new InfoDialog(this, "Title", "Description", "http://www.google.com", myIntent);
	infoDialog.show();
    }
}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical" android:layout_width="fill_parent"
	android:layout_height="fill_parent">
	<TextView android:layout_width="fill_parent"
		android:layout_height="wrap_content" android:text="@string/hello" />
	<Button android:layout_width="wrap_content"
		android:layout_height="wrap_content" android:id="@+id/btn_dialog"
		android:text="Show Dialog"></Button>
</LinearLayout>

Activity2.java

public class Activity2 extends Activity {
	private Context context;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		RelativeLayout relativeLayout = new RelativeLayout(this);
		Button button = new Button(this);
		button.setText("Go Back");
		context = this;
		button.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				startActivity(new Intent(context, Splash.class));
				finish();
			}
		});
		relativeLayout.addView(button);
		LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT,
				LayoutParams.FILL_PARENT);
		setContentView(relativeLayout, params);
	}

}
Advertisements
This entry was posted in Android, Tutorials and tagged , , , , . Bookmark the permalink.

3 Responses to Custom Dialog Tutorial for Android

  1. Pingback: Pop Ups(Toast, AlertDialog, Layout) Tutorial for Android | Farhan's Blogs & Portal

  2. usman says:

    Always take a snapshot of the output dialog in order to demonstrate it.

  3. Mikel says:

    thank’s you for tutorial. A greeting for Spain

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