Android Development Change screen orientation

Source: Internet
Author: User

In some scenarios, we need to change the screen direction of the mobile phone, here is a sample I wrote.

XML Paging File:

< Relativelayout xmlns:android="Http://schemas.android.com/apk/res/android"

xmlns:tools="Http://schemas.android.com/tools"

android:layout_width="Match_parent"

android:layout_height="Match_parent"

android:paddingbottom="@dimen/activity_vertical_margin"

android:paddingleft="@dimen/activity_horizontal_margin"

android:paddingright="@dimen/activity_horizontal_margin"

android:paddingtop="@dimen/activity_vertical_margin"

tools:context=". Mainactivity ">


<Button

android:id="@+id/btn"

android:layout_width="Wrap_content"

android:layout_height="Wrap_content"

android:layout_alignparentleft="true"

android:layout_alignparenttop="true"

Android:text="Change screen orientation"/>


</ Relativelayout>

Page Java files:

Package com.example.chcfg;


import Android.os.Bundle;

import android.app.Activity;

import Android.content.pm.ActivityInfo;

import android.content.res.Configuration;

import Android.view.View;

import Android.view.View.OnClickListener;

import Android.widget.Button;

import android.widget.Toast;


Public class Mainactivityextends Activity {


@Override

protected void onCreate (Bundle savedinstancestate) {

Super. OnCreate (savedinstancestate);

Setcontentview (r.layout. Activity_main);

Button btn = (button) Findviewbyid (r.id. BTN);

Btn.setonclicklistener (new Onclicklistener ()

{

Public void OnClick (View source) {

TODO auto-generated Method stub

Configuration config = getresources (). GetConfiguration ();

if (config. Orientation = = Configuration. Orientation_landscape)

{

Set as vertical screen

Mainactivity. this. Setrequestedorientation (activityinfo. Screen_orientation_portrait);

}

Assuming the current is a vertical screen

if (config. orientation = = Configuration. orientation_portrait)

{

Mainactivity. this. Setrequestedorientation (activityinfo. Screen_orientation_landscape);

}

}

});

}


@Override

Public void onconfigurationchanged (Configuration newconfig) {

TODO auto-generated Method stub

Super. onconfigurationchanged (Newconfig);

String screen = Newconfig. orientation = = Configuration. orientation_landscape ? "Landscape Screen" : "Portrait Screen";

Toast.maketext (This,"the system screen orientation has changed \ nthe direction is:" + screens , Toast. ) Length_long). Show ();

}

}

Mainfest XML file:

<? XML Version="1.0"encoding="utf-8"?

>

< Manifest xmlns:android="Http://schemas.android.com/apk/res/android"

Package="Com.example.chcfg"

Android:versioncode="1"

android:versionname="1.0">


<USES-SDK

android:minsdkversion="8"

android:targetsdkversion="/> "


<uses-permissionandroid:name=" Android.permission.CHANGE_CONFIGURATION "/>

<application

android:allowbackup="true"

android:icon="@drawable/ic_launcher"

android:label="@string/app_name"

android:theme="@style/apptheme">

< Activity

android:name="Com.example.chcfg.MainActivity"

android:label="@string/app_name"

android:configchanges="orientation">

<intent-filter>

<actionandroid:name="Android.intent.action.MAIN" c9>/>


<categoryandroid:name="android.intent . category. LAUNCHER "/>

</intent-filter>

</ Activity >

</application>


</manifest>

Personal hard work results, if reproduced. Please indicate the source. Thank you.

Android Development Change screen orientation

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.