HOW TO CREATE THE CUSTOM DIALOG BOX IN ANDROID

HOW TO CREATE THE CUSTOM DIALOG BOX IN ANDROID:

1.Create a custom layout in xml (example :alert_dialog.xml)

2. Write this code in java class

Complete code in XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/popup_element"
    android:layout_width="400dp"
    android:layout_height="200dp"

    android:background="@drawable/popupbg"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="22dp"
        android:layout_marginTop="45dp"
        android:src="@drawable/tyretronicswarning" />

    <TextView
        android:id="@+id/txt_dia"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/imageView1"
        android:layout_margin="10dp"
        android:layout_marginLeft="21dp"
        android:layout_toRightOf="@+id/imageView1"
        android:text="Do you realy want to exit ?"
        android:textColor="@color/aqua"
        android:textSize="20dp"
        android:textStyle="bold" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/imageView1"
        android:layout_marginTop="21dp"
        android:layout_toRightOf="@+id/imageView1"
        android:gravity="end"
        android:orientation="horizontal" >
//add the multiple button 
        <Button
            android:id="@+id/btn_yes"
            android:layout_width="100dp"
            android:layout_height="30dp"
            android:background="@drawable/curve_shap"
            android:clickable="true"
            android:text="Yes"
            android:textColor="#ffffff"
            android:textStyle="bold" />

        <Button
            android:id="@+id/btn_no"
            android:layout_width="100dp"
            android:layout_height="30dp"
            android:layout_marginLeft="50dp"
            android:background="@drawable/curve_shap"
            android:clickable="true"
            android:text="No"
            android:textColor="#ffffff"
            android:textStyle="bold" />
    </LinearLayout>

</RelativeLayout>


Java class :

Main Activity.Java:


final Dialog dialog = new Dialog(this);

background.setVisibility(View.INVISIBLE);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.popup);

Button no = (Button) dialog.findViewById(R.id.btn_no);
Button Yes  = (Button) dialog.findViewById(R.id.btn_yes);

dialog.getWindow().setBackgroundDrawable(
new ColorDrawable(android.graphics.Color.TRANSPARENT));
// primary  xml screen  hide activity
dialog.setCancelable(false);
//dispaly the alert dialog in correspond xml layout 
dialog.show();


yes.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub

Intent i = new Intent(getApplicationContext(),
VEHICLEINFO.class);
startActivity(i);

}
});
// on click listener
no.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub

Intent i = new Intent(getApplicationContext(),
your Activity.class);
startActivity(i);

}
});
}

output screenshort





Comments

Post a Comment

Popular posts from this blog

QR_CODE GENERATOR IN ANDROID