In Android, you can set the background via xml and everything will be fine. In some special cases, I need to install it inside Java (i.e., modify list lines). You might think that he could just declare:
View v = findViewById(R.id.some_view); v.setBackgroundResource(R.drawable.somedrawable);
but unfortunately this view is losing its complement. I have an object method like this that works fine:
private void applyDifferentBackgroundResource(int resId, View v){ final int paddingBottom = v.getPaddingBottom(); final int paddingLeft = v.getPaddingLeft(); final int paddingRight = v.getPaddingRight(); final int paddingTop = v.getPaddingTop(); v.setBackgroundResource(resId); v.setPadding(paddingLeft, paddingTop, paddingRight, paddingBottom); }
But just out of curiosity: is there a reason why the View is losing its complement, or the setBackgroundResource Android setBackgroundResource cannot (or should not) process on its own?
source share