-2

I want to load one url into my webview, Its a Casino game. But when i open the app it show "Please Rotate Your Device" in a black screen.

How i can check that message programmatically and set the screen orientation landscape instead of user rotating their device ?

My MainActivity Code :

package com.mob.wunderin.oapp;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebSettings;
import android.content.pm.ActivityInfo;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_USER);


        WebView webView = findViewById(R.id.myWebview);
        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);

        webView.loadUrl("http://deuceswildcards.site/wildcards.php?app=fun&Gtype=poker#/main");

        getSupportActionBar().setDisplayShowTitleEnabled(false);

    }
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mob.wunderin.oapp">
    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

The Output :

Click here for Output Image

Please help me to check the "Rotate your device" message and set the screen orientation to landscape programatically ?

Santosh Kumar
  • 26,475
  • 20
  • 67
  • 118
user3026614
  • 11
  • 1
  • 1
  • 4
  • 3
    Possible duplicate of [Change Screen Orientation programmatically using a Button](https://stackoverflow.com/questions/18268218/change-screen-orientation-programmatically-using-a-button) – M3-n50 Nov 07 '18 at 13:14
  • 1
    Why do you even bother making an app?, this url works good in a mobile browser – finki Nov 07 '18 at 13:21

2 Answers2

0

Just add

android:screenOrientation="sensorLandscape"

to your Activity tag. It will force the device into landscape while that Activity is displayed, while allowing either landscape orientation (based on the device's sensor readings).

    <activity android:name=".MainActivity"
        android:screenOrientation="sensorLandscape">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

EDIT

Remove this line from your Activity as well:

this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_USER);
TheWanderer
  • 16,775
  • 6
  • 49
  • 63
0

There is a way to do this programmatically in Java. In your onCreate method, try this:

@Override
public void onCreate(Bundle savedInstanceState) {
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}

Or, you can do it through your Manifest file like this:

<activity android:name=".MainActivity"
android:label="@string/app_name"
android:screenOrientation="landscape">

To check the current screen orientation: do the following:

int orientation = MainActivity.getResources().getConfiguration().orientation; 
if (orientation == Configuration.ORIENTATION_PORTRAIT) {      
    //The orientation is in portrait so it will set it to landscape
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 
}else {
     //The orientation is now landscape which is what you want.
}
Ishaan Javali
  • 1,711
  • 3
  • 13
  • 23
  • I don't want to set landscape permanently. When the device show "Rotate your device" message then only i want to make it landscape mode. – user3026614 Nov 07 '18 at 13:15
  • I made changes to my answer. I think this is what you want. It checks the current orientation and then if it is portrait, it sets it to landscape. Hope this helps! – Ishaan Javali Nov 07 '18 at 13:17
  • you might need to set the following within your Activity on your manifest as well, if you're handling all rotation changes: android:configChanges="orientation|screenSize" – Nikos Hidalgo Nov 07 '18 at 14:11