Ad

Space Top In AlertDialog On Tablets

- 1 answer

Hi

I develop mobile app for calculation Matrices. Today I figured out one intresting problem. In custom AlertDialog appears space in top, only on Tablets:

enter image description here

While I run the app in SmartPhones I don't have this problem:

enter image description here

XML code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:tools="http://schemas.android.com/tools"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

    <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimary"
            android:text="@string/text_save_result"
            android:padding="5dp"
            android:paddingStart="25dp"
            android:textStyle="bold"
            android:textSize="20sp"
            android:textColor="@color/white"
            tools:ignore="RtlSymmetry"/>

    <EditText
            android:id="@+id/edt_name_save"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/hint_enter_name_saving"
            android:inputType="text"
            android:layout_marginTop="10dp"
            android:layout_marginStart="10dp"
            android:layout_marginEnd="10dp"/>

    <TextView
            android:id="@+id/tv_error"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="@color/red"
            android:drawableStart="@drawable/ic_error"
            android:layout_marginStart="10dp"
            android:gravity="center_vertical"
            android:layout_marginEnd="10dp"
            android:drawablePadding="4dp"
            android:visibility="gone"/>

    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

        <Button
                android:id="@+id/btn_save"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                style="@style/Widget.AppCompat.Button.Borderless"
                android:text="@string/text_btn_save"
                android:textColor="@color/colorPrimary"/>

        <Button
                android:id="@+id/btn_cancel"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                style="@style/Widget.AppCompat.Button.Borderless"
                android:text="@string/text_btn_cancel"
                android:textColor="@color/colorPrimary"/>
    </LinearLayout>

</LinearLayout>

Java code of AlertDialog:

    package com.whitedeveloper.matrix.alerts;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.whitedeveloper.matrix.R;
import com.whitedeveloper.matrix.instance.SavingInstance;

public class AlertDialogSave extends Dialog {
    public interface CallBackFromAlertDialogSave {
        void callBack(String name);
    }

    private CallBackFromAlertDialogSave callBack;
    private TextView tvError;

    public AlertDialogSave(@NonNull Context context, CallBackFromAlertDialogSave callBack) {
        super(context);
        this.callBack = callBack;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.alert_dialog_name_saving);

        init();
    }

    private void init() {
        tvError = findViewById(R.id.tv_error);

        final Button btnSave = findViewById(R.id.btn_save);
        final Button btnCancel = findViewById(R.id.btn_cancel);
        final EditText edtName = findViewById(R.id.edt_name_save);
        edtName.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if (tvError.getVisibility() == View.VISIBLE)
                    tvError.setVisibility(View.GONE);
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

        btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!edtName.getText().toString().trim().equals("")) {
                    if (SavingInstance.isNameExisted(getContext(), edtName.getText().toString())) {
                        if (tvError.getVisibility() == View.GONE) {
                            tvError.setVisibility(View.VISIBLE);
                            tvError.setText(R.string.text_existed_already);
                        }
                        return;
                    }
                    callBack.callBack(edtName.getText().toString());
                    hide();
                    dismiss();
                } else
                if (tvError.getVisibility() == View.GONE) {
                    tvError.setVisibility(View.VISIBLE);
                    tvError.setText(R.string.cannot_be_empty);
                }

            }
        });

        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                hide();
                dismiss();
            }
        });
    }
}

I don't understand whats going on? Where am I wrong? Please help:))

Ad

Answer

I solved this weird problem. I figured out that I use Dialog instead AlertDialog.

Ad
source: stackoverflow.com
Ad