Developer_99
Developer_99

Reputation: 155

How to remove extra space from Alert dialog in android

In My android app, It is an alert dialog but there is one problem It have extra space from top of the title . How to remove extra space can anyone help me.

In this image there is an alert dialog but I want to remove extra spaces from above welcome title

java code

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_welcome_from);

    final AlertDialog.Builder builder =
            new AlertDialog.Builder(this, R.style.AppCompatAlertDialogStyle);

    Typeface tfmain = Typeface.createFromAsset(getAssets(), fontFooterpath);
    Typeface tfContent = Typeface.createFromAsset(getAssets(), fontMain);

    CustomTFSpan tfSpan = new CustomTFSpan(tfContent);
    SpannableString spannableString = new SpannableString("Welcome");
    spannableString.setSpan(tfSpan, 0, spannableString.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    builder.setTitle(spannableString);

    CustomTFSpan tfSpan1 = new CustomTFSpan(tfmain);
    SpannableString spannableString1 = new SpannableString("Hello");
    spannableString1.setSpan(tfSpan1, 0, spannableString1.length(), spannableString1.SPAN_EXCLUSIVE_EXCLUSIVE);
    builder.setMessage(spannableString1);

    builder.setPositiveButton("START", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            Intent ii = new Intent(WelcomeFromActivity.this, Question1Activity.class);
            startActivity(ii);
        }
    });
    builder.setCancelable(false); //for prevent outside touch
    builder.show();

}

Can anyone know that how to remove extra space from alert dialog title.

Upvotes: 2

Views: 3035

Answers (2)

Bhaven Shah
Bhaven Shah

Reputation: 692

There are server ways:-

  1. Alert Dialog

    AlertDialog.Builder builder;
         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
             builder = new AlertDialog.Builder(activityContext, android.R.style.Theme_Material_Light_Dialog_NoActionBar); // To remove extra space
         } else {
             builder = new AlertDialog.Builder(activityContext);
         }
    
    builder.setTitle(dialogTitle);
    builder.setButton(AlertDialog.BUTTON_NEUTRAL, "OK", new DialogInterface.OnClickListener() {
           @Override
           public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss(); // Close dialog
           }
        });
    builder.setCancelable(false); //to make Dialog's outside non-touchable
    builder.show(); // to show dialog
    
  2. Custom Dialog

    Make Custom Design in style.xml file and you can add more values according to your requirement.

    <style name="CustomDesign" parent="Theme.AppCompat.Light.Dialog.Alert">>
          <item name="android:layout_width">wrap_content</item>
          <item name="android:layout_height">wrap_content</item>
          <item name="android:windowBackground">@null</item>
          <item name="android:windowNoTitle">true</item>
    </style>
    

    Give this style to Alert Dialog or any dialog.

    For Alert Dialog :-

    AlertDialog.Builder builder =
        new AlertDialog.Builder(this, R.style.CustomDesign);
    

    For Dialog :-

    Dialog customDialog= new Dialog(this,R.style.CustomDesign);
    
  3. Dialog

    Dialog dialog = new Dialog(context);
    dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE); // To remove extra space
    dialog.setContentView(R.layout.layout); // to set custom layout
    dialog.show();
    

Upvotes: 3

tahsinRupam
tahsinRupam

Reputation: 6405

Use a custom style for your AlertDialog.

Add the following style to your style.xml.

<style name="MyCustomTheme" parent="Theme.AppCompat.Light.Dialog.Alert">>
        <item name="android:layout_width">fill_parent</item>
        <item name="android:layout_height">fill_parent</item>
        <item name="android:windowBackground">@null</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowIsFloating">false</item>     
</style>

Set the theme to your AlertDialog in the activity:

final AlertDialog.Builder builder =
            new AlertDialog.Builder(this, R.style.MyCustomTheme);

Upvotes: 4

Related Questions