2

I'm creating a AlertDialog using only code and inserting a view for check box. But the check is with a strange gravity behaviour.

How I could put text+box in the middle or at least text on the left with padding for the start.

Thanks.

private void openFilterDialog() {
    final AlertDialog.Builder builder = new AlertDialog.Builder(Objects.requireNonNull(getContext()));
    builder.setTitle(getString(R.string.filter));

    // Set up the input
    final CheckBox hideNoResponseBox = new CheckBox(getContext());
    //Put the checkBox on the right side
    hideNoResponseBox.setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
    hideNoResponseBox.setText(R.string.hide_no_responses);
    hideNoResponseBox.setChecked(hideNoResponse);
    hideNoResponseBox.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
    hideNoResponseBox.setGravity(View.TEXT_ALIGNMENT_CENTER);

    builder.setView(hideNoResponseBox);

    // Set up the buttons
    builder.setPositiveButton(getString(R.string.do_filter), new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            if (Utils.isDoubleClick()) return;
            hideNoResponse = hideNoResponseBox.isChecked();

            showLoadingDialog(true);
            hideListItem(adapter.getDistanceArrayList());
            dialog.dismiss();
        }
    });
    builder.setNegativeButton(getString(R.string.str_cancel), new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
        }
    });

    builder.show();
}

enter image description here

Canato
  • 3,598
  • 5
  • 33
  • 57

2 Answers2

1

The CheckBox's setGravity() method which is inherited from the View class, takes Gravity constants as values.

https://developer.android.com/reference/android/view/Gravity

EDIT:

You can try to set the text gravity using the layout params. The LayoutParams for an AlertDialog are the LinearLayout's LayoutParams as stated here: What LayoutParams should be used in an AlertDialog?

You just need to use the LayoutParam's setGravity() method.

And about the padding you can try to cast the CheckBox's parent to a ViewGroup and set the padding there, like this:

https://stackoverflow.com/a/38691663/1403997

4gus71n
  • 3,717
  • 3
  • 39
  • 66
  • just to be clear, this is right, but still need to know how put the padding or how put the checkbox with the text on the middle. =) – Canato Jul 12 '18 at 18:29
  • Coolio didn't realize that you needed that as well. I added some info about how to center the text and adding the padding. Lemme know if it works. – 4gus71n Jul 13 '18 at 12:58
  • I was looking for you edit, but I have no LinearLayout or view with ID to get to the LinearLayout. I don't understood your suggestion to cast the CB into a ViewGroup. – Canato Jul 16 '18 at 12:55
0
private void openFilterDialog() {
    final AlertDialog.Builder builder = new AlertDialog.Builder(Objects.requireNonNull(getContext()));
    builder.setTitle(getString(R.string.filter));

    // Set up the input
    final CheckBox hideNoResponseBox = new CheckBox(getContext());
    //Put the checkBox on the right side
    hideNoResponseBox.setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
    hideNoResponseBox.setText(R.string.hide_no_responses);
    hideNoResponseBox.setChecked(hideNoResponse);
    hideNoResponseBox.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);

    hideNoResponseBox.setGravity(Gravity.START | Gravity.CENTER_VERTICAL);
    hideNoResponseBox.setTextAlignment(View.TEXT_ALIGNMENT_TEXT_START);

    int px = Math.round(TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_DIP, 16, getResources().getDisplayMetrics()));

    hideNoResponseBox.setPadding(px,0,0,0);

    builder.setView(hideNoResponseBox);


    // Set up the buttons
    builder.setPositiveButton(getString(R.string.do_filter), new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            if (Utils.isDoubleClick()) return;
            hideNoResponse = hideNoResponseBox.isChecked();

            showLoadingDialog(true);
            hideListItem(adapter.getDistanceArrayList());
            dialog.dismiss();
        }
    });
    builder.setNegativeButton(getString(R.string.str_cancel), new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
        }
    });

    builder.show();
}
Canato
  • 3,598
  • 5
  • 33
  • 57