Search code examples
androidandroid-alertdialogflicker

AlertDialog - proper way to build it. Now blink and flick opening


In my project I am using a simple AlertDialog to enter some information. My problem is that when I go to open this AlertDialog I have a flicker for a very short time which is not correct. I would like to know what I'm wrong and above all what should be the correct way to build it because I can't find a solution around, if not that of changing objects. But I would really need the AlertDialog

    private void insertNameManually(String hint, int mType) {
    AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getContext());
    LayoutInflater inflater = this.getLayoutInflater();
    View dialogView = inflater.inflate(R.layout.alert_dialog, null);
    EditText editText = dialogView.findViewById(R.id.editTextName);
    Button clearTextButton = dialogView.findViewById(R.id.clearText);
    Button confirmButton = dialogView.findViewById(R.id.confirmButton);
    dialogBuilder.setView(dialogView);
    dialogBuilder.setTitle(R.string.std_choose);
    confirmButton.setOnClickListener(v -> {

        name = editText.getText().toString();

        name = hint;
        new Handler().postDelayed(() -> {
            MyFragment.this.type = mType;
            Intent data = new Intent();
            data.putExtra("type", type);
            activity.setResult(Activity.RESULT_OK, data);
            activity.finish();
        }, 275);
    });

    editText.setText(hint);

    clearTextButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            editText.setText("");
        }
    });

    AlertDialog alertDialog = dialogBuilder.create();
    alertDialog.show();
}

Solution

  • Solution: You can try by adding flag to your alert dialog, WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED;

    You can try by switching to a DialogFragment can help to remove the flicker.

    Tip: Just want add here by looking at your code instead of,

      AlertDialog alertDialog = dialogBuilder.create();
      alertDialog.show();
    

    You can do this way, will create and show the alert dialog.

    dialogBuilder.show();