How to assign a drawable to an ImageView
I’m working on adding halo to cyanogenmod 11. To do this, I’m using a method to get if halo is in the active state and then assign an image to it accordingly.
First, here’s the code I’ve tried recently (I’ve tried this several times) – >
protected void updateHalo() {
mHaloActive is a boolean. mHaloButton is an ImageView
mHaloActive = Settings.System.getInt(mContext.getContentResolver(),
Settings.System.HALO_ACTIVE, 0) == 1;
int resID;
String mDrawableName;
if (mHaloActive) {
mDrawableName = "ic_notify_halo_pressed";
resID = getResources().getIdentifier(mDrawableName, "drawable", getPackageName());
mHaloButton.setImageResource(resID);
} else {
mDrawableName = "ic_notify_halo_normal";
resID = getResources().getIdentifier(mDrawableName, "drawable", getPackageName());
mHaloButton.setImageResource(resID);
}
if (mHaloActive) {
if (mHalo == null) {
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mHalo = (Halo) inflater.inflate(R.layout.halo_trigger, null);
mHalo.setLayerType(View.LAYER_TYPE_HARDWARE, null);
WindowManager.LayoutParams params = mHalo.getWMParams();
mWindowManager.addView(mHalo, params);
mHalo.setStatusBar(this);
}
} else {
if (mHalo != null) {
mHalo.cleanUp();
mWindowManager.removeView(mHalo);
mHalo = null;
}
}
}
Now here, it says cannot find symbol: method getPackageName().
I’ve tried setImageResource(R.drawable.ic_notify_halo_pressed) several times before;
But this gave me NPE.
Then I also tried using :
Resources resources = getResources(); mHaloButton.setImageDrawable(resources.getDrawable(R.drawable.ic_notify_halo_pressed));
But this gives the error No sign found: method getResources().
I’ve also tried mHaloButton.setImageDrawable(R.drawable.ic_notify_halo_pressed);
But it gives the error: setImageDrawable(android.graphics.drawable.Drawable) in android.widget.ImageView cannot applied to (int).
So I know it requires a resource ID, but how do I get it?
Please note: I’m not building an app, and this isn’t an Android Studio project. I’m trying to port halo to cyanogenmod 11.