2

I have a webView in an Activity. Inside, the webView I have a button that opens a new HTML page. When this button is pressed and the new html page opens I would like the screen orientation to change to horizontal.

var searchButton = $('<button/>',
            {
                text:'Search!',
                "class":"buttons",
                id: "searchButtonID",
                click: function(){

                var select = document.getElementById("subCategory");
                var option = select.options[select.selectedIndex];
                var subCategoryId = option.id;
                window.location.href = "deals.html?subCategoryId=" + subCategoryId; 

                Android.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);             
            }
        });

I recently learnt about WebAppInterfaces that allows Javascript to interact with Android. I tried adding the line:

Android.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

However, I get an error message saying:

 Uncaught ReferenceError: ActivityInfo is not defined

I imported import android.content.pm.ActivityInfo; in the relevant class.

Not sure if what I'm doing is even possible...

Any help would be greatly appreciated!

3 Answers3

0
 private void rotate(float degree) {
    final RotateAnimation rotateAnim = new RotateAnimation(0.0f, degree,
            RotateAnimation.RELATIVE_TO_SELF, 0.5f,
            RotateAnimation.RELATIVE_TO_SELF, 0.5f);

    rotateAnim.setDuration(0);
    rotateAnim.setFillAfter(true);
    oWebView.startAnimation(rotateAnim);
}

btn.setOnClickListener(new OnClickListener()
    {

        @Override
        public void onClick(View v)
        {


            rotate(90);
        }
    });

try this

Nithinlal
  • 4,845
  • 1
  • 29
  • 40
  • Nithinlal can you explain please how this code will work through "Javascript" code to "setOnClickListener"? Thanks. – Siddiq Abu Bakkar Nov 05 '13 at 07:32
  • Hi Nithinlal, rather than using `btn.setOnClickListener(new OnClickListener()`, should I access the 'rotate' function in `'click: function(){}'`. Secondly, how do I access my `webview` because that is in a different class to my `WebAppInterface`? –  Nov 05 '13 at 09:54
0

Do something like this

boolean rotationRequired = false;    

private void openWebviewDialog(String days)
{
dialog = new Dialog(MainActivity.GLOBAL_CONTEXT);
dialog.setContentView(R.layout.simple_webview);
WebView wb = (WebView) dialog.findViewById(R.id.simple_webview);
WebSettings webSettings = wb.getSettings(); 
wb.setWebViewClient(new MyWebViewClient());
webSettings.setJavaScriptEnabled(true);
wb.addJavascriptInterface(this, "javaScriptInterface");
String url = "http://www.yourdomain.com" // your url here
String title = "Hello";

wb.loadUrl(url);
dialog.setCancelable(true);
dialog.setTitle(title);
dialog.show();
}

class MyWebViewClient extends WebViewClient 
{
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) 
{
    if(url.contains("openExternal"))
    { 
        return super.shouldOverrideUrlLoading(view, url); // Leave webview and use browser
    } 
    else 
    {
        view.loadUrl(url); // Stay within this webview and load url
        return true;
    }
}

@Override
public void onPageFinished(WebView view, String url) 
{
    super.onPageFinished(view, url);
    if(rotationRequired)
    {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    }
}

@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) 
{
    super.onPageStarted(view, url, favicon);
}
}

@JavascriptInterface
public void passResult(String status) 
{
   rotationRequired = status.equalsIgnoreCase("true") ? true : false;
}

and don't forget to add this to your manifest:

android:configChanges = "orientation"

control the rotation as

 <p>rotate...</p><script type='text/javascript' language='javascript'>javaScriptInterface.passResult('true');</script>
Rohit5k2
  • 17,948
  • 8
  • 45
  • 57
0

Please define and import packages

    WebView myWebView;
    WebAppInterface mWebAppInterface;

add this code to onCreate() of your activity

mWebAppInterface = new WebAppInterface(this);
myWebView = (WebView) findViewById(R.id.webview);
myWebView.addJavascriptInterface(mWebAppInterface, "Android1");
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
//load your URL by myWebView.loadUrl("www.example.com");

Add this public function also

@JavascriptInterface
public void setorientation(){

     setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}

Now you can call this public function for setting orientation from javascript button click function

Android1.setorientation();

visit also http://developer.android.com/guide/webapps/webview.html using javascript in android webview

Community
  • 1
  • 1