How to detect orientation change in layout in Android?

后端 未结 10 2338
挽巷
挽巷 2020-11-22 09:36

I just implemented a orientation change feature - e.g. when the layout changes from portrait to landscape (or vice versa). How can I detect when the orientation change event

10条回答
  •  鱼传尺愫
    2020-11-22 09:54

    use this method

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT)
        {
            Toast.makeText(getActivity(),"PORTRAIT",Toast.LENGTH_LONG).show();
           //add your code what you want to do when screen on PORTRAIT MODE
        }
        else if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE)
        {
            Toast.makeText(getActivity(),"LANDSCAPE",Toast.LENGTH_LONG).show();
            //add your code what you want to do when screen on LANDSCAPE MODE
       }
    }
    

    And Do not forget to Add this in your Androidmainfest.xml

    android:configChanges="orientation|screenSize"
    

    like this

    
    
        
    

提交回复
热议问题