Handling orientation changes android

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

I have a Table containing a number of images per row. The number of images per row is decided on the fly based on the image width and screen width. When I use the phone normally, images are spaced on the phone screen. When the phone orientation changes, all the images appear on the same row. Should I explicitly handle the orientation changes for this case?

// get the number of images per column based on the screen
// resolution
Display display = getWindowManager().getDefaultDisplay();
int screenWidth = display.getWidth();

Resources res = getResources();
Drawable drawable = res.getDrawable(R.drawable.emo_im_happy);

numberOfImagesPerRow = screenWidth / (drawable.getIntrinsicWidth() + 10);

int numberOfRows = (int) Math.ceil(numberOfEmotions
                    / numberOfImagesPerRow);

Answers

It depends when that code is run, but you generally don t need to do anything special.

When Android detects an orientation change, the system will re-create your Activity and it will get a chance to go through onCreate and re-layout and re-render everything according to the new configuration.

Using android android:configChanges should be used with care and not be the first option you think of, since the system will do a better job at selecting appropriate resources for you after a configuration change (and you ll have to handle that code path for other forms of configuration change anyways).

See: http://developer.android.com/guide/topics/resources/runtime-changes.html http://developer.android.com/guide/topics/resources/runtime-changes.html

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/8253735/handling-orientation-changes-android

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils