I tried to create this small custom browser, but I have a small problem, when I click on a link, it opens it quietly, but first I open a blank page, as if it were a new instance of the activity. In fact when I try to go back from the open link, I go about the activity with the blank page. It does not change page, but the activity. What am I doing wrong?
ACTIVITY:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String url = "";
if (getIntent().getData() != null) {
setContentView(R.layout.activity_main);
url = getIntent().getData().toString();
WebView wv = (WebView) findViewById(R.id.webView);
WebSettings settings = wv.getSettings();
settings.setDomStorageEnabled(true);
wv.clearCache(true);
wv.clearHistory();
settings.setJavaScriptEnabled(true);
settings.setLoadsImagesAutomatically(true);
settings.setJavaScriptCanOpenWindowsAutomatically(true);
wv.setWebChromeClient(new WebChromeClient());
wv.loadUrl(url);
}
}
}
LAYOUT:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.andrea.webviewsample.MainActivity">
<WebView
android:id="#+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
MANIFEST:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.andrea.webviewsample">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<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"
android:launchMode="singleTask">
<activity
android:name=".MainActivity"
android:theme="#style/Theme.AppCompat.NoActionBar">
<intent-filter>
<data android:scheme="http" />
<data android:scheme="https" />
<data android:host="*" />
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<category android:name="android.intent.category.APP_BROWSER" />
<action android:name="android.intent.action.VIEW" />
</intent-filter>
</activity>
</application>
</manifest>
THIS IS THE RESULT:
Solution found!
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String url = "";
if (getIntent().getData() != null) {
WebView wv = new WebView(this);
url = getIntent().getData().toString();
WebSettings settings = wv.getSettings();
settings.setDomStorageEnabled(true);
wv.clearCache(true);
wv.clearHistory();
settings.setJavaScriptEnabled(true);
settings.setLoadsImagesAutomatically(true);
settings.setJavaScriptCanOpenWindowsAutomatically(true);
wv.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
});
wv.loadUrl(url);
setContentView(wv);
}
}
Related
Some how it works on older devices but i tried in 2 new devices the release app doesnt work its shows a white blank screeen after splash screen
And there is no error showing please answer me
main activity
private WebView mywebview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mywebview = (WebView) findViewById(R.id.webview);
WebSettings webSettings = mywebview.getSettings();
mywebview.setWebViewClient(new WebViewClient());
mywebview.loadUrl("https://netixshop.com/");
webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);
}
public class myWebClient extends WebViewClient {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
#Override
public void onBackPressed() {
if (mywebview.canGoBack()) {
mywebview.goBack();
}else{
super.onBackPressed();
}
}
}
here is my manifest
pleasse check this out and find why is the error caused
android manifest
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<application
android:usesCleartextTraffic="true"
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"
tools:ignore="GoogleAppIndexingWarning">
<activity android:name=".MainActivity"></activity>
<activity android:name=".splashactivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>```
Probably you didn't add the internet permission to Manifest file while it is added to debug manifest file by default. So you need to add the internet permission;
<uses-permission android:name="android.permission.INTERNET" />
and if you work with android 8 or above, probably you will need to add this line under application tag for accessing to non-https web urls;
android:usesCleartextTraffic="true"
finally it will look like this;
<?xml version="1.0" encoding="utf-8"?>
<manifest ...>
<uses-permission android:name="android.permission.INTERNET" />
<application
...
android:usesCleartextTraffic="true"
...>
...
</application>
</manifest>
Hi I've create a simple link browser, but I lost last update to open external URL.
This is the AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.browser">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:launchMode="singleInstance"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name="com.test.browser.WebViewActivity"
android:screenOrientation="portrait"
android:theme="#style/Theme.AppCompat.NoActionBar">
<intent-filter>
<data android:scheme="http" />
<data android:scheme="https" />
<action android:name="android.intent.action.PICK" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
</intent-filter>
</activity>
</application>
</manifest>
And this my simple WebViewActivity.java
public class WebViewActivity extends AppCompatActivity {
public static Activity controller;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
controller = this;
mURL = "";
if (getIntent().getData() != null) {
setWebViewAndLoadURL(mURL);
} else {
this.finishAffinity();
}
}
}
The method 'setWebViewAndLoadURL' is not important because the action.VIEW not working.
Do you have any solutions?
I suggest to use the WebView layout in your activity which is more simple.
`<?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/my_webview"
android:layout_width="match_parent"
android:layout_height="match_parent" />`
in your WebViewActivity modify the code with :
String url = "https://www.google.com";
setContentView(R.layout.your_layout);
WebView webView = (WebView) findViewById(R.id.my_webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl(url);
Tnx to all.
I found solution.
In XML I must add this to intent-filter:
<data android:scheme="http" android:host="*"/>
<data android:scheme="https" android:host="*"/>
And in Activity I must add intent URL:
mURL = "";
if (getIntent().getData() != null) {
mURL = getIntent().getData().toString();
setWebViewAndLoadURL(mURL);
}
i have a problem i am create an app web view to get and website on play store.
the problem is that it have a dropdown Menu html5+jquery+css3. when i rotate the smartphone, the web are refreshed.
this my code Android Studio 3.0:
Activity xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.icarosnet.icarosnetsa.IcarosWeb">
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/wv"
/>
</android.support.constraint.ConstraintLayout>
Main.Java
package com.icarosnet.icarosnetsa;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class IcarosWeb extends AppCompatActivity {
WebView wv ;
#Override
public void onBackPressed(){
if(wv.canGoBack()){
wv.goBack();
}else{
super.onBackPressed();
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_icaros_web);
wv= (WebView) findViewById(R.id.wv);
wv.getSettings().setJavaScriptEnabled(true);
wv.setFocusable(true);
wv.setFocusableInTouchMode(true);
wv.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
wv.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
wv.getSettings().setDomStorageEnabled(true);
wv.getSettings().setDatabaseEnabled(true);
wv.getSettings().setAppCacheEnabled(true);
wv.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
wv.loadUrl("https://icarosnet.com");
wv.setWebViewClient(new WebViewClient());
}
#Override
protected void onSaveInstanceState(Bundle outState ){
super.onSaveInstanceState(outState);
wv= (WebView) findViewById(R.id.wv);
wv.saveState(outState);
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState){
super.onRestoreInstanceState(savedInstanceState);
wv= (WebView) findViewById(R.id.wv);
wv.restoreState(savedInstanceState);
}
}
Xml Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.icarosnet.icarosnetsa">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<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=".IcarosWeb">
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
does not respect the content that was loaded by ajax.
how can i prevent refresh and resize the view; or how can i show it only portrait and block the view.
Try this:
It saves state onPause for WebView.
#Override
public void onPause() {
super.onPause();
webViewBundle = new Bundle();
wv.saveState(webViewBundle);
}
Add in OnCreate:
if (webViewBundle == null) {
wv.getSettings().setJavaScriptEnabled(true);
wv.setFocusable(true);
wv.setFocusableInTouchMode(true);
wv.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
wv.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
wv.getSettings().setDomStorageEnabled(true);
wv.getSettings().setDatabaseEnabled(true);
wv.getSettings().setAppCacheEnabled(true);
wv.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
wv.loadUrl("https://icarosnet.com");
} else {
wv.restoreState(webViewBundle);
}
*Assuming wv would be declared for the class
<activity android:name=".TestActivity"
android:screenOrientation="portrait">
write this line and show it only portrait
You can just add android:screenOrientation = "potrait" inside activity tag in manifest file to make your Activity not rotate to landscape even when phone is rotated.
<activity
android:name="YourActivity"
android:screenOrientation="portrait"
android:configChanges="orientation|keyboardHidden|screenSize"/>
#Override
protected void onSaveInstanceState(Bundle outState )
{
super.onSaveInstanceState(outState);
wv.saveState(outState);
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState)
{
super.onRestoreInstanceState(savedInstanceState);
wv.restoreState(savedInstanceState);
}
<activity
android:name=".MainActivity"
android:configChanges="orientation|keyboardHidden|screenSize"><intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
I'm trying to achieve this: MKyong - WebView.
To get a hint at what exactly, then take a look at the last picture before "Download Source Code".
My applications code is:
bookingView = (WebView) findViewById(R.id.fullscreen_content);
bookingView.getSettings().setJavaScriptEnabled(true);
bookingView.loadUrl("http://www.google.com");
But what that code does is opening the url(in this case Google) inside the default browser/internal browser in android and it's not meant to be that in the android application I am making.
Any ideas?
Add this line before the loadUrl() call
bookingView.setWebViewClient(new WebViewClient());
This is the answer:
Features
Load a URL on WebView
Open another page in the website on Webview dont on local Browser.
If you press on backbutton will go to the before page dont out of app.
MainActivity.java
public class MainActivity extends AppCompatActivity {
WebView webview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webview = (WebView)findViewById(R.id.webview);
webView();
}
//Metodo llamar el webview
private void webView(){
//Habilitar JavaScript (Videos youtube)
webview.getSettings().setJavaScriptEnabled(true);
//Handling Page Navigation
webview.setWebViewClient(new MyWebViewClient());
//Load a URL on WebView
webview.loadUrl("http://stackoverflow.com/");
}
// Metodo Navigating web page history
#Override public void onBackPressed() {
if(webview.canGoBack()) {
webview.goBack();
} else {
super.onBackPressed();
}
}
// Subclase WebViewClient() para Handling Page Navigation
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("stackoverflow.com")) { //Force to open the url in WEBVIEW
return false;
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<WebView android:id="#+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
Include this in AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET" />
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.webview" >
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
RESULT
START PAGE
ANOTHER PAGE IN THE WEBSITE ON WEBVIEW
well i have one problem with parse, but i can´t know why it crashes, i explain it.
Im working with parse, and works ok, but then with no reason, sometimes, when i turn off the mobile or go out of the wifi´s range, the app crashes. i think all its ok, cause i can recieve parse´s notifications with no problem.... well need some help here
The app its a webview
public class MainActivity extends Activity {
private WebView myWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
this.setContentView(R.layout.activity_main);
Parse.initialize(getApplicationContext(), "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
PushService.setDefaultPushCallback(this, MainActivity.class);
ParseInstallation.getCurrentInstallation().saveEventually();
this.myWebView = (WebView) this.findViewById(R.id.web);
myWebView.getSettings().setJavaScriptEnabled(true);
//iniciamos javascript
myWebView.addJavascriptInterface(new WebAppInterface(this),"Android");
//webviewclient para websettings
WebSettings websettings= myWebView.getSettings();
websettings.setJavaScriptEnabled(true);
myWebView.setWebViewClient(new MyWebViewClient());
myWebView.loadUrl("http://www.aaaaa.com/);
}
#Override
public void onBackPressed(){
if(this.myWebView.canGoBack())
this.myWebView.goBack();
else
super.onBackPressed();
}
private class MyWebViewClient extends WebViewClient{
#SuppressWarnings("unused")
private long loadTime;
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url){
if (Uri.parse(url).getHost().equals("www.aaaaa.com")) {
return false;
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
public class ParseBroadcastReceiver extends BroadcastReceiver {
private static final String TAG = "com.parse.ParseBroadcastReceiver";
#Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "received " + intent.getAction());
PushService.startServiceIfRequired(context);
}
}
}
}
and then the manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.jor.dietd"
android:versionCode="3"
android:versionName="1.2" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<permission android:name="com.jor.dietd.permission.C2D_MESSAGE"
android:protectionLevel="signature"/>
<uses-permission android:name="com.jor.xxxxxx.permission.C2D_MESSAGE" />
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="22" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.jor.xxxxx.MainActivity"
android:label="#string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name="com.parse.PushService" />
<receiver android:name="com.parse.ParseBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.USER_PRESENT" />
</intent-filter>
</receiver>
<receiver android:name="com.parse.GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.jor.xxxx" />
</intent-filter>
</receiver>
</application>
</manifest>