It seems to be impossible. The resource is allowed for Drawable, and thatβs all you can return to standard functionality. There may be a way to allow reverse return to id differently, but this functionality is not implemented in the button class.
If you need easy access to this resource and you install the source code from the code, you can write ButtonClass implementing the Android button and reload / create setBackgroundResource to save the identifier in an additional field that you can access. This, of course, does not work if the button receives its BackgroundRessource not because of a function call on your part.
Here is the code.
import android.content.Context; import android.util.AttributeSet; import android.widget.Button; public class MyButton extends Button { private int bgId; public MyButton(Context context) { super(context); } public MyButton(Context context, AttributeSet attrs) { super(context, attrs); } public MyButton(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override public void setBackgroundResource(int resId) { super.setBackgroundResource(resId); bgId = resId; } public int getBackgroundId() { return bgId; } }
source share