How to assign to ImageView

I am working on adding a halo to cyanogen mode 11. And for that I am using a method that activates the halo state or not and then assigns an image to it appropriately.

First, here's my code from the last try (I've made several attempts on this task) ->

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 it says here cannot find symbol: method getPackageName()

I've done a few tries before, like using setImageResource(R.drawable.ic_notify_halo_pressed);

But this gave me NPE.

Then I also tried using:

Resources resources = getResources();        mHaloButton.setImageDrawable(resources.getDrawable(R.drawable.ic_notify_halo_pressed));

      

But it gave an error cannot find symbol: method getResources()

And I also tried mHaloButton.setImageDrawable(R.drawable.ic_notify_halo_pressed);

, but it gives an error:setImageDrawable(android.graphics.drawable.Drawable) in android.widget.ImageView cannot be applied to (int)

So, I know it needs a resource id, but how do I get that?

Please note, I am not building an app and this is not an Android studio project. I am trying to transfer a halo to cyanogen mode 11.

+3


source to share


1 answer


Replace Resources resources = getResources();

with Resources resources = mContext.getResources();

as you already have a context reference.



The same goes for getPackageName()

. Replace it with mContext.getPackageName()

.

+5


source







All Articles