Reputation: 11545
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 stuck with margin and padding of it. I don't want unwanted padding which is showing and also i want to set some top margin to title, i am using LinearLayout.LayoutParams
but it has no effect. please suggest what to do to handle it.thanks
Code :
dialog = new AlertDialog.Builder(context, R.style.DialogTheme);
TextView title = new TextView(context);
title.setTextColor(ContextCompat.getColor(context, R.color.black));
title.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
title.setTypeface(Typeface.DEFAULT_BOLD);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(0, 20, 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", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
dialog.show();
Upvotes: 2
Views: 13628
Reputation: 729
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
TextView title = new TextView(this);
title.setTextColor(ContextCompat.getColor(this, android.R.color.black));
title.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
title.setTypeface(Typeface.DEFAULT_BOLD);
LinearLayout.LayoutParams lp = new LinearLayout.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", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
dialog.show();
Replace this code it will work
Upvotes: 8
Reputation: 5865
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"?>
<RelativeLayout xmlns: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">
<LinearLayout
android:id="@+id/header"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:orientation="horizontal">
<TextView
android: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>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/header"
android:padding="10dp">
<TextView
android: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" />
<Button
android: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>
Upvotes: 0
Reputation: 54
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.
Upvotes: 1