user2668638
user2668638

Reputation: 653

How to change background in Java?

I'm working on an app that allows changing theme. To achieve that, I need to change background in java.

I created imageView and tried to set background as imageView. I was using this code:

ImageView imgViewBackground =(ImageView) findViewById(R.id.imageViewBackground);
int ID = getResources().getIdentifier("imagebackground", "drawable",  getPackageName());
imgViewBackground.setImageResource(ID);

but the app crashes after 20-30 seconds of usage.

I also tried this, but the app crashes on startup:

RelativeLayout layout =(RelativeLayout)findViewById(R.id.imageViewBackground);
layout.setBackgroundResource(R.drawable.imagebackground);

Is there an effective way to change background directly, and not through imageView in java?

Upvotes: 0

Views: 160

Answers (1)

Rahul
Rahul

Reputation: 45090

Why not simply do this:

ImageView imgViewBackground =(ImageView) findViewById(R.id.imageViewBackground);
imgViewBackground.setImageResource(R.drawable.imagebackground);

P.S: An image by the name imagebackground must be present in the drawable folder.

Upvotes: 2

Related Questions