0

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?

user2668638
  • 653
  • 1
  • 7
  • 9
  • post your Log Cat, here everything seems fine. – hemantsb Oct 05 '13 at 08:13
  • I forgot to remove android:background="@drawable/imagebackground" from xml file. For now app is not crashing, but my question still remains: Is there a way to change background directly, and not through imageView, because on some screens I can't manage to put imageView behind the buttons? – user2668638 Oct 05 '13 at 08:30

1 Answers1

2

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.

Rahul
  • 44,383
  • 11
  • 84
  • 103
  • Is there a way to change background directly, and not through imageView, because on some screens I can't manage to put imageView behind the buttons? EDIT: I forgot to remove android:background="@drawable/logo" from xml file. – user2668638 Oct 05 '13 at 08:20
  • Don't you think its(1920x1080 resolution) a bit toooo big for an android phone(whose resolution must be definitely quite less than the image)? That is most likely why the app is crashing. Why not resize it to something smaller and see. – Rahul Oct 05 '13 at 08:24
  • I wanted for background to look on all android phones, and some of them have 1920x1080 resolution, like Galaxy S4, HTC One... But maybe I could resize the image and make it fitXY. – user2668638 Oct 05 '13 at 08:28
  • 1
    there are different drawable folders for different resolution of images. Use them – Rahul Oct 05 '13 at 08:30
  • Thanks for the tip. Do you know which maximum resolution should I use for each drawable folder? – user2668638 Oct 05 '13 at 08:34
  • Check these out. [1](http://stackoverflow.com/questions/16079588/setting-drawable-folder-to-use-for-different-resolutions), [2](http://stackoverflow.com/questions/17994018/drawable-folder-different-screen-resolution-issues), [3](http://developer.android.com/guide/topics/resources/providing-resources.html). – Rahul Oct 05 '13 at 08:52