Dialog Boxes in Android: A Beginner’s Guide

 

Dialog :

Dialog, As in previous tutorial on android alertdialog example we have seen positive negative and neutral buttons  where as in current android custom alertdialog tutorial we will be designing the box which is being displayed.

android custom dialog in android is used to show a message in the form of a dialog with a button.

In case of form filling if you miss any option then a dialog box will open up asking to fill all the fields in this way a dialog box will be used, not only this we can use this type of box even with android popup dialog refer

https://androidcoding.in/2016/09/26/android-tutorial-popup-window/

 

Dialog video tutorial:

MainActivity.java

Providing the code for android alertdialog implementation.

public class MainActivity extends Activity {

	Button buttonClick,submitbtn;
	Dialog dialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		buttonClick = (Button) findViewById(R.id.buttonClick);

		buttonClick.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
			 Opendialog();
			}

		});

	}
	private void Opendialog() {
		dialog = new Dialog(MainActivity.this);
		dialog.setTitle("AndroidCoding.in");
		dialog.setContentView(R.layout.dialog);

		submitbtn = (Button) dialog.findViewById(R.id.submitbtn);

		submitbtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
			 dialog.dismiss();
			}

		});

		dialog.show();
	}

 

Adding textview and button to xml screen which is to be displayed in the custom alert-box.You can customize the screen according to your requirement.

 

activity_main.xml :

We are designing this android alertdialog here in this layout file you can add your own customization’s if required.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.androidcoding.dialog_box.MainActivity" >


     <TextView
         android:id="@+id/textView"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="Dialog-Box"
         android:layout_marginTop="100dp"
         android:layout_gravity="center"
         android:textAppearance="?android:attr/textAppearanceLarge" />

   <Button
        android:id="@+id/buttonClick"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show Dialog"
        android:layout_gravity="center"
        android:layout_marginTop="80dp" />

</LinearLayout>

Now we have to design dialog box, we can set textview, edittext, buttons and more depending upon requirement.

Here i am showing you a default dialog-box where i have taken a textview and button  but you can also add a webview, or video view, imageview to even show ads on the dialog box with button below it. to close or reedem the offers.

 

dialog.xml :

In this layout we will be adding the required fields in the dialog, so here we are adding a simple android textview and button but you can even add more fields and align them properly then the rest is similar to normal coding.

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="400dp"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textDialog"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="20dp"
        android:text="This is the Dialog Box"
        android:textSize="20dp" />

    <Button
        android:id="@+id/submitbtn"
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:layout_gravity="center"
        android:layout_marginTop="20dp"
        android:text=" Submit " />

</LinearLayout>


 

AndroidManifest.xml :

There is no special permission required for this app you can leave this as it is.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.androidcoding.abhi.dialog_box"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.androidcoding.abhi.dialog_box.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>



Android dialog Output :

This screen below depicts the usage of dialog box in android implementation.

dialog

 

 

 

 

 

 

 

 

 

 

 

If you have any query in this tutorial on android dialog box do let us know in the comment section below.

Like and share this tutorial for more interesting android tutorials.

Show Buttons
Hide Buttons
Read previous post:
Checkbox in Android: Step-by-Step Tutorial

Android CheckBox : Android CheckBox, Generally when there is form or any any other kind where we have to choose...

Close