Unknown attribute onItemSelected/onCheckedChanged

Viewed 1561

I created SwitchCompat and added android:onCheckedChanged. everything works fine, but Android Studio marks this attribute as warning with description.

Unknown attribute android:onCheckedChanged.

The same case is with AppCompatSpinner with android:onItemSelected.

Why Android Studio displaying this warning? My Android Studio version is 3.5.1

screenshot with warning

2 Answers

That is because SwitchCompat actually does not have such attribute (see documentation), so you can't assign the listener in the XML. Try implementing an onClickListener instead or assign the OnCheckedChanged listener outside of XML, such as:

  final SwitchCompat switchButton = (SwitchCompat) view.findViewById(R.id.switch_button);
  switchButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
      //do stuff
    }
  });

Edit: You can also get the switch button from the dataBinding object in java code by calling binding.switch_button which returns the SwitchButton directly.

Here is a code for my CheckBox Try somethiing like this in your java file etc. remeber to call the and asign the variable of the check box or wich ever

Your problem is that AS does not recognize the attribute IN the XML try Hardcoding the ifchecked etc in the coding side, as you call the attribute AS finds that but it is a "custom" attribute if you understand wha I am saying

 CheckBox chk=findViewById(R.id.chk1);
            if (chk.isChecked()) {
                Uri.Builder builder=new Uri.Builder();
                builder.scheme("https")
                        .authority("www.smartpractice.co.za")
                        .appendPath("files-upload-ruben.asp")
                        .appendQueryParameter("MyForm", "Yes")
                        .appendQueryParameter("ClientID", clientId)
                        .appendQueryParameter("Username", email)
                        .appendQueryParameter("Pwd", pwd)
                        .appendQueryParameter("Category", Item)
                        .appendQueryParameter("ClientName", Item2)
                        .appendQueryParameter("NoEmail", "Yes");
                myURL=builder.build().toString();
            } else {
                Uri.Builder builder4=new Uri.Builder();
                builder4.scheme("https")
                        .authority("URL)
                        .appendPath("CheckBox chk=findViewById(R.id.chk1);
            if (chk.isChecked()) {
                Uri.Builder builder=new Uri.Builder();
                builder.scheme("https")
                        .authority("www.FakeSite.co.za")
                        .appendPath("ServerFile.asp")
                        .appendQueryParameter("MyForm", "Yes")
                        .appendQueryParameter("ClientID", clientId)
                        .appendQueryParameter("Username", email)
                        .appendQueryParameter("Pwd", pwd)
                        .appendQueryParameter("Category", Item)
                        .appendQueryParameter("ClientName", Item2)
                        .appendQueryParameter("NoEmail", "Yes");
                myURL=builder.build().toString();
            } else {
                Uri.Builder builder4=new Uri.Builder();
                builder4.scheme("https")
                        .authority("www.fakeSite.co.za")
                        .appendPath("ServerFile.asp")
                        .appendQueryParameter("MyForm", "Yes")
                        .appendQueryParameter("ClientID", clientId)
                        .appendQueryParameter("Username", email)
                        .appendQueryParameter("Pwd", pwd)
                        .appendQueryParameter("Category", Item)
                        .appendQueryParameter("ClientName", Item2)
                        .appendQueryParameter("NoEmail", "");
                myURL=builder4.build().toString();")
                        .appendQueryParameter("MyForm", "Yes")
                        .appendQueryParameter("ClientID", clientId)
                        .appendQueryParameter("Username", email)
                        .appendQueryParameter("Pwd", pwd)
                    .appendQueryParameter("Category", Item)
                    .appendQueryParameter("ClientName", Item2)
                    .appendQueryParameter("NoEmail", "");
            myURL=builder4.build().toString();
Related