I have create an android application
In that application i have one use of browser
So i have create a browser in android application
But my problem is that - when i try to write any thing in google search engine it written in url of edittext control (i have load a google page as a url) and my code is below
public void onClick(View b)
{
wv=(WebView)findViewById(R.id.webView1);
WebSettings settings=wv.getSettings();
settings.setJavaScriptEnabled(true);
settings.setBuiltInZoomControls(true);
wv.setWebViewClient(new MyWebViewClient());
EditText et=(EditText)findViewById(R.id.editText1);
String url=et.getText().toString().trim();
wv.loadUrl("http://"+url);
}
private class MyWebViewClient extends WebViewClient
{
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
return false;
}
You have to put String url=et.getText().toString().trim(); in some Button click event
like below
ib_load.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String url=et.getText().toString().trim();
wv.loadUrl("http://"+url);
}
});
and also make your WebViewClient
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
and for more information go to this demo link. I hope this help you.
Related
I utilize the facebook html like for my app to open a webview like this https://developers.facebook.com/docs/plugins/like-button/
The webview shows a Like and Share button, but after I login to facebook, it doesnt return to the Like and Share button, but a blank page, the share button works fine.
So how do I return to the facebook like url after logging in?
public class LikeFacebookActivity extends BaseActivity {
private WebView webView;
private final String URL = "facebookIDhere";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.like_facebook_webview);
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setAppCacheEnabled(true);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
showLoading();
webView.setWebViewClient(new MyWebViewClient());
webView.loadUrl(URL);
ActionBar actionbar = getActionBar();
actionbar.setCustomView(R.layout.actionbar_top_like_facebook);
actionbar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
Button backButton = (Button) findViewById(R.id.buttonGeneralBack);
backButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
onBackPressed();
}
});
}
#Override
public void onBackPressed() {
finish();
overridePendingTransition(R.anim.animation_slide_from_left,
R.anim.animation_slide_to_right);
}
public class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url.contains("something")) return true;
return false;
}
public void onPageFinished(WebView view, String url) {
hideLoading();
}
}
}
I have solved this, just use system.out.println to see which page does facebook load after loggin in
public class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url.contains("something")) return true;
return false; //Default is to not override unless our condition is met.
}
public void onPageFinished(WebView view, String url) {
hideLoading();
//String webUrl = webView.getUrl();
//System.out.println(webUrl);
if(url.startsWith("https://www.facebook.com/plugins/close_popup.php#_=_")){
String redirectUrl = URL;
view.loadUrl(redirectUrl);
return;
}
super.onPageFinished(view, url);
}
}
I have a webView in Android, and I open a html webpage in it. But it's full of links and images, and when I click one of them, it loads in my webview. I want to disable this behaviour, so if I click on a link, don't load it. I've tried this solution and edited a bit for myselft, but not worked.
My webviewclient code:
private boolean loaded = false;
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(loaded == false){
view.loadUrl(url);
loaded = true;
return true;
}else{
return false;
}
}
My webview implementation and settings.
WebView wv = (WebView) findViewById(R.id.recipeWv);
ourWebViewClient webViewClient = new ourWebViewClient();
webViewClient.shouldOverrideUrlLoading(wv, URLsave);
wv.setWebViewClient(webViewClient);
wv.setFocusableInTouchMode(false);
wv.setFocusable(false);
wv.setClickable(false);
WebSettings settings = wv.getSettings();
settings.setDefaultTextEncodingName("utf-8");
settings.setLoadWithOverviewMode(true);
settings.setBuiltInZoomControls(true);
Example: If the user open in my webview the StackOverflow homepage and clicks on one of the links(like "Questions") then the webview should stay on the StackOverflow homepage.
You should save in a class variable your current url and check if it's the same with the loaded one. When the user clicks on a link the shouldOverrideUrlLoading is called and check the website.
Something like this:
private String currentUrl;
public ourWebViewClient(String currentUrl) {
this.currentUrl = currentUrl;
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.equals(currentUrl)) {
view.loadUrl(url);
}
return true;
}
Important: don't forget set the WebViewClient to your WebView.
ourWebViewClient webViewClient = new ourWebViewClient(urlToLoad);
wv.setWebViewClient(webViewClient);
Implement a WebViewClient and Just return true from this method of WebView Client
webView.setWebViewClient(new WebViewClient(){
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return true;
}
});
If you want to open inner link of a web page in different window then
Don't use
WebView webView;//Your WebView Object
webView.setWebViewClient(new HelpClient());// Comment this line
B'coz setWebViewClient() method is taking care of opening a new page in the same webview. So simple comment this line.
private class HelpClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("www.example.com")) {
// This is my web site, so do not override; let my WebView load the page
return false;
}
// Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}
Hope this will work.
Even I had been facing the same issue. I solved this issue like below
webView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
return true;
}
});
With lambda expressions
webView.setOnTouchListener((v, event) -> true);
Note that the method signature changed in API 24.
For APIs earlier than 24 the second parameter is String and that signature is now deprecated.
For APIs 24 and later, the second parameter is WebResourceRequest.
If your app supports both pre and post API 24 and you want to disable all links you can use this:
webView.setWebViewClient(new WebViewClient(){
#Override //for APIs 24 and later
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request){
return true;
}
#Override //for APIs earlier than 24
public boolean shouldOverrideUrlLoading(WebView view, String url){
return true;
}
});
You need to add an extra line of code, like so-
webview.loadUrl(url);
webview.setWebViewClient(new MyWebViewClient());
And add an additional class MyWebViewClient like this-
/* Class for webview client */
class MyWebViewClient extends WebViewClient {
// show the web page in webview but not in web browser
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
#Override
public void onLoadResource(WebView view, String url) {
super.onLoadResource(view, url);
}
}
Using inject javascript On #Override onPageFinished
view.loadUrl("javascript: (function () {document.addEventListener('click', function (e) {e.stopPropagation();}, true);})()");
Very similar to Amit Gupta's answer, but I found a shorter way to do it.
private String currentUrl;
public CustomWebViewClient(String currentUrl) {
this.currentUrl = currentUrl;
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return ! url.equals(currentUrl);
}
After this CustomWebViewClient is defined, set the client to the webview.
CustomWebViewClient webViewClient = new CustomWebViewClient(urlToLoad);
wv.setWebViewClient(webViewClient);
I am experimenting with the loopj package. I am trying to make a HTTP request to a website and display the website in the webview.
I am successfully getting a result back, however the web view does not display the page as desired, instead chrome opens up and displays the page.
Am I missing something or is there a way I can override this unwanted behaviour?
Below is my oncreate method where I am making the request:
public class MainActivity extends Activity {
Button connectBtn;
TextView status;
WebView display;
String url = "http://www.google.com";
AsyncHttpClient client;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
status = (TextView)findViewById(R.id.statusbox);
connectBtn = (Button)findViewById(R.id.connectBtn);
display = (WebView)findViewById(R.id.webView1);
connectBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
client = new AsyncHttpClient();
client.get(url, new AsyncHttpResponseHandler(){
#Override
public void onSuccess(String response) {
Toast.makeText(getApplicationContext(), "Success!", Toast.LENGTH_SHORT).show();
display.loadUrl(url);
}
});
}
});
}
setWebViewClient to your WebView and override shouldOverrideUrlLoading() now write view.loadUrl(url); in that method.
Just add this code,
display.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}});
You need to set a WebViewClient and override the shouldOverrideUrlLoading method. Something like this:
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(final WebView view, final String url) {
view.loadUrl(url);
}
});
That makes sure that clicks on links in the WebView are handled by the WebView itself.
Edit: Actually, I misread the question. You aren't dealing with a click in the WebView itself, so this isn't relevant. Sorry!
just use this code under you webviewclient
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
final EditText editText = (EditText) findViewById(R.id.urlfield);
editText.setText(url);
}
}
l am using webview in my xml, loading html file from asset directory. But clicking on links sometimes launching browser on first click and sometimes not responding even after 5 clicks.
Any help is appreciated.
Thanks
For, this you've to use WebViewClient() to your WebView
WebView web = (WebView)findViewById(R.id.webView1);
.....
..... // Your stuff
.....
web.setWebViewClient(new HelloWebViewClient());
public class HelloWebViewClient extends WebViewClient
{
public HelloWebViewClient()
{
// do nothing
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url)
{
// TODO Auto-generated method stub
super.onPageFinished(view, url);
}
}
just add these lines
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
I seen in android documentation where you use
private class HelloWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
To handle when items are clicked within a webview.
The only problem is with me, is that im setting the url in another method.
The HelloWebViewClient overrides that and doesnt use the url that the user can chose from. It just returns null..How could i over ride this method to use the url set by the user?
The URL is loaded when i use it in a regular method with the WebView browser; and then browser.loadUrl(String url)
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.shopping);
findIT = (Button)findViewById(R.id.findIT);
edittext = (EditText)findViewById(R.id.item);
type = (RadioGroup)findViewById(R.id.console);
site = (RadioGroup)findViewById(R.id.shopping_group);
findIT.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
item = edittext.getText().toString();
lookUp();
}
});
}
public void lookUp(){
browser = (WebView) findViewById(R.id.shoppingBrowser);
browser.getSettings().setJavaScriptEnabled(true);
Log.v(item, item);
getUserPreference();
browser.setWebViewClient(new HelloWebViewClient());
browser.loadUrl(url);
}
private class HelloWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String notuse) {
Log.v("shopping", url+" loaded");
return true;
}
}
public void getUserPreference(){
switch(type.getCheckedRadioButtonId()){
case R.id.item:
console = "item";
break;
case R.id.PS3:
console = "item";
break;
case R.id.item:
console = "item";
break;
}Log.v("item", console);
switch(site.getCheckedRadioButtonId()){
case R.id.store:
url = "http://www.gamestop.com/browse?nav=16k- "+ item +" " + console;
break;
case R.id.store:
url = "http://www.google.com/search?q="+item + " " + console+"&tbm=shop&hl=en&aq=0&oq=where+";
break;
case R.id.store:
url = "http://www.amazon.com/s/ref=nb_sb_noss?url=search-alias%3Dvideogames&field-keywords="+item + " "+ console+"&x=0&y=0";
Log.v("shopping", url);
}
}
}
If you see what im trying to do the user gets to select what site they want to shop from. and from there i set it to the url.
If the user is choosing the URL from the same activity you can just reference the URL from the member variable instead of the URL from the parameter:
// Member variable stored to reflect user's choice
private String mUserUrl = "http://stackoverflow.com";
private class HelloWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// This line right here is what you're missing.
// Use the url provided in the method. It will match the member URL!
view.loadUrl(url);
return true;
}
}
This tells the WebviewClient that you've overloaded the URL loading (and in fact caused it to load the URL that you wish instead of the url supplied).
Here is a complete example of something I mocked up:
public class HelloWebViewActivity extends Activity {
private WebView mWebView = null;
private EditText mInputUrl = null;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mInputUrl = (EditText)findViewById(R.id.input_url);
Button button = (Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String url = mInputUrl.getText().toString();
mWebView.loadUrl(url);
}
});
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setWebViewClient(new HelloWebViewClient());
}
private class HelloWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
}
Hope this helps. If this works for you please mark the answer as accepted.
It isn't clear from your question whether this is the case, but it is possible that you did not set the WebViewClient of your WebView to the custom subclass that you created in your code. Somewhere in your code you should have something like:
browser.setWebViewClient(new HelloWebViewClient());
If you are only doing this with this one instance of WebView and your modifications to the WebViewClient are simple, then I would suggest that a more elegant way to accomplish this would be the following:
browser.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// Insert your code here
}
});
Edit:
Is it possible that the WebViewClient is actually a red herring? It appears to me that there is a problem with your switch statements in getUserPreference(). While the first switch statement seems unnecessary, the second one only ever sets the url to gamestop because all of your cases are the same.
You will set your custom WebViewClient for your webview and load the url in the webview as you we're doing before:
mWebView.setWebViewClient(new HellowWebViewClient());
mWebView.loadUrl(yourUrl);