tags:

views:

279

answers:

2

Hi all Is there a way to change ImageView drawable with its corresponding drawable for landscape mode without destroying the whole activity. I tried to set the drawable again in onConfigurationChanged callback but it doesn't work. Something like this

@Override
public void onConfigurationChanged(Configuration conf) {
    super.onConfigurationChanged(conf);
    mImageView.setImageDrawable(getResources().getDrawable(R.drawable.iv));
}

When i star the activity in landscape mode to correct drawable is displayed so resources are located correctly.

A: 

Is there a way to change ImageView drawable with its corresponding drawable for landscape mode without destroying the whole activity.

When you change from portrait to landscape or vice versa the Activity is recreated. Check that the onCreate() method is called.

If you want to avoid this kind of behaviour use:

android:configChanges="orientation|keyboardHidden"

for your Activity in the AndroidManifest.

--

In relation to your question: I first would check what it's called first after an orientation change: onConfigurationChanged or onCreate.

If onConfigurationChanged is called first, then your setContentView() in your onCreate() method is replacing your ImageView drawable.

Macarse
As i posted i don't want to recreate the activity after configuration changed. Yes i used android:configChanges="orientation|keyboardHidden" in manifest file so my activity is created only once and then with every configuration change onCongigurationChanged is called without recreating the activity and my goal is to change some ImageView underlying drawable with the drawable placed in res/drawable-land.
Mojo Risin
The OP wants to handle the orientation change by his code(to avoid app recreate), and he needs access to the resources for landscape orientation.
Pentium10
@Mojo Risin: oh, ok. So `onConfigurationChanged` is called although you have setted that in your Manifest?
Macarse
If you don't set it in manifest android won't call it. According to documentation onConfigurationChanged is called only for configurations that are set in android:configChanges clause for the rest the activity is just recreated.
Mojo Risin
+1  A: 

In your sample project just replace

iv.setBackgroundResource(R.drawable.android);

with

iv.setImageDrawable(getResources().getDrawable(R.drawable.android));

I guess setBackgroundResource does nothing because the same resource is already assigned to ImageView.

Take a look at this
http://open-pim.com/tmp/ImageTest.zip
I tried your approach and it works just great for me.

Fedor
Mojo Risin
android:configChanges is there, onConfigurationChanged is called. Please try my sample to ensure. I updated the link, it was incorrect.
Fedor
setBackgroundResource is another possible issue
Fedor
Thanks it works now :)
Mojo Risin