How to set the search url? - android

i came across a problem with the url for google search.
Actually, I have a string and I need to use the string search in google using webview. Unfortunately, the webview shows nothing. Can anyone help?
public class viewPage extends Activity {
WebView mWebView;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView (R.layout.searchonwebview);
Intent intent = getIntent();
String result = intent.getStringExtra("result");
String url = "http://www.google.com/"+result+" pdf";
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl(url);
mWebView.setWebViewClient(new HelloWebViewClient());
}
private class HelloWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
}

Why you are setting
mWebView.setWebViewClient(new HelloWebViewClient());
After loading the url? It should be before loading url.
String url = "http://www.google.com/"+result+" pdf";
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setWebViewClient(new HelloWebViewClient());
mWebView.loadUrl(url);

Related

WebViewClient shouldOverrideUrlLoading doesn't seem to work

I'm trying to listen to url changes from a WebView client and just log the updates. I'm navigating through my webView and shouldOverrideUrlLoading() is never called. I don't know why.
Here is my code :
public class WebViewActivity extends Activity {
private WebView webView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
Intent i = getIntent();
String url= i.getStringExtra("url");
webView = (WebView) findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
String currentUrl = url;
Log.i("currentUrl",": " + currentUrl);
view.loadUrl(url);
return true;
}
});
webView.loadUrl(url);
}
}

in webview external link not work in my app

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;
}
});
}

Web View not working with some specific url

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 have a webview app and facing that when i click on Hyperlink then it open in default browse

I have a webview app and facing a problem that
when I click on Hyperlink then it opens in default browser.
But I want to open that link in same webview.
How to open it in same webview.
Here is my code.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView myWebView = (WebView) findViewById(R.id.myWebView);
myWebView.loadUrl("file:///android_asset/abc/index.html");
myWebView.setWebViewClient(new MyWebViewClient());
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
}
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("file:///android_asset/abc/index.html")) {
return false;
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}
Do it like,
myWebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
});
Return false in all cases.
ok.. you have added a WebViewClient also..
then try it in this way...
myWebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
});
this should work for you..

android WebView and web browser

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);
}
}

Categories

Resources