Friday, 17 July 2015

Custom Dialog In Android

Hello friends, In last post we have seen how to create alert dialog. Alert dialog is a default functionality in Android.

If you want to create your own dialog then what to do...??????

Let's start to create Custom Dialog in Android.

In this example i applied screen's background and Button with background, its border. So you can learn to customize button also.

Same like you can create a design for header, footer and any components according to your project.

Step 1: You need to create your own design for dialog.

Here I provide you sample code.

File : activity_custom_alert_dialog.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="@drawable/mainbg">

    <Button
        android:id="@+id/btn_click"
        android:layout_width="250dp"
        android:layout_height="45dp"
        android:text="Click Me"
        android:layout_gravity="center"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:layout_marginTop="20dp"
        android:typeface="serif"
        android:textStyle="bold"
        android:background="@drawable/btnbg"/>

</LinearLayout>

Step 2: Do the java code into your activity file.

File : CustomAlertDialog.java

protected void showCustomDialog() {
cad_dialog = new Dialog(CustomAlertDialog.this,android.R.style.Theme_Translucent);
cad_dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

cad_dialog.setCancelable(true);
cad_dialog.setContentView(R.layout.customdialog);

edt_feedback = (EditText) cad_dialog.findViewById(R.id.etfeedback);
btn_ok = (Button) cad_dialog.findViewById(R.id.btnok);
btn_cancel = (Button) cad_dialog.findViewById(R.id.btncancel);

cad_dialog.show();

btn_ok.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "You have clicked OK...!!!",                                                 Toast.LENGTH_LONG).show();
cad_dialog.dismiss(); // Here you can perform any action as per your requirement.
}
});

btn_cancel.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
cad_dialog.dismiss();
}
});

}

Step 3: Output


























You can download full source code from here. CustomAlertDialog.zip

Enjoy Coding...!!!

No comments:

Post a Comment