Sunday, July 12, 2020

How to set a custom layout in Alert Dialog In Android

How to set a custom layout in Alert Dialog In Android.


In Android we can set a custom layout in Alert Dialog making it look better.
For this we need to create layout file and inflate this layout file in Alert Dialog.


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#004d66">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:orientation="horizontal"
        android:gravity="center_vertical"
        android:background="#00394d"
        android:padding="5dp">


        <TextView
            android:id="@+id/textViewDialogSingleButtonTitle"
            android:layout_width="match_parent"
            android:layout_height="30dp"
            android:textStyle="bold"
            android:paddingLeft="5dp"
            android:paddingRight="5dp"
            android:text="Dilaog Details"
            android:textColor="#ffffff"
            android:gravity="center_vertical"
            android:textSize="18dp" />


    </LinearLayout>


    <TextView
        android:id="@+id/textViewDialogSingleButtonMessage"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:paddingLeft="5dp"
        android:paddingRight="5dp"
        android:text="Dilaog Title"
        android:textColor="#ffffff"
        android:textSize="16dp" />

        <Button
            android:id="@+id/buttonOKDialogSingleButton"
            android:background="@drawable/button_gradient"
            android:layout_marginTop="15dp"
            android:textColor="#FFFFFF"
            android:textSize="18dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="         OK         "
            android:layout_marginBottom="10dp"
            />
    </LinearLayout>



Inflate the layout in Alert Dialog



AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
ViewGroup viewGroup = findViewById(android.R.id.content);
View dialog = LayoutInflater.from(this).inflate(R.layout.custom_dialog_single_button, viewGroup, false);
builder.setView(dialog);
final AlertDialog alertDialog = builder.create();

TextView textViewTtile=(TextView) dialog.findViewById(R.id.textViewDialogSingleButtonTitle);
TextView textViewMessage=(TextView) dialog.findViewById(R.id.textViewDialogSingleButtonMessage);
Button btnOK=(Button)dialog.findViewById(R.id.buttonOKDialogSingleButton);

textViewTtile.setText("  Title");
textViewMessage.setText("Dialog Message");

alertDialog.show();

btnOK.setOnClickListener(new View.OnClickListener() {
    @Override    public void onClick(View view) {
        if(alertDialog.isShowing())
            alertDialog.dismiss();
    }
});



Screenshot


No comments:

Post a Comment