how do i open external URL out of my app? - android

it is still running within my app. Can someone help me ? how do i open external URL out of my app?
public static void openUrl(final Activity context, final String url) {
openUrl(context, url, false);
}
public static void openUrl(final Activity context, final String url, final boolean withoutTransition) {
final Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
intent.setPackage(getAlternative(context));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
if (withoutTransition) {
context.overridePendingTransition(0, 0);
}
}

use web view or redirect url.
Intent intent = new Intent(MainActivity.this, Web_view.class);
intent.putExtra("url", "https://www.hive.co/contests/contest/5267/spotlight/");
startActivity(intent);
after this in web view
public class Web_view extends AppCompatActivity {
private WebView webView;
private ProgressBar progressBar;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.web_view);
progressBar = (ProgressBar) findViewById(R.id.progressBar_cyclic);
String url = getIntent().getStringExtra("url");
webView = (WebView) findViewById(R.id.webview);
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
progressBar.setVisibility(View.GONE);
}
});
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.loadUrl(url);
// progressBar.setVisibility(View.GONE);
}
}

Related

WebView is not loading webpage

I am using WebView for loading a website. But it is very slow and is leaking when specific websites are loaded.
I am loading WebView with the following code.
#Override
protected void onNewIntent(Intent intent) {
if (intent.getStringExtra("url") != null) {
webView.loadurl(intent.getStringExtra("url"));
}
}
But I am calling webView.loadUrl(Config.URL); (Config.URL may contain same url as specified above) in onCreate() method after initializing WebView with the following.
this.webView = (WebView) findViewById(R.id.wv);
this.webView.getSettings().setJavaScriptEnabled(true);
this.webView.getSettings().setLoadsImagesAutomatically(true);
this.webView.getSettings().setDomStorageEnabled(true);
this.webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
MyClient client = new MyClient(WebActivity.this, (ProgressBar)findViewById(R.id.progressBar));
webView.setWebViewClient(client);
Loading a from onCreate() is working fine (not fine, it's too slow). But
the same URL that is loading from onNewIntent() is not working!!!.
After I did this inonNewIntent() no URLs got loaded using
webView.loadurl() and the current page is getting immovable. ie. the
scrollbars are moving in WebView but page is not scrolling. I tested
the same URL in onCreate() and it is working.
For doing that I am passing url with
intent.putExtra("url", Config.URL+targetUrl);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
with the pending intent from the notifications. Although it is working in some devices i.e Google Nexus. But it is not working on most of the phones.
I have
android:hardwareAccelerated="true"
Myclient
public class MyClient extends WebViewClient{
private Context context;
private Activity activity;
private Handler handler;
private Runnable runnable;
private ProgressBar viewBar;
private String ret,ret2;
public void setFirstLoad(boolean firstLoad) {
this.firstLoad = firstLoad;
}
private boolean firstLoad=false;
public MyClient(Activity activity, ProgressBar bar) {
this.context = activity.getApplicationContext();
this.activity = activity;
viewBar=bar;
handler=new Handler();
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
/*if (url.startsWith("tel:")) {
Intent intent = new Intent(Intent.ACTION_DIAL,
Uri.parse(url));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}else if(url.startsWith("http:") || url.startsWith("https:")) {
*//*view.setVisibility(View.GONE);
viewBar.setVisibility(View.VISIBLE);*//*
view.loadUrl(url);
}
return true;*/
if (Uri.parse(url).getHost().equals("www.somepage.com")) {
return false;
}
// Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
try {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
Answers.getInstance().logShare(new ShareEvent()
.putContentId(Build.USER)
.putMethod(shareName(url))
.putContentName(contentDecode(url))
.putContentType("news_share"));
}catch (android.content.ActivityNotFoundException e){
Log.e("Activity not found",e.toString());
Toast.makeText(context,"Application not found",Toast.LENGTH_LONG).show();
}
return true;
}
#Override
public void onReceivedError(final WebView view, int errorCode, String description, final String failingUrl) {
//Clearing the WebView
try {
view.stopLoading();
} catch (Exception e) {
}
try {
view.clearView();
} catch (Exception e) {
}
if (view.canGoBack()) {
view.goBack();
}
view.loadUrl("about:blank");
//Showing and creating an alet dialog
AlertDialog.Builder alertDialog = new AlertDialog.Builder(activity);
alertDialog.setTitle("Error");
alertDialog.setMessage("No internet connection was found!");
alertDialog.setPositiveButton("Retry", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
view.loadUrl(failingUrl);
}
});
AlertDialog alert = alertDialog.create();
alert.show();
//Don't forget to call supper!
super.onReceivedError(view, errorCode, description, failingUrl);
}
#Override
public void onLoadResource(final WebView view, String url) {
super.onLoadResource(view, url);
//injectScriptFile(view, "js/script.js");
injectCSS(view,"css/style.css");
if (firstLoad){
firstLoad=false;
view.setVisibility(View.INVISIBLE);
viewBar.setVisibility(View.VISIBLE);
runnable=new Runnable() {
#Override
public void run() {
viewBar.setVisibility(View.GONE);
view.setVisibility(View.VISIBLE);
}
};
handler.postDelayed(runnable,2000);
}
// test if the script was loaded
// view.loadUrl("javascript:setTimeout(hideMe(), 200)");
}
/*#Override
public void onPageFinished(final WebView view, String url) {
//System.gc();
}*/
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
System.gc();
}
The question is: What is the problem when using loadurl() method in onNewIntent()?
Try this from where you loads webview
web.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return super.shouldOverrideUrlLoading(view, url);
}
});
You can use the webclient to handle the webview. Here I include the javascript with loading.
String aboutURL="YOUR URL";
final ProgressDialog pd = ProgressDialog.show(, "", "Please wait", true);
WebSettings settings=Webview.getSettings();
settings.setJavaScriptEnabled(true);
settings.setAppCacheEnabled(true);
settings.setDomStorageEnabled(true);
settings.setLoadsImagesAutomatically(true);
settings.setDatabaseEnabled(true);
settings.setRenderPriority(WebSettings.RenderPriority.HIGH);
settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
settings.setSupportZoom(true);
settings.setBuiltInZoomControls(true);
Webview.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon)
{
pd.show();
}
#Override
public void onPageFinished(WebView view, String url) {
pd.dismiss();
}
});
Webview.loadUrl(aboutURL);
Here Loading is processed based on network
Use Handler to post a delay action as below will fix this, but I don't known why.
new Handler().post(webView.loadurl(url));

How to add a Custom Error page in an Android WebView application without any errors?

The following code is the MainActivity of the app. I tried to add a Custom Error page by using:
mywebView.setWebViewClient(new WebViewClient() {
#Override public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
mywebView.loadUrl("file:///android_asset/error.html");
} });
When I use the above code in the main activity either the Error page or the Loading Icon are working at a time. (overriding each other).
I'm not understanding where I'm mistaking. Can anyone please help me fix this problem? Thanks in advance.
public class MainActivity extends AppCompatActivity {
public WebView mywebView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mywebView = (WebView)findViewById(R.id.webView);
WebSettings webSettings = mywebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mywebView.loadUrl("http://google.com/");
mywebView.setWebViewClient(new WebViewClient());
mywebView.setDownloadListener(new DownloadListener() {
public void onDownloadStart(String url, String userAgent,
String contentDisposition, String mimetype,
long contentLength) {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
});
mywebView.setWebViewClient(new WebViewClient() {
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
findViewById(R.id.progress).setVisibility(View.VISIBLE);
}
public void onPageFinished(WebView view, String url) {
findViewById(R.id.progress).setVisibility(View.GONE);
}
});
}
public void onBackPressed() {
if(mywebView.canGoBack()){
mywebView.goBack();
} else {
super.onBackPressed();
}
}}
mywebView.setWebViewClient(new WebViewClient() {
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
findViewById(R.id.progress).setVisibility(View.VISIBLE);
}
public void onPageFinished(WebView view, String url) {
findViewById(R.id.progress).setVisibility(View.GONE);
}
public void onReceivedError(WebView webview, int i, String s, String s1)
{
webview.loadUrl("file:///android_asset/error.html");
}
});
Finally I found the solution.

How to multi urls in single webview

I am new to webView ,here I had t onclick event when I click text one and open in webview and when I click text 2 open with in same webview any one please how to place two onclick with in same webview ,every textview string come from server
I tried this way but no use any ne please help me I search I google but their is no use
Here below my code
Activity.java
//webview onclick and get bundle
webviewurl=NewsMainFregmant_List.listData.get(pos).getNewsSourceUrl();
webviewurl2=NewsMainFregmant_List.listData.get(pos).getNewsSourceUrl2();
news_site_link_one=(TextView)findViewById(R.id.news_SourceLink_text_one_t_webview);
news_site_like_two=(TextView)findViewById(R.id.news_SourceLink_text_two_t_webview);
news_site_link_one.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent webviewintent = new Intent(getApplicationContext(), News_WebView.class);
webviewintent.putExtra("webviewurl", webviewurl);
startActivity(webviewintent);
}
});
news_site_like_two.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent webviewintent = new Intent(getApplicationContext(), News_WebView.class);
webviewintent.putExtra("webviewurl2", webviewurl2);
startActivity(webviewintent);
}
});
here my webview code
String SourceURL;
WebView webview;
final Activity activity = this;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_news__web_view);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
setContentView(R.layout.activity_news__web_view);
Intent intent =this.getIntent();
if(intent!=null)
SourceURL = intent.getStringExtra("webviewurl");
if(SourceURL.equals("webviewurl")) {
webview = (WebView) findViewById(R.id.webView);
webview.getSettings().setJavaScriptEnabled(true);
}
if(SourceURL.equals("webviewurl2")) {
webview = (WebView) findViewById(R.id.webView);
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);
}
});
webview.setWebViewClient(new WebViewClient() {
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
// Handle the error
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
webview.loadUrl(SourceURL);
}
Use containsKey
Bundle data =getIntent().getExtras();
if(data.containsKey("webviewurl"))
{
SourceURL =data.getString("webviewurl");
}
else if(data.containsKey("webviewurl2"))
{
SourceURL =data.getString("webviewurl2");
}
Here is code snippet I tried for multiple urls
Myactivity.java
public void openWeb() {
impressum = (TextView) findViewById(R.id.web_link_1);
konkact = (TextView) findViewById(R.id.web_link_2);
uber = (TextView) findViewById(R.id.web_link_3);
impressum.setOnClickListener(this);
konkact.setOnClickListener(this);
uber.setOnClickListener(this);
}
#Override
public void onClick(View v) {
String disclaimer = "http://lucidspace.de/imprint.php";
String contact = "http://lucidspace.de/contact.php";
String about = "http://lucidspace.de/about-us.php";
switch(v.getId()) {
case R.id.web_link_1:
Intent imprintIntent = new Intent(MainActivity.this, WebViewOpen.class);
imprintIntent.putExtra("webivewImprint",disclaimer);
this.startActivity(imprintIntent);
break;
case R.id.web_link_2:
Intent contactIntent = new Intent(MainActivity.this, WebViewOpen.class);
contactIntent.putExtra("webivewContact",contact);
this.startActivity(contactIntent);
break;
case R.id.web_link_3:
Intent aboutIntent = new Intent(MainActivity.this, WebViewOpen.class);
aboutIntent.putExtra("webivewAbout",about);
this.startActivity(aboutIntent);
break;
}
WebViewAcitivty
public class WebViewOpen extends AppCompatActivity {
String url;
WebView webView;
final Activity webViewActivity = this;
#Override
protected void onCreate(Bundle savedInstanceState) {
/*webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView wView, int progress) {
webViewActivity.setTitle("Loading...");
webViewActivity.requestWindowFeature(progress * 100);
if(progress == 100) {
webViewActivity.setTitle(R.string.app_name);
}
}
});*/
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_webview);
Intent intent =this.getIntent();
if(intent!= null) {
Bundle data = getIntent().getExtras();
if(data.containsKey("webivewImprint")) {
url = data.getString("webivewImprint");
webView = (WebView) findViewById(R.id.web_link);
webView.clearCache(true);
webView.clearHistory();
webView.getSettings().setJavaScriptEnabled(true);
}
if(data.containsKey("webivewContact")) {
url = data.getString("webivewContact");
webView = (WebView) findViewById(R.id.web_link);
webView.clearCache(true);
webView.clearHistory();
webView.getSettings().setJavaScriptEnabled(true);
}
if(data.containsKey("webivewAbout")) {
url = data.getString("webivewAbout");
webView = (WebView) findViewById(R.id.web_link);
webView.clearCache(true);
webView.clearHistory();
webView.getSettings().setJavaScriptEnabled(true);
}
webView.loadUrl(url);
WebView.xml
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/web_link"
android:layout_width="match_parent"
android:layout_height="match_parent" />
This has worked for me

Why doesn't a link open up in a pop up webview?

There is a website that I use which opens up in a new window on a desktop, now what I want to do is replicate that in an app. The code I have so far includes the setsupportformultiplewindows but when I click the link that opens the new window, nothing happens. my code for the browser is as follows
public class WebActivity extends Activity {
private WebView mWebView = null;
private EditText mInputUrl = null;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
super.onCreate(savedInstanceState);
getWindow().setFeatureInt( Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON);
setContentView(R.layout.activity_web_view);
Intent intent = getIntent();
String thesite = intent.getStringExtra(MainPage.EXTRA_MESSAGE);
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 baseurl = "http://";
String url = baseurl + mInputUrl.getText().toString();
mWebView.loadUrl(url);
}
});
mWebView = (WebView) findViewById(R.id.webview);
mWebView.loadUrl(thesite);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setWebViewClient(new WebViewClient());
mWebView.getSettings().setAllowFileAccess(true);
mWebView.getSettings().setPluginState(WebSettings.PluginState.ON);
mWebView.getSettings().setBuiltInZoomControls(true);
mWebView.getSettings().setLoadWithOverviewMode(true);
mWebView.getSettings().setUseWideViewPort(true);
mWebView.getSettings().setGeolocationEnabled(true);
mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
mWebView.getSettings().setSupportMultipleWindows(true);
final Activity MyActivity = this;
mWebView.setWebChromeClient(new WebChromeClient() {
public void onGeolocationPermissionsShowPrompt(String origin, android.webkit.GeolocationPermissions.Callback callback) {
callback.invoke(origin, true, false);
}
There is further code below but not related to my issue
EDIT:
Here is an image which shows how I would ideally like it. https://www.dropbox.com/s/gwllmjebtw13rzw/webview.png
Try this :
mWebView.setWebChromeClient(new WebChromeClient() {
#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);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Intent webIntent = new Intent(WebViewActivity.this,
NewWebViewActivity.class);
webIntent .putExtra("urlparam", url);
startActivity(webIntent);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
}
}
In shouldOverrideUrlLoading(),you can check the url and open it in another activity using startActivity().
Hope this helps.

android webview not displaying video using WebViewClient

i want to play a video from youtube on webview.. it displays the video,But i want to play it on the same page i mean i've to use WebViewClient.. but using that it doesn't play the video.. (on pressing play button it doesn't play the video) what should i do? my code is
setContentView(R.layout.main);
wvSpecials = (WebView) findViewById(R.id.webView1);
WebSettings webSettings = wvSpecials.getSettings();
webSettings.setJavaScriptEnabled(true);
wvSpecials.loadUrl("http://here.com/is link/");
wvSpecials.setWebViewClient(new WebViewClient() {
ProgressDialog progressDialog = new ProgressDialog(
specialsActivity.this);
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
progressDialog.setMessage("Please wait...");
progressDialog.show();
super.onPageStarted(view, url, favicon);
}
#Override
public void onPageFinished(WebView view, String url) {
if (progressDialog.isShowing()) {
progressDialog.dismiss();
}
super.onPageFinished(view, url);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return super.shouldOverrideUrlLoading(view, url);
}
});
}
You may use the below listed code:
public class YouTube extends Activity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
WebView myWebView;
myWebView = (WebView) findViewById( R.id.web);
myWebView.setWebViewClient(new MyWebViewClient());
String pre="<iframe class=youtube-player type=text/html width=";
String height=" height=";
String suffix=" src=http://www.youtube.com/embed/**xxxxxxxxxxx**?autoplay=1 frameborder=0>"; // replace xxxxxxxxxxx with the specific embed id of your video
String playVideo=pre+260+height+150+suffix;
myWebView.getSettings().setPluginsEnabled(true);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.loadData(playVideo,"text/html","UTF-8");
}
// override default behaviour of the browser
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
ProgressDialog dialog = ProgressDialog.show(getApplicationContext(), "",
"Loading. Please wait...", true);
#Override
public void onPageFinished(WebView view, String url) {
dialog.dismiss();
}
} }

Categories

Resources