简体   繁体   中英

Android documentation differs from Android Studio warning

I'm currently implementing an AlertDialog in which there is a CheckBox . In order to do that, I inflate a view containing a CheckBox and then I add it to my Dialog using the setView(View) method, but by proceeding that way I get huge padding. From what I read, a solution to that problem would apparently be to use the setView(View, int, int, int, int) method (which sets the padding for all direction) instead, but when I try to implement that android studio gives me an error (that can be avoided by adding @SuppressLint("RestrictedApi") but I don't know what that does nor means, so if you know don't hesitate to explain to me) and then says that this method is deprecated, the weird thing is that in the documentation (right here ), there is nothing saying that this method is deprecated, so I would like to know, in these kinds of moment, should I trust the documentation or androidStudio? (and also if it is a good idea to use the second method)

here is the code of the AlertDialog :

@SuppressLint("RestrictedApi")
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {

    View checkBoxView = View.inflate(getContext(), R.layout.checkbox, null);
    CheckBox checkBox = (CheckBox) checkBoxView.findViewById(R.id.checkbox_dontaskmeagain);

    checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            SharedPrefUtils.setShowAlertDialog(getContext(), b);
        }
    });

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setMessage(R.string.alert_everyday_dialog_message);
    builder.setTitle(R.string.alert_dialog_title)
            .setView(checkBoxView, 0, 0, 0, 0) //this apparently is deprecated
            .setPositiveButton(R.string.alert_dialog_delete, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    mListener.onEverydayDialogPositiveClick(DeleteTaskEverydayDialog.this);
                }
            })
            .setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    mListener.onEverydayDialogNegativeClick(DeleteTaskEverydayDialog.this);
                }
            });
    return builder.create();
}

Note that this is a custom class that extends from AlertDialog in which there are some other functions but I don't think that they are needed, if you think so, tell me and I'll provide them

You're looking at the wrong docs - namely AlertDialog . The code you are calling is on AlertDialog.Builder . It does not have any docs since it is annotated with @hide and thus excluded from SDK and its documentation. It also has @RestrictTo and @Deprecated annotations as seen in the support library source .

Some rationale for hiding is given in the javadoc:

This is currently hidden because it seems like people should just be able to put padding around the view.

Generally speaking, you should not be calling hidden or restricted methods. Find some other way to achieve your goal.

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