简体   繁体   中英

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.

在此图像中有一个警报对话框,但我想从欢迎标题上方删除多余的空格

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.

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);

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(); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM