Android :: How To Detect Orientation Change In Home Screen Widget?

Mar 15, 2010

I am writing a home screen widget and want to update (modify) the home screen widget when the device orientation changes from portrait to landscape or the other way. How can I make it? Currently, I tried to register to CONFIGURATION_CHANGED action like the code below ...

Is there a way to make the home screen change orientation with the accelerometer? On my phone, it will only change when I pull out the physical keyboard, which is not really what I'm looking to do most of the time.It changes just fine in all other applications, but the home screen doesn't. Is this a flaw with my phone or a design decision?

I got a question about screen orientation in Android Home screen. The scenario is as following. A G-sensor detects the movement on the device. Then it rotates the screen. If the device is portrait mode, the system sets to landscape mode. With G-sensor, theres is a SensorService to detect that and do some things to reach the function. I want to know how to reach that? Is there a API or a event to do that? I means that hall system, not a activity!

I'm making App Widget program on Android 1.5 I wonder if I can change my app widget size on home screen . I can set my app widget's size in XML file. But, how can I change it's size after my app widget launched from home screen ? I want to add some button to my app widget that can change it's own app widget size. Is this possible?

I have a widget which has say, a dimension of 294*72(portrait). But when the orientation is changed, I need to change this dimension of the widget to another dimension which would look good on the landscape orientation.

Maybe I'm missing something obvious, but how do you handle orientation changes for a Cupcake desktop widget?When it is initially created, the onUpdate method of my AppWidgetProvider calls views.setOnClickPendingIntent(buttonID, pendingIntent) to register for my button events, but when the orientation changes, new buttons are created and I don't have an opportunity to re-register the "onClick" listeners. What is the proper way to do this?

I have two buttons on a widget that change some items in a widget, if an orientation is changed on a phone, buttons do nothing. I read http://developer.android.com/guide/topics/resources/runtime-changes.html but this is all about activity not widget.

I'm writing a customized view similar to Grid which will be used to hold a large amount of images. I'm using ImageButton to display the images, and I only want to load images onto the buttons once they are on the screen (and just display a static background image when they are not). The reason for it is that I'm fetching images from some back-end service and I only want to fetch them when the buttons are on the screen.To do that, I'd like to know if the widget is on the screen. Is it possible to detect if a widget like button is on/off screen? If not, any suggestions on how to achieve what I'm trying to do?

I snap a photo, and it gets placed onto an ImageView. When the user sees the page where the Image View is, and I turn the phone sideways where the orientation is horizontal, the app immediately returns to the app's main screen.

I'm the proud owner of the HTC Magic on Vodafone UK. I'm loving the Android.I have to say that after 5 days of owning this device I'm only left scratching my head on one point the auto-orientation of the home screen.I've seen videos of rooted G1's running the hacked 1.5 firmware and the recently posted video of the Samsung i7500 with the 1.5 firmware all CLEARLY show that when the device is on home screen, the desktop (read home screen) auto-rotates depending on the orientation of the device. The Samsung video shows it working in ALL FOUR orientations!This doesn't happen on my Magic . Flip the orientation in an application? Works fine. Flip it back in an application? Works fine. Do it on the home screen?Am I missing something? Is this a feature that is disabled in the offical 1.5 Cupcake release? If that's the case - fine with me. I'll stop scratching my head and move on.

From reading Romain Guy's blog post, I understand keep static reference to a Drawable can create huge memory leaks. So what is the recommended solution if I don't want to reload my big Drawable image every time screen orientation changes and effect on performance? Currently my app takes >3 seconds to complete each orientation change.

I want to change the screen orientation from portrait to Landscape and vice-versa when the user shakes his/her android mobile phone, can any one help how to achieve this, because I do not know what event is fired, at the time his/her shaking thier android mobile phone. For Instance When the users touch the button in UI, touch event is fired, similarly when the user shakes his android mobile phone, what is fired.

I searched for this but couldn't find a solution. Is there anyway to get the home screen and subsequent side screens to go into landscape mode? I've seen some other Android Phones/Tablets doing this and would like to be able to do so. Or does anyone know if this will be an option in the 2.1 update? I heard that the Droid used to do this, and then some update messed it up, but then saw it going into landscape mode on the video they posted today of the Droid running 2.1.

I have a 3 nested ViewFlippers which seem to be working ok except that when I change the screen orientation the view flip back to the first one of my views, giving the user the impression it went back a few steps.I am a bit stumped as to what to do and was hoping someone had some experience with this.

I'm trying to find a way to properly handle setting up an activity where its orientation is determined from data in the intent that launched it. This is for a game where the user can choose levels, some of which are int portrait orientation and some are landscape orientation. The problem I'm facing is that setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) doesn't take effect until the activity is fully loaded. This is a problem for me because I do some loading and image processing during startup, which I'd like to only have to do once.

Currently, if the user chose a landscape level:

the activity starts onCreate(), defaulting to portrait discovers from analysing its launching Intent that it should be in landscape orientation continues regardless all the way to onResume(), loading information and performing other setup tasks at this point setRequestedOrientation kicks in so the application runs through onPause() to onDestroy() it then again starts up from onCreate() and runs to onResume() repeating the setup from earlier

Is there a way to avoid that and have it not perform the loading twice? For example, ideally, the activity would know before even onCreate was called whether it should be landscape or portrait depending on some property of the launching intent, but unless I've missed something that isn't possible. I've managed to hack together a way to avoid repeating the loading by checking a boolean before the time-consuming loading steps, but that doesn't seem like the right way of doing it. I imagine I could override onSaveInstanceState, but that would require a lot of additional coding. Is there a simple way to do this?

Solution:

As per Daniel's answer, this was actually quite easy to fix. I just needed to make a few small changes. In my 'menu' Activity, where the player would choose which level to play, I just had to add an if/else check to choose which class would be started by my Intent. This was done with a simple int representing portrait or landscape, determined when the player selected a level. I then created a second class extending my 'GameLogic' class; this is the class which contained most of the code for the game itself, rather than the menus, instructions, etc.

public class GameLandscape extends GameLogic{}

Literally that simple and completely empty. That way it inherited all the code from my previous activity where I had already coded it to handle things differently depending on the orientation. Lastly I just had to add a line to the manifest stating that GameLandscape would always run in landscape, and GameLogic would always run in portrait.

I am relatively new to the Android world and am having some difficultly understanding how the whole screen orientation cycle works. I understand that when the orientation changes from portrait to landscape or vice versa the activity is destroyed and then re-created. Thus all the code in the onCreate function will run again. So here's my situation: I have an app that I am working on where it logs into a website, retrieves data, and displays it to the user. While this is all done in background threads, the code that starts these threads is in the onCreate function. Now, the problem lies in that whenever the user changes the screen orientation, the app will log in, retrieve the data, and display it to the user again. What I would like to do is set a boolean that tells the app if it is logged in or not so it knows whether or not it must log in when the onCreate function is called. So long as the app is in memory the HttpClient will exist and contain the cookies from logging the user in but when the app is killed by the system those will go away. So I would assume that I need to do something like setting the logged in boolean to false when the app is killed but since onDestroy is called when the screen is rotated how is this possible? I also looked into the finalize function and isFinishing() but those seem to not be working.

Shorter version: How can I distinguish between when an app is being killed from memory from when an activity is being rotated and different code for each event?

I am designing a home screen widget. I ran this widget on a HTC Hero device, which has a screen of 320 pixels * 480 pixels with mdpi. It ran perfect on HTC Hero. The widget takes 3 cells * 2 cells space, i.e. 240 pixels * 200 pixels.Then I ran this widget on a Nexus One device, which has a screen of 480 pixels * 800 pixels, mdpi. Since Nexus One also is mdpi, so I though 240dip is equivalent to 240 pixels on Nexus One and 200dip is equivalent to 200 pixels on Nexus One, so the widget will not take 3 cells * 2 cells space on Nexus One device. To my surprise, when running on Nexus One device, the widget take exact 3 cells * 2 cells, about 360 pixels * 300 pixels, on Nexus One device.I am confused. The layout xml above specifies 240dip in width and 200dip in height for the widget, but why did it take 360 pixels * 300 pixels on Nexus One Device? What am I missing?

It seems when screen orientation changes, the activity's onCreate() method is called. In my onCreate() method, I have an AlertDialog which pops up when the activity is called. The problem is when I switch the screen sideways, the popup is displayed again. How can I avoid this?

This is more of an annoyance, but whenever I reply to a message off the home screen widget, it automatically defaults to the home number for that contact, instead of replying to the same (cell) number that it was sent from. If I go into the messaging app, it works as it should (replies to the same number).