I have a problem with webview. In my app when i click a link it open with the default browser. But I want the link open with my own app. Please help how I do this
public class MainActivity extends AppCompatActivity {
private WebView webView ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView =findViewById(R.id.webView);
WebSettings webSettings=webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("http://www.somoynews.tv/");
}
}
Try this :
webView = (WebView) findViewById(R.id.webView);
WebSettings webSettings=webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.loadUrl("http://www.somoynews.tv/");
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
view.loadUrl(request.getUrl().toString());
return false;
}
});
}
public class MainActivity extends AppCompatActivity {
String url = "https://www.pinterest.com";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView webView = findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, final String url) {
}
});
webView.loadUrl(url);
}
}
Web View is not loading "https://www.pinterest.com" within the application but it was working with chrome browser using intent. I tried of other urls it's working. While loading "https://www.pinterest.com" using webview, getting the output screen like below
You try this:
WebSettings settings = webView.getSettings();
settings.setJavaScriptEnabled(true);
settings.setAllowContentAccess(true);
settings.setDomStorageEnabled(true);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("https://www.pinterest.com/");
In manifest:
android:hardwareAccelerated="true"
set the JavascriptEnabled and AllowContentAccess and setDomStorageEnabled to true in settings
I am working on a android project. In that project i have to load a URL in WebView.
But I am unable to Load the particular URL (Any other URL is loading perfectly). I have also added INTERNET permission. Though the URL open from Android browser .
public class MainActivity extends Activity {
private WebView webView;
private ProgressDialog progDailog;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = (WebView)findViewById(R.id.webview1);
progDailog = ProgressDialog.show(this, "Loading","Please wait...", true);
progDailog.setCancelable(false);
webView = (WebView) findViewById(R.id.webview1);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
// webView.setWebChromeClient(new WebChromeClient());
webView.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
progDailog.show();
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, final String url) {
progDailog.dismiss();
}
});
webView.loadUrl("https://XXXXX.com/");
}
}
This site might contain insecure content.Check your proxy settings for the WIFI network connection and make sure they are correct.
I am trying to access the url using the shouldOverrideUrlLoading method. For some reason when I try to access the url that are present in the html files, I get Web page not available error.Previously I could access the url because I stored my html file in the raw folder but I needed to move it to assets folder as the code looks cleaner this way. This is my code, could anyone let me know how I can resolve this.
webview.setWebChromeClient(new WebChromeClient());
webview.setWebViewClient(new WebViewClient());
webview.loadUrl("file:///android_asset/myfile/file.html");
webview.setVerticalScrollBarEnabled(false);
webview.setWebViewClient(new WebViewClient()
{
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.equalsIgnoreCase("some text")){
setDialog("some fancy text");
}
Try to get the Real Path of the file first and then try as your way like this:
String YourURI="/android_asset/myfile/file.html";
YourURI = new File(getRealPathFromURI(YourURI));
//Got the Real Path Of the File
private String getRealPathFromURI(Uri contentURI) {
Cursor cursor = getContentResolver().query(contentURI, null, null, null, null);
cursor.moveToFirst();
int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
return cursor.getString(idx);
}
webview.setWebChromeClient(new WebChromeClient());
webview.setWebViewClient(new WebViewClient());
webview.loadUrl(YourURI); //**Used the Real Path**
webview.setVerticalScrollBarEnabled(false);
webview.setWebViewClient(new WebViewClient()
{
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.equalsIgnoreCase("some text")){
setDialog("some fancy text");
}
}
If the File is in the SDCARD :-
webview.setWebChromeClient(new WebChromeClient());
webview.setWebViewClient(new WebViewClient());
if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
{
Log.d(TAG, "No SDCARD");
}
else
{
webview.loadUrl("file://"+Environment.getExternalStorageDirectory()
+"/android_asset/myfile/file.html");
}
webview.setVerticalScrollBarEnabled(false);
webview.setWebViewClient(new WebViewClient()
{
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.equalsIgnoreCase("some text")){
setDialog("some fancy text");
}
}
Final Edit:- This Code Works Perfectly for Asset Folder
public class MyActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
WebView webview;
webview = (WebView) findViewById(R.id.webView1);
webview.loadUrl("file:///android_asset/myfile/file.html");
}
}
There is another way that would also work for you---
webView.loadData(customHtml, "text/html", "UTF-8");
where custromHtml is the HTML line that is used to be displayed.
on the place of "custromHtml" you just pass "file://android_asset/myfile/file.html".
I think this should work.
You may try for this too -
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String myURL = "file:///android_asset/index.html";
myBrowser=(WebView)findViewById(R.id.mybrowser);
/*By default Javascript is turned off,
* it can be enabled by this line.
*/
myBrowser.getSettings().setJavaScriptEnabled(true);
myBrowser.setWebViewClient(new WebViewClient());
myBrowser.loadUrl(myURL);
}
and also check you have placed the file in correct folder of main project. If all this is ok, make a clean project and fix project. Then try to run it.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String myURL = "file:///android_asset/index.html";
myBrowser=(WebView)findViewById(R.id.mybrowser);
myBrowser.getSettings().setJavaScriptEnabled(true);
myBrowser.setWebViewClient(new WebViewClient());
myBrowser.loadUrl(myURL);
}
This would work well try it-----
"android.resource://[package]/[res type]/[res name]"
Uri path = Uri.parse("android.resource://com.androidbook.samplevideo/raw/myvideo");
This link is also useful -
http://developer.android.com/reference/android/webkit/WebView.html
Loading an Android Resource into a WebView
shouldOverrideUrlLoading does not work on ICS for file://, but works for fine for external urls. loadURL works fine loading from the assets folder.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
WebView web=(WebView)findViewById(R.id.webView1);
web.setWebViewClient(new MyWebClient());
//web.loadUrl("file:///android_asset/index.html");
web.loadUrl("http://google.com");
.....
class MyWebClient extends WebViewClient
{
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.d("MyWebClient", "url is: " + url);
if (url.equalsIgnoreCase("google.com")){
Log.d("MyWebClient","url is: " + url);
}
return false;
}
}
I found this forum solution which solved the problem for me: http://www.cynosurex.com/Forums/DisplayComments.php?file=Java/Android/Android_4_has_a_.file....android_asset.webkit.._bug
I think you should try this ....
webview.setWebChromeClient(new WebChromeClient());
webview.setWebViewClient(new WebViewClient());
webview.loadUrl("file://android_asset/myfile/file.html");
webview.setVerticalScrollBarEnabled(false);
webview.setWebViewClient(new WebViewClient()
{
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.equalsIgnoreCase("some text")){
setDialog("some fancy text");
}
I am opening a URL in web view. I want to prevent it from opeing in web browser. User may click anything it should be with in that web view. Can set that ? How ?
public class AcDetails extends Activity {
private String url;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.acdetails);
url = getIntent().getExtras().getString("AC");
getWindow().setLayout(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
WebView wv = (WebView) findViewById(R.id.webac);
wv.getSettings().setJavaScriptEnabled(true);
wv.loadUrl(url);
}
}
No need to write a new class, just do it in-line:
WebView wv = (WebView) findViewById(R.id.webac);
wv.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
});
wv.getSettings().setJavaScriptEnabled(true);
wv.loadUrl(url);
http://developer.android.com/resources/tutorials/views/hello-webview.html
You have to write a new class, which should extend the Webviewclient class:
private class HelloWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
And change your code to:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.acdetails);
url = getIntent().getExtras().getString("AC");
getWindow().setLayout(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
WebView wv = (WebView) findViewById(R.id.webac);
wv.getSettings().setJavaScriptEnabled(true);
wv.setWebViewClient(new HelloWebViewClient());
wv.loadUrl(url);
}
}