Skip to content Skip to sidebar Skip to footer

Setting Background Image In Java

So basically, I have an xml, which i want to reuse. The content is somewhat the same, only the background is different and a few adjustment too.. How can I go about to do this.. I

Solution 1:

You can try to assign an id for your layout on your xml so that on your java code, you can set a different layout for it.. Here is an example:

//assuming your Layout is named linearlayout1:LinearLayoutll= (LinearLayout) findViewById(R.id.linearlayout1);
 ll.setBackgroundResource(R.drawable.sample);

You can also create if statements before setting your background like:

LinearLayoutll= (LinearLayout) findViewById(R.id.linearlayout1);
if( yourifstatement) {
ll.setBackgroundResource(R.drawable.sample);
}

If it is a RelativeLayout, then the same code applies, just change LinearLayout to RelativeLayout.

If this is not the problem, please post your LogCat.

Post a Comment for "Setting Background Image In Java"