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.
Android WebView is a system component for the Android operating system (OS) that allows Android apps to display content from the web directly inside an application.
Android App Development for Beginners This example demonstrate about How to enable webview java script in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.
You might often face issues in updating the chrome and Android System Webview. To fix this problem, you can reboot your device, check your internet connection, stop auto-updating all apps, clear Google Playstore cache, and storage, leave the beta testing program, and manually update Android WebView app from Playstore.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With