Tuesday, 9 September 2014

Custom dialog box in android with Example

Custom dialog box in android with Example

In this android application development tutorial i am going to demonstrate how to create custom dialog in android. Android have many features and while creating a dialog in the android we just don't want to be limited to the default theme of an android dialog.

So, In this tutorial you will learn how to create your own custom dialog in android.

How to Create Custom Dialog Box in android



Go to File -> new and  Create a New Android Application Project 

Now our new android project is ready and now as we want to create custom dialog box then we should have a layout for our Custom Dialog.


Now again Go to File -> New and select Android XML File and name it customdialog.xml

As we are going to add image in our custom android dialog box simply drag any image in drawable folder and then copy it there.

Now navigate to image and media and then simply click and drag ImageView in to the layout.



And now select your image for your custom dialog box and then simply add some message there and we are done with the designing of android custom dialogbox.


Here is the code for customdialog.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/imageView1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="46dp"
        android:text="Heyyy!! I am cute cat"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="26dp"
        android:src="@drawable/cat" />
</RelativeLayout>
 Now again navigate to our Activity_main.xml and then simply drag and drop one button.

Navigate to our MainActivity.java file and then simply create onclick method for the button and write the following code.

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    Button S = (Button) findViewById(R.id.button1);
    S.setOnClickListener(new OnClickListener(){
        public void onClick(View arg0) {
            Dialog CD = new Dialog(MainActivity.this);
            CD.setContentView(R.layout.customdialog);
            CD.setTitle("This is custom dialog box");
            CD.show();           
        }
    });
    }
}
Now we are done!

Now run your app and then simply click on Show my custom dialogbox button and then it will show you the custom dialogbox.



You have successfully created awesome custom dialog box in android. If you have any question about creating custom dialog box in android then feel free to ask!

Never Miss a Single Android Tutorial!!!


Share This

No comments:

Post a Comment

Designed By Blogger Templates