loading different urls via WebView settings - android

I have a WebView app which loads a url, I want to create a Settings Activity where user can customize settings..
My que is, How to make a RadioButton or Chooser which when clicked will open the Mobile version of Site, Similarly if Desktop Ver. is clicked, webview will load Desktop version...

By default webview loads mobile site, if you want to force load desktop site you can use
String newUA= "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.4)Gecko/20100101 Firefox/4.0";
mWebView.getSettings().setUserAgentString(newUA);
use radio button to choose this settings. Use isChecked() to check radio button else load normal webview.

You can try code below
if(rbtnMobile.isChecked){
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setUserAgentString("Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1A543a Safari/419.3");
  webview.loadUrl(url);
}else{
webview.loadUrl(url);
}

Below is WebView code.
public class WebViewLinks extends Activity {
WebView webView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.web_view);
String Url = getIntent().getExtras().getString("Url");
webView = (WebView) findViewById(R.id.webView);
webView.setWebViewClient(new MyWebClient());
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.loadUrl(Url);
}
public class MyWebClient extends WebViewClient {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
}
You can use below code on button click or radio button selection.
Intent webViewIntent = new Intent(this, WebViewLinks.class);
webViewIntent.putExtra("Url", yourURL);
startActivity(webViewIntent);
see below example.
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if(checkedId == R.id.mobileview){
Intent webViewIntent = new Intent(this, WebViewLinks.class);
webViewIntent.putExtra("Url", mobileURL);
startActivity(webViewIntent);
}else if(checkedId == R.id.desktopview){
Intent webViewIntent = new Intent(this, WebViewLinks.class);
webViewIntent.putExtra("Url", deskTopURL);
startActivity(webViewIntent);
}
}
});

Related

Android webview load Instagram URL but URL not changed

I have a simple webview app that loads my website. Everything is ok, but when the user clicks on the Instagram icon in bottom of the webpage, I want to open the Instagram app instead of loading the Instagram webpage.
I'm trying to achieve this with shouldOverrideUrlLoading function and webview.geturl("https://www.instagram.com/").
The problem is when the user clicks on the Instagram icon, the URL doesn't change and stays default "http://archclub.ir/".
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);
webView.setWebViewClient(new WebViewClient());
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://archclub.ir/login");
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
url = webView.getUrl();
Toast.makeText(MainActivity.this, webView.getUrl(), Toast.LENGTH_SHORT).show();
if (url.contains("https://www.instagram.com/")) {
Toast.makeText(MainActivity.this, "salam", Toast.LENGTH_SHORT).show();
webView.loadUrl("http://archclub.ir/");
}
return false;
}
});
}}
My problem is: When the user clicks on the Instagram icon on the website, the Toast.makeText(MainActivity.this, "salam", Toast.LENGTH_SHORT).show(); doesn't run.
I've checked this. I've debugged it. The problem is when the Instagram icon is clicked, the webview.geturl is equal to archclub.ir/login and doesn't change to instagram.com but the webview shows the Instagram page.
you were replacing the url over and over.
please replace this block of code instead of shouldOverrideUrlLoading method :
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// url = webView.getUrl(); // just omit this line
Toast.makeText(ali.this, webView.getUrl(), Toast.LENGTH_SHORT).show();
if (url.contains("https://www.instagram.com/")) {
Toast.makeText(ali.this, "salam", Toast.LENGTH_SHORT).show();
webView.loadUrl("http://archclub.ir/");
return true;
} else return false;
}
});

WebView showing webpage loading progress only once

This similar SO question didn't help.
I have implemented a simple WebView to my MainActivity which loads a particular webpage on app launch. I have a TextView that shows the loading % of current webpage. The code is as below
public class MainActivity extends AppCompatActivity {
WebView webView;
final String homeUrl = "https://example.com";
String lastAccessedUrl = homeUrl;
WebChromeClient webChromeClient;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
webView = (WebView) findViewById(R.id.browser);
WebSettings settings = webView.getSettings();
settings.setJavaScriptEnabled(true);
settings.setBuiltInZoomControls(true);
settings.setLoadsImagesAutomatically(false);
settings.setLoadWithOverviewMode(true);
settings.setUseWideViewPort(true);
webView.canGoBack();
webView.canGoForward();
webView.setWebViewClient(new MyBrowser());
webView.loadUrl(homeUrl);
webChromeClient = new WebChromeClient(){
#Override
public void onProgressChanged(WebView view, int newProgress) {
TextView textView = (TextView) findViewById(R.id.loadingPercentage);
textView.setText(""+newProgress);
}
};
webView.setWebChromeClient(webChromeClient);
}
private class MyBrowser extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
lastAccessedUrl = url;
view.loadUrl(url);
return true;
}
}
}
The problem I am facing is, the TextView shows loading % only for the first time the webpage loads. Then it is set to 100 permanently. Then if I click on any link on the first page, the TextView does not start from zero.
Do justify in comments if you vote down.
Any help would be appreciated.
There is no such problem with your code. Make sure you are clicking on a url. If you click on an expand view, it will just expand to show you certain data. It wont load as a new url. Click on say, www.stackoverflow.com and check if it's showing you loading percentage.

Android:Open iframe of WebView in Browser/YouTube App

I have some JSON content of a blog which i am displaying using loadDataWithBaseURL in WebView. Many articles of this blog have embedded youtube/vimeo videos.
Currently, all the videos are played inside the webview itself.
What i want to achieve is, when the user clicks to Play the Video, he should get the popup "Complete Action Using" Browser or YouTube App. In short, i dont want the videos to be played inside the webview of app.
I found questions where everyone was trying to avoid this popup.. Dont' know why i am not getting it.
Here is my code
public class DetailFragment extends Fragment {
String strTitle = null;
String strURL = null;
String strContent = null;
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setHasOptionsMenu(true);
AdView mAdView = (AdView) getView().findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.detail_fragment, container, false);
strTitle = getArguments().getString("title");
strURL = getArguments().getString("url");
strContent = getArguments().getString("content");
String contToDisplay = "<html><head><meta http-equiv='Content-Type' content='text/html' charset='UTF-8' /></head><body><style type='text/css'> img{width:80%;} .wp-image-42223{display:none;}</style>" + strContent+"</body></html>";
TextView title = (TextView) view.findViewById(R.id.title);
WebView desc = (WebView) view.findViewById(R.id.desc);
WebSettings ws = desc.getSettings();
ws.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
ws.setJavaScriptEnabled(true);
ws.setLoadWithOverviewMode(true);
ws.setUseWideViewPort(true);
ws.setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
ws.setUserAgentString("Mozilla/5.0 (Linux; U; Android 2.0; en-us; Droid Build/ESD20) AppleWebKit/530.17 (KHTML, like Gecko) Version/4.0 Mobile Safari/530.17");
title.setText(strTitle);
// desc.loadData(contToDisplay, "text/html; charset=UTF-8", null);
desc.loadDataWithBaseURL("about:blank",strContent,"text/html", "UTF-8", null);
return view;
}
}
any solution that wold make the videos load in browser or YouTube App would help.
NOTE
The content i am displaying in webview is in HTML format with iframe as one of the elements. Here is the sample data:
"<p>Some text</p>
<p><iframe src=\"https://www.youtube.com/embed/q7uBAtaK15I\" width=\"560\" height=\"315\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\"></iframe> </p>
<p>some more text</p>
This entire content is displayed in webview. What i want to achieve is when someone clicks on iframe, he should be prompted to complete action using that is, the person shd not be allowed to play the video in webview
I managed to achieve this using regex.
Here is what i did
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.detail_fragment, container, false);
strTitle = getArguments().getString("title");
strURL = getArguments().getString("url");
strContent = getArguments().getString("content");
contToDisplay = "<html><head><meta http-equiv='Content-Type' content='text/html' charset='UTF-8' /><script type='text/javascript'></script></head><body><style type='text/css'> img{width:80%;} .wp-image-42223{display:none;} </style>" + strContent+"</body></html>";
TextView title = (TextView) view.findViewById(R.id.title);
TextView date = (TextView) view.findViewById(R.id.date);
TextView authorName = (TextView) view.findViewById(R.id.auth);
desc = (WebView) view.findViewById(R.id.desc);
setHasOptionsMenu(true);
WebSettings ws = desc.getSettings();
ws.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
ws.setJavaScriptEnabled(true);
ws.setLoadWithOverviewMode(true);
ws.setUseWideViewPort(true);
ws.setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
ws.setSupportMultipleWindows(true);
ws.setUserAgentString("Mozilla/5.0 (Linux; U; Android 2.0; en-us; Droid Build/ESD20) AppleWebKit/530.17 (KHTML, like Gecko) Version/4.0 Mobile Safari/530.17");
title.setText(strTitle);
final String s = strContent;
final String regex = "(?<=<iframe src=\")[^\"]*";
final String regex2 = "<\\s*iframe[^>]*><\\s*/\\s*iframe>";
p = Pattern.compile(regex);
final Pattern p2 = Pattern.compile(regex2);
m = p.matcher(s);
m2 = p2.matcher(s);
evaluate(s,contToDisplay,this);
desc.loadData(value, "text/html; charset=UTF-8", null);
return view;
}
String evaluate(String token,String defaultValue, DetailFragment context){
value=null;
Matcher matcher=p.matcher(token);
if (matcher.find()) {
iframeURL=matcher.group();
String newURL = "Watch the Video Here";
value = m2.replaceAll(newURL);
}
if (value == null) {
value=defaultValue;
}
return value != null ? value : "";
}
Might help someone with similar issue.
This may help.we are loading a new url load in the correct native app.
desc.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view,
String url)
{
return true;
}
});
desc.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view,
String url)
{
if(url.contains("youtube.com")){
// Could be cleverer and use a regex return
return super.shouldOverrideUrlLoading(view, url);
}
return true;
});
If the links are embedded in an iframe (as the title suggested) you have to set a WebChromeClient to your webview and override the onCreateWindow-method:
#Override
public boolean onCreateWindow (WebView view, boolean isDialog, boolean isUserGesture, Message resultMsg) {
WebView targetWebView = new WebView(view.getContext());
targetWebView.setWebViewClient(new WebViewClient(){
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
//simple url matching
if (url.contains("youtube.com") == true) {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
view.getContex().startActivity(i);
}
view.stopLoading();
}
});
WebView.WebViewTransport transport = (WebView.WebViewTransport) resultMsg.obj;
transport.setWebView(targetWebView);
resultMsg.sendToTarget();
return true;
}
And at the websettings of your webview, you have to set:
settings.setSupportMultipleWindows(true);

Zoom in/out in webview android

Hi i have used following code for built in zoom in zoom out control
mWebView = (WebView) findViewById(R.id.webView1);
mWebView.getSettings().setBuiltInZoomControls(true);
mWebView.getSettings().setSupportZoom(true);
also i have used code like below
mWebView.setWebViewClient(new MyWebViewClient());
mWebView.loadUrl(sabNZBurl);
mWebView.getSettings().setDomStorageEnabled(true);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setBuiltInZoomControls(true);
mWebView.getSettings().setSupportZoom(true);
backwardBtn = (Button) findViewById(R.id.btnBackWard);
cancelBtn = (Button) findViewById(R.id.btnCancel);
refreshBtn = (Button) findViewById(R.id.btnRefresh);
forwardBtn = (Button) findViewById(R.id.btnForward);
backwardBtn.setOnClickListener(this);
cancelBtn.setOnClickListener(this);
refreshBtn.setOnClickListener(this);
forwardBtn.setOnClickListener(this);
final class MyWebViewClient extends WebViewClient {
#Override
// Show in WebView instead of Browser
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onLoadResource(WebView view, String url) {
super.onLoadResource(view, url);
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
PD.dismiss();
view.getSettings().setBuiltInZoomControls(true);
view.getSettings().setSupportZoom(true);
}
}
but still webview does not come with built in zoom in zoom out control can any body help me to solve this problem
Do you have a viewport meta tag in web page to display? If yes and if it contains user-scalable="no" then it is normal, the HTML itself disables zoom. Simply edit the tag like this:
<meta name="viewport" content="initial-scale=1.0, user-scalable=yes, width=device-width" />
Try this, I got answer
WebSettings webSetting = mWebView.getSettings();
webSetting.setBuiltInZoomControls(true);
May be this is what your are searching..
settings.setBuiltInZoomControls(true);
settings.setSupportZoom(true);
settings.setUseWideViewPort(true);
settings.setJavaScriptEnabled(true);
settings.setJavaScriptCanOpenWindowsAutomatically(true);
try this:-
webview.getSettings().setUseWideViewPort(true);

twitter url can not open in android webview

I m beginner in android.
i want to open twitter url in webview. i have a twitter url(http://twitter.com/..). i try to this code but can not open in webview.
code:
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress)
{
activity.setTitle(" Loading...");
activity.setProgress(progress * 100);
if(progress == 100){
activity.setTitle(R.string.app_name);
//view.canGoBack();
}
}
});
webView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url){
view.loadUrl(url);
view.canGoBack();
return true;
}
});
//The URL that webview is loading
webView.loadUrl(uri);
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(event.getAction() == KeyEvent.ACTION_DOWN){
switch(keyCode)
{
case KeyEvent.KEYCODE_BACK:
if(webView.canGoBack() == true){
webView.goBack();
}else{
finish();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
help me.....
I got solution in my Question
webView.getSettings().setUserAgentString("Mozilla/5.0 (Linux; U; Android 2.0; en-us; Droid Build/ESD20) AppleWebKit/530.17 (KHTML, like Gecko) Version/4.0 Mobile Safari/530.17");
try this:
Uri uri = Uri.parse(Place your URL here);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
Hope this Helps:
DisplayWeb.java:
public class DisplayWeb extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.web);
String url = "http://www.twitter.com";
System.out.println("++++URL GOT = "+url);
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.getSettings().setBuiltInZoomControls(true);
myWebView.getSettings().setSupportZoom(true);
myWebView.loadUrl(url);
Button back = (Button) findViewById(R.id.back);
back.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
DisplayDirections.this.finish();
}
});
}
}
web.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<WebView
android:id="#+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</RelativeLayout>
Hope this help!
I had the same issue and have been doing some searching to look for possible answers to this problem.
Hacking the Browser Agent might work but it's not really a good idea.
The solution given in this post also worked for me and I think is a better solution: Problems loading mobile.twitter in webview
Hope that helps.

Categories

Resources