Error message about 'DrawableCompat'

I’m trying to run the “androidWidgets” example, and I’m getting an error message on a couple of lines, which I don’t understand. The example works, though, so I guess they’re just warning messages. The error message is:

The name "DrawableCompat" cannot recognized

The lines getting the error are in the Switch tab, lines 28 and 29:

int[][] states = new int[][]{
  new int[] {-android.R.attr.state_checked},
  new int[] {android.R.attr.state_checked},
};

int[] thumbColors = new int[] {
  Color.WHITE,
  Color.GREEN,
};

int[] trackColors = new int[] {
  Color.WHITE,
  Color.WHITE,
};

void switchBtn(int x, int y) {  
  Switch switchBtn = new Switch(activity);
  LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
  layoutParams.setMargins(x, y, 200, 190);
  switchBtn.setLayoutParams(layoutParams);
  switchBtn.setText("OFF");
  switchBtn.setScaleX(2.0);
  switchBtn.setScaleY(2.0);
  FrameLayout layout = (FrameLayout)activity.findViewById(R.id.content);
  layout.addView(switchBtn);
DrawableCompat.setTintList(DrawableCompat.wrap(switchBtn.getThumbDrawable()), new ColorStateList(states, thumbColors));
DrawableCompat.setTintList(DrawableCompat.wrap(switchBtn.getTrackDrawable()), new ColorStateList(states, trackColors));
  switchBtn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
      String msg = isChecked ? "ON" : "OFF";
      switchBtn.setText(msg);
      println("switch btn = ",msg);
    }
  }
  );
}

In the main tab, there’s an import statement:

import androidx.core.graphics.drawable.DrawableCompat;

Can anyone explain what the error message means? Is there something wrong with the import statement? Or is there a problem with the two lines that reference ‘DrawableCompat’?

Thanks.

Error message is due to ‘No library found for androidx.core.graphics.drawable’
Try this code instead and delete import androidx.core.graphics.drawable.DrawableCompat;

int[][] states = new int[][]{
  new int[] {-android.R.attr.state_checked},
  new int[] {android.R.attr.state_checked},
};

int[] thumbColors = new int[] {
  Color.WHITE,
  Color.GREEN,
};

int[] trackColors = new int[] {
  Color.WHITE,
  Color.WHITE,
};

void switchBtn(int x, int y) {  
  Switch switchBtn = new Switch(activity);
  LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
  layoutParams.setMargins(x, y, 200, 190);
  switchBtn.setLayoutParams(layoutParams);
  switchBtn.setText("OFF");
  switchBtn.setScaleX(2.0);
  switchBtn.setScaleY(2.0);
  FrameLayout layout = (FrameLayout)activity.findViewById(R.id.content);
  layout.addView(switchBtn);
  switchBtn.setThumbTintList(new ColorStateList(states, thumbColors));
  switchBtn.setTrackTintList(new ColorStateList(states, trackColors));
  switchBtn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
      String msg = isChecked ? "ON" : "OFF";
      switchBtn.setText(msg);
      println("switch btn = ",msg);
    }
  }
  );
}
1 Like

Thanks, that got rid of the error messages. But why didn’t I get an error message on the import statement itself, if it couldn’t be found?

But why didn’t I get an error message on the import statement itself, if it couldn’t be found?

It was at the top of the console log on my system; you probably missed it.