Trying to create an Android app for my site which can simply display the webpage in the Android app.
I have the following code in MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView mywebview = (WebView) findViewById(R.id.webView);
/*WebSettings webSettings = mywebview.getSettings();
webSettings.setJavaScriptEnabled(true); Works with and without this commented part*/
mywebview.loadUrl("https://www.javatpoint.com/");
}
}
The above is working fine and displays the 'https://www.javatpoint.com/' webpage correctly in the Android app.
However, the moment I change the "mywebview.loadUrl("https://www.javatpoint.com/");
" to my own test website "mywebview.loadUrl("https://pricepurple.com/j4f/xaph1.html");
" , it displays a blank screen on the emulator as well as the connected mobile app.
I have granted all the necessary permissions in the manifest.xml file, like,
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
And since it is working for 'https://www.javatpoint.com/' but not for my site 'https://pricepurple.com/j4f/xaph1.html', I assume there is some configuration issue??
Any help will be appreciated.
PS: Mentioned a test page on my test site, so that problem can be analyzed properly. Once the root cause and fix is known, I'' edit the q to update it to example.com with specific details
CodePudding user response:
Make this link from https://pricepurple.com/j4f/xaph1.html
to https://www.pricepurple.com/j4f/xaph1.html
. You may be missing a www
.
CodePudding user response:
You need to use setWebChromeClient to enable javascript in your WebView. But don't worry, you can use both setWebChromeClient and setWebViewClient in the same time. Just like in official docs:
// Let's display the progress in the activity title bar, like the
// browser app does.
getWindow().requestFeature(Window.FEATURE_PROGRESS);
webview.getSettings().setJavaScriptEnabled(true);
final Activity activity = this;
webview.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
// Activities and WebViews measure progress with different scales.
// The progress meter will automatically disappear when we reach 100%
activity.setProgress(progress * 1000);
}
});
webview.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(activity, "Oh no! " description, Toast.LENGTH_SHORT).show();
}
});
webview.loadUrl("https://pricepurple.com/j4f/xaph1.html");
https://developer.android.com/reference/android/webkit/WebView.html
CodePudding user response:
Try this.
uncomment those websetting lines and
after this line setJavaScriptEnabled(true)
write setDomStorageEnabled(true)
like this
webSettings.setDomStorageEnabled(true)