How to set webview client? - android

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

Related

When i try write into google search engine it written in url?

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.

facebook like on webview android shows blank screen after login

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

Trying to display url in Web View

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

Webview shouldoverrideurlloading doesn't work

I have this code in my app:
public class Home extends Activity{
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
final ProgressDialog progressBar;
if(isOnline()){
WebView webView = (WebView) findViewById(R.id.home_web);
webView.setBackgroundColor(Color.parseColor(getString(R.color.colore_bg)));
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setPluginsEnabled(true);
webView.setWebViewClient(new MyWebViewClient());
progressBar = ProgressDialog.show(this,getString(R.string.caricamento),getString(R.string.attendere));
webView.setWebViewClient(new WebViewClient(){
public void onPageFinished(WebView view, String url) {
if (progressBar.isShowing()) {
progressBar.dismiss();
}
}
});
webView.loadUrl("http://www.mysite.com/android.php");
}else{
Toast.makeText(this,getString(R.string.no_connessione),Toast.LENGTH_LONG).show();
}
}
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
System.out.println("here");
if (Uri.parse(url).getHost().equals("mysite.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;
}
}
public boolean isOnline(){
ConnectivityManager cm=(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ni = cm.getActiveNetworkInfo();
if(ni==null){
return false;
}
return ni.isConnected();
}
}
The shouldOverrideUrlLoading doesn't work, neither print the system.out, it seems to be never called. How can I repair this? I need to open all the link (except the main page www.mysite.com/iphone.php) in the default browser
You've set the WebViewClient twice, thus replacing the first one (shouldOverrideUrlLoading) with the second one (onPageFinished). Combine the two for it to work:
public class Home extends Activity{
private ProgressDialog progressBar;
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
if(isOnline()){
progressBar = ProgressDialog.show(this,getString(R.string.caricamento),getString(R.string.attendere));
WebView webView = (WebView) findViewById(R.id.home_web);
webView.setBackgroundColor(Color.parseColor(getString(R.color.colore_bg)));
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setPluginsEnabled(true);
webView.setWebViewClient(new MyWebViewClient());
webView.loadUrl("http://www.mysite.com/android.php");
}else{
Toast.makeText(this,getString(R.string.no_connessione),Toast.LENGTH_LONG).show();
}
}
private class MyWebViewClient extends WebViewClient {
#Override
public void onPageFinished(WebView view, String url) {
if (progressBar != null && progressBar.isShowing()) {
progressBar.dismiss();
}
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
System.out.println("here");
if (Uri.parse(url).getHost().equals("mysite.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;
}
}
}
(Please ignore the bad formatting :p)

how to set a progress Dialog when a browser is being launched

I have come up with a private browser,but I want to know how to link it to my code below when the browser is being opened.
Main Class
b5.setOnClickListener(new OnClickListener(){
public void onClick(View v){
Intent i =new Intent(FiltersActivity.this,MyBrowser.class);
i.setData(Uri.parse("http://www.google.com"));
startActivity(i);
}
});
Second Class
public class MyBrowser extends Activity {
public void onCreate(Bundle SavedIntstanceState) {
super.onCreate(SavedIntstanceState);
setContentView(R.layout.browser);
Uri uri = getIntent().getData(); // get Intent and the linked data
WebView webview = (WebView) findViewById(R.id.webView_01); // get the
// webview
// item
webview.setWebViewClient(new Callback()); // set the object to the view
// to webview
webview.loadUrl(uri.toString()); // load the url
}
private class Callback extends WebViewClient { // crate a virtual class
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) { // /load
// the
// necessary
return (false);
}
}
}
You can check out this blog with complete example that shows progess of the webview in the Title Bar.
Also you can have a look in the Android Docs activity.setProgress(progress)
And finally but not the least StackOverflow Answer

Categories

Resources