Detect rotation of Android home screen Detect rotation of Android home screen android android

Detect rotation of Android home screen


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();                    }                }            }        };    }


Check this code it works:

 myButton.setOnClickListener(new OnClickListener() {   public void onClick(View v) {    int rotation = getWindowManager().getDefaultDisplay()      .getRotation();    // DisplayMetrics dm = new DisplayMetrics();    // getWindowManager().getDefaultDisplay().getMetrics(dm);    int orientation;    CharSequence text;    switch (rotation) {    case Surface.ROTATION_0:     text = "SCREEN_ORIENTATION_PORTRAIT";     break;    case Surface.ROTATION_90:     text = "SCREEN_ORIENTATION_LANDSCAPE";     break;    case Surface.ROTATION_180:     text = "SCREEN_ORIENTATION_REVERSE_PORTRAIT";     break;    case Surface.ROTATION_270:     text = "SCREEN_ORIENTATION_REVERSE_LANDSCAPE";     break;    default:     text = "SCREEN_ORIENTATION_PORTRAIT";     break;    }    // CharSequence text = String.valueOf(orientation);    Toast toast = Toast.makeText(getApplicationContext(), text,      Toast.LENGTH_SHORT);    toast.setGravity(Gravity.CENTER | Gravity.CENTER, 10, 0);    toast.show();   }  });