Skip to content Skip to sidebar Skip to footer

How To Set Alertdialog Custom Title Top Margin And Remove Unwanted Padding?

I am stuck with a margin and padding issue of a title of AlertDialog.Builder, what i am doing is i want the title in center so i am using setCustomTitle i am able to do so but stu

Solution 1:

AlertDialog.Builderdialog=newAlertDialog.Builder(this);
TextViewtitle=newTextView(this);
title.setTextColor(ContextCompat.getColor(this, android.R.color.black));
title.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
title.setTypeface(Typeface.DEFAULT_BOLD);
LinearLayout.LayoutParamslp=newLinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(0, 20, 0, 0);
title.setPadding(0,30,0,0);
title.setLayoutParams(lp);
title.setText("Dialog");
title.setGravity(Gravity.CENTER);
dialog.setCustomTitle(title);
dialog.setMessage("Dialog box with custom title view ");
dialog.setCancelable(false);
dialog.setPositiveButton("OK", newDialogInterface.OnClickListener() {
    @OverridepublicvoidonClick(DialogInterface dialog, int which) {

    }
});
dialog.show();

Replace this code it will work

Solution 2:

You can avoid setting up all this methods to your AlertDialog by using DialogFragment. Just use getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE); in onCreateView method and make your own custom view for the dialog in the way you create a common fragment. It's a better way to make proper dialogs.

Solution 3:

I suggest you to use DialogFragment to show custom layout as alert Dialog its very easy and we can customize our dialog as per requirement..for more detail about it : https://developer.android.com/reference/android/app/DialogFragment.html

Below is the layout as per your requirement, You can use the below xml layout to show your custom layout.

<?xml version="1.0" encoding="utf-8"?><RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_margin="10dp"><LinearLayoutandroid:id="@+id/header"android:layout_width="match_parent"android:layout_height="?attr/actionBarSize"android:background="?attr/colorPrimary"android:orientation="horizontal"><TextViewandroid:layout_width="match_parent"android:layout_height="match_parent"android:layout_marginLeft="@dimen/margin10dp"android:layout_weight="1"android:gravity="center"android:text="My Dialog"android:textColor="@color/white"android:textSize="22dp"android:textStyle="bold" /></LinearLayout><RelativeLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@+id/header"android:padding="10dp"><TextViewandroid:id="@+id/textView2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:gravity="center"android:text="My Dialog Box Description !"android:textAppearance="?android:attr/textAppearanceMedium"android:textSize="20dp" /><Buttonandroid:id="@+id/btnSubmit"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentEnd="true"android:layout_alignParentRight="true"android:layout_below="@+id/textView2"android:background="@android:color/transparent"android:text="OK"android:textSize="22dp" /></RelativeLayout></RelativeLayout>

Post a Comment for "How To Set Alertdialog Custom Title Top Margin And Remove Unwanted Padding?"