SoFunction
Updated on 2025-03-02

Android horizontal and vertical screen switching summary

Android phones or tablets have horizontal and vertical screen switching functions, which are usually triggered by physical gravity sensing, but sometimes it is not the case. Usually, we can turn off horizontal and vertical screen switching of the phone in the settings.

<activity
android:name=""
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
android:theme="@android:style/" />
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:andro
package=".yl7" >
<!-- Include required permissions for Google Mobile Ads to run. -->
<uses-permission android:name="" />
<uses-permission android:name=".ACCESS_NETWORK_STATE" />
<uses-permission android:name=".CHANGE_CONFIGURATION"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<!-- This meta-data tag is required to use Google Play Services. -->
<meta-data
android:name=""
android:value="@integer/google_play_services_version" />
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="" />
<category android:name="" />
</intent-filter>
</activity> <!-- Include the AdActivity configChanges and theme. -->
<activity
android:name=""
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
android:theme="@android:style/" />
</application>
</manifest>

activity_main.xml

&lt;RelativeLayout
xmlns:andro
xmlns:tools="/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
&gt;
&lt;Button
android:
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Get Configuration Information"
android:textSize="25sp"
android:layout_marginTop="80dip"
android:layout_centerHorizontal="true"
/&gt;
&lt;TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Test ConfigurationChange"
android:textSize="25sp"
android:layout_centerInParent="true"
android: /&gt;
&lt;/RelativeLayout&gt;

package .yl7;
import ;
import ;
import .;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
public class MainActivity extends AppCompatActivity {
private Button mButton;
private TextView pTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
(savedInstanceState);
setContentView(.activity_main);
pTextView=(TextView)();
("---&gt; onCreate()");
init();
}
private void init() {
mButton = (Button) findViewById();
(new ClickListenerImpl());
}
private class ClickListenerImpl implements OnClickListener {
@Override
public void onClick(View v) {
getConfigurationInfo();
}
}
private void getConfigurationInfo() {
Configuration configuration = getResources().getConfiguration();
//Get the screen directionint l = configuration.ORIENTATION_LANDSCAPE;
int p = configuration.ORIENTATION_PORTRAIT;
if ( == l) {
("Now it's horizontal screen====");
("Now it's horizontal screen");
}
if ( == p) {
("It's a vertical screen now===");
("Now it's a vertical screen");
}
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
(newConfig);
//Get the current screen status is horizontal or vertical//Configuration.ORIENTATION_PORTRAIT means vertical//Configuration.ORIENTATION_LANDSCAPE means horizontal screenif(==Configuration.ORIENTATION_PORTRAIT){
("Now it's a vertical screen");
(, "Now it's a vertical screen", Toast.LENGTH_SHORT).show();
}
if(==Configuration.ORIENTATION_LANDSCAPE){
("Now it's horizontal screen");
(, "Now it's horizontal screen", Toast.LENGTH_SHORT).show();
}
}

@Override
protected void onSaveInstanceState(Bundle outState) {
(outState);
("name", "zxx");
("id", 9527);
("---&gt; onSaveInstanceState()");
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
(savedInstanceState);
String name = ("name");
int id = ("id");
("---&gt; onRestoreInstanceState()");
("name=" + name + ",serial number=" + id);

}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in .
int id = ();
if (id == .action_settings) {
return true;
}
return (item);
}
}

The above content introduces you to the relevant knowledge of the Android horizontal and vertical screen switching summary, and I hope it will be helpful to everyone!