2

de nuevo molestando con mis dudas, estoy usando un código que encontré en github, es de un web view, el asunto es que no logro hacer que la orientación de la pantalla se mantenga siempre en horizontal, no se si esto sea por el uso de WindowManager, bueno la idea es esa hacer que la pantalla siempre sea horizontal, aun cuando se habilite y se gire el teléfono. dejo el código a continuación

 public class MainActivity extends AppCompatActivity {


private VideoEnabledWebView webView;
private VideoEnabledWebChromeClient webChromeClient;
@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
  setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

    setContentView(R.layout.activity_main);

    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,       WindowManager.LayoutParams.FLAG_FULLSCREEN);
    // Save the web view
    webView = (VideoEnabledWebView)findViewById(R.id.webView);
    //webView.getSettings().setPluginsEnabled(true);

    // Initialize the VideoEnabledWebChromeClient and set event handlers
    View nonVideoLayout = findViewById(R.id.nonVideoLayout); // Your own view, read class comments
    ViewGroup videoLayout = (ViewGroup)findViewById(R.id.videoLayout); // Your own view, read class comments
    //noinspection all
    View loadingView = getLayoutInflater().inflate(R.layout.view_loading_video, null); // Your own view, read class comments
    webChromeClient = new VideoEnabledWebChromeClient(nonVideoLayout, videoLayout, loadingView, webView) // See all available constructors...
    {
        // Subscribe to standard events, such as onProgressChanged()...
        @Override
        public void onProgressChanged(WebView view, int progress)
        {
            // Your code...
        }
    };
    webChromeClient.setOnToggledFullscreen(new VideoEnabledWebChromeClient.ToggledFullscreenCallback()
    {
        @Override
        public void toggledFullscreen(boolean fullscreen)
        {
            // Your code to handle the full-screen change, for example showing and hiding the title bar. Example:
            if (fullscreen)
            {
                WindowManager.LayoutParams attrs = getWindow().getAttributes();
                attrs.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
                attrs.flags |= WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
                getWindow().setAttributes(attrs);
                if (android.os.Build.VERSION.SDK_INT >= 14)
                {
                    //noinspection all
                    getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE);
                }
            }
            else
            {
                WindowManager.LayoutParams attrs = getWindow().getAttributes();
                attrs.flags &= ~WindowManager.LayoutParams.FLAG_FULLSCREEN;
                attrs.flags &= ~WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
                getWindow().setAttributes(attrs);
                if (android.os.Build.VERSION.SDK_INT >= 14)
                {
                    //noinspection all
                    getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
                }
            }

        }
    });
    webView.setWebChromeClient(webChromeClient);
    // Call private class InsideWebViewClient
    webView.setWebViewClient(new InsideWebViewClient());

    // Navigate anywhere you want, but consider that this classes have only been tested on YouTube's mobile site

    webView.getSettings().setAllowFileAccess(true);
    webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);

    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    webView.loadUrl("http://url.mx/");

}

private class InsideWebViewClient extends WebViewClient {
    @Override
    // Force links to be opened inside WebView and not in Default Browser
    // Thanks http://stackoverflow.com/a/33681975/1815624
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }
}

@Override
public void onBackPressed()
{
    // Notify the VideoEnabledWebChromeClient, and handle it ourselves if it doesn't handle it
    if (!webChromeClient.onBackPressed())
    {
        if (webView.canGoBack())
        {
            webView.goBack();
        }
        else
        {
            // Standard back button implementation (for example this could close the app)
            super.onBackPressed();
        }
    }
}


  // @Override

  }

Archivo XML

<!-- View that will be hidden when video goes fullscreen -->
<RelativeLayout
    android:id="@+id/nonVideoLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <com.qmovie.qmvie.qmovie.VideoEnabledWebView
        android:id="@+id/webView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>

<!-- View where the video will be shown when video goes fullscreen -->
<RelativeLayout
    android:id="@+id/videoLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <!-- View that will be shown while the fullscreen video loads. For  example you could include
    a "Loading..." message. However, in this particular example this view is actually defined in
    view_loading_video.xml and inflated programmatically in ExampleActivity.java. The reason for
    this is to keep the main layout (this file) cleaner. -->
    <!-- <View
        android:id="@+id/videoLoading"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:visibility="invisible" /> -->

</RelativeLayout>

MANIFES.XML

    android:theme="@style/AppTheme"

    android:hardwareAccelerated="true" >
    <activity android:name=".MainActivity"
     android:screenOrientation="landscape"
        android:configChanges="keyboardHidden|screenSize|screenLayout">>

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

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

De antemano cualquier comentario o sugerencia se agradece :D

1 respuesta 1

2

Bloquear la aplicacion en orientacion Horizontal, se puede realizar configurando desde el AndroidManifest.xml en la Activity :

<activity android:name=".MainActivity"
    android:label="@string/app_name"
    android:screenOrientation="landscape"
    android:configChanges="orientation|keyboardHidden|screenSize" />

(usa screenSize no screenLayout).

o definiendo en onCreate() de tu activity:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
...
...

Puedes bloquear la Activity con cualquiera de las opciones anteriores.

Debes especificar que lo que se se esta abriendo es el WebView y posiblemente es lo que esta cambiando de orientacion (no la Activity) ya que realizas una visualizacion fullscreen.

2
  • muchas gracias @Elenasys siempre me salvas el día, funciona perfectamente Commented el 8 ago. 2016 a las 0:16
  • 1
    Un placer Jesus me alegra solucionaste tu problema.
    – Jorgesys
    Commented el 8 ago. 2016 a las 4:36

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.