I have an App Widget which, when it updates, fetches an image having dimensions to match the widget, and places that image into an ImageView
(via RemoteViews
). It works just fine.
But for devices that support rotation of the home screen (and I'm not talking about rotation of e.g. an Activity
based on device orientation, but about rotation of the home screen itself) the dimensions and aspect ratio of the widget changes a bit when going from landscape to portrait and vice versa... i.e. a fixed size is not maintained.
So, my widget needs to be able to detect when the home screen rotates, and fetch a new image (or at least load a different pre-fetched image).
I can't for the life of me work out whether and how this is possible. Any clues?
int orientation = display. getOrientation(); Check orientation as your way and use this to change orientation: setRequestedOrientation (ActivityInfo.
If the screen rotation is already on try turning it off and then on again. To check this setting, you can swipe down from the top of the display. If it's not there, try going to Settings > Display > Screen rotation.
Short guide: Tap the Settings icon to open the Settings app. Scroll down and tap Accessibility. Scroll down to Interaction controls and tap Auto-rotate screen to turn it off.
Use below code to detect orientation:-
View view = getWindow().getDecorView(); int orientation = getResources().getConfiguration().orientation; if (Configuration.ORIENTATION_LANDSCAPE == orientation) { relativeLayout.setBackgroundDrawable(getResources().getDrawable(R.drawable.log_landscape)); imageview_Logo.setImageResource(R.drawable.log_landscape_2); Log.d("Landscape", String.valueOf(orientation)); //Do SomeThing; // Landscape } else { relativeLayout.setBackgroundDrawable( getResources().getDrawable(R.drawable.login_bg) ); imageview_Logo.setImageResource(R.drawable.logo_login); //Do SomeThing; // Portrait Log.d("Portrait", String.valueOf(orientation)); }
You can listen for broadcast ACTION_CONFIGURATION_CHANGED which is sent by android system when the current device Configuration (orientation, locale, etc) has changed. As per documentation :
ACTION_CONFIGURATION_CHANGED :
Broadcast Action: The current device Configuration (orientation, locale, etc) has changed. When such a change happens, the UIs (view hierarchy) will need to be rebuilt based on this new information; for the most part, applications don't need to worry about this, because the system will take care of stopping and restarting the application to make sure it sees the new changes. Some system code that can not be restarted will need to watch for this action and handle it appropriately.
You cannot receive this through components declared in manifests, only by explicitly registering for it with Context.registerReceiver().
This is a protected intent that can only be sent by the system.
public class YourWidgetProvider extends AppWidgetProvider { @Override public void onEnabled(Context context) { super.onEnabled(context); context.registerReceiver(mOrientationChangeReceiver,new IntentFilter(Intent.ACTION_CONFIGURATION_CHANGED)); } @Override public void onDisabled(Context context) { context.unregisterReceiver(mOrientationChangeReceiver); super.onDisabled(context); } public BroadcastReceiver mOrientationChangeReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent myIntent) { if ( myIntent.getAction().equals(Intent.ACTION_CONFIGURATION_CHANGED)) { if(context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE){ // landscape orientation //write logic for building remote view here // buildRemoteViews(); } else { //portrait orientation //write logic for building remote view here // buildRemoteViews(); } } } }; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With