So as the title says, my ImageButton won't update its resource even though I called setImageResource.
Here is my code:
public static void toggleqsring() {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.activity_main, null, false);
SquareButton qs_ringer = (SquareButton) v.findViewById(R.id.qs_ring);
if (mPhoneIsSilent==0) {
qs_ringer.setImageResource(R.drawable.ic_qs_ring_off);
} else if (mPhoneIsSilent==1) {
qs_ringer.setImageResource(R.drawable.ic_qs_ring_on);
} else if (mPhoneIsSilent==2) {
qs_ringer.setImageResource(R.drawable.ic_qs_vibrate_on);
}
}
SquareButton is a class that extends ImageButton, but I have made its height equal to its width.
I don't know what I have done wrong here, can anyone help?
Ok I got it working after a bit of experimenting.
Instead of passing the Context of MainActivity, I passed the Activity itself to a new variable
public static Activity mActivity;
And used this code to change the image resource. Since findViewById is a method of Activity but not Context, you can only do it with an Activity variable:
public static void toggleqsring() {
SquareButton qs_ringer = (SquareButton) mActivity.findViewById(R.id.qs_ring);
if (mPhoneIsSilent==0) {
qs_ringer.setImageResource(R.drawable.ic_qs_ring_off);
} else if (mPhoneIsSilent==1) {
qs_ringer.setImageResource(R.drawable.ic_qs_ring_on);
} else if (mPhoneIsSilent==2) {
qs_ringer.setImageResource(R.drawable.ic_qs_vibrate_on);
}
}