how to start third activity if url is same in webview
for example i have webview
in my webview if url is like this
http://example.com/access.html
then start thirdactivity
how can i do this please help me to fix this issue
thanks
in advance
here is my webview code
public class SecondActivity extends AppCompatActivity {
private WebView wv1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
String url = getIntent().getStringExtra("url");
wv1=(WebView)findViewById(R.id.webView);
wv1.setWebViewClient(new WebViewClient());
wv1.getSettings().setLoadsImagesAutomatically(true);
wv1.getSettings().setJavaScriptEnabled(true);
wv1.loadUrl(url);
}
}
here is xml file of secondactivity
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorPrimaryDark"
tools:context="com.shuvro.barcodescanner.BarcodeScannerActivity">
<WebView
android:id="#+id/webView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageStarted(WebView view, String url, Bitmap
favicon) {
super.onPageStarted(view, url, favicon);
// write your logic here.
if (url.equals(pdfWebURL)) {
loadingIndicator.setVisibility(View.VISIBLE);
}
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
loadingIndicator.setVisibility(View.GONE);
}
});
please write your logic in onPageStarted.
Please try below code
public class WebView extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String weburl) {
if (weburl.equals("YOURLINK")) {
Intent i = new Intent(getContext(), YourActivity.class);
startActivity(i);
return true;
} else {
view.loadUrl(url);
return true;
}
}
}
Related
I have a URL of a presentation and i want to show this in the webview in android. I have tried the below code but it is redirecting in google drive app.
String url1 =
"https://docs.google.com/presentation/d/1nL5yO1HX_";
And my Webiew code is below:
WebView mywebview = findViewById(R.id.webview);
WebSettings settings = mywebview.getSettings();
settings.setJavaScriptCanOpenWindowsAutomatically(true);
settings.setBuiltInZoomControls(true);
settings.setAllowFileAccess(true);
settings.setJavaScriptEnabled(true);
mywebview.loadUrl(url1);
Use setWebViewClient and getHitTestResult.It is use to get the url which will be going to open and then check whether it is ppt file or not.
First XML File :-
<WebView
android:id="#+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"></WebView>
Main Activity :-
webView.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String request)
{
try{
if(webView.getHitTestResult().getExtra().endsWith(".pptx"))
{
try
{
Intent intent=new Intent(MainActivity.this,Main2Activity.class);
intent.putExtra("url",webView.getHitTestResult().getExtra());
startActivity(intent);
return true;
}
catch (Exception e)
{
Toast.makeText(MainActivity.this, e+"", Toast.LENGTH_SHORT).show();
}
return true;
}
else
{
webView.loadUrl(request);
return true;
}}
catch (Exception e)
{
return false;
}
}
Then in new activity get value of intent :-
public class Main2Activity extends AppCompatActivity {
WebView webView;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Intent intent=getIntent();
String t=intent.getStringExtra("url");
String s="http://docs.google.com/viewer?url=";
s=s.concat(t);
webView=(WebView)findViewById(R.id.webView);
webView.loadUrl(s);
}
#Override
protected void onResume()
{
this.finish();
super.onResume();
}
Second XML File :-
<WebView
android:id="#+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
I have used webclient and it worked for me
public class WebClient extends WebViewClient{
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
view.loadUrl(url);
return true;
}
}
Please help me. How to disable webview open new page ? 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:
public class MainActivity extends AppCompatActivity {
private WebView webView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = (WebView) findViewById(R.id.webView);
webView.setWebViewClient(new myWebClient());
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://example.com");
webView.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView webView, int errorCode, String description, String failingUrl) {
try {
webView.stopLoading();
} catch (Exception e) {
}
if (webView.canGoBack()) {
webView.goBack();
}
webView.loadUrl("about:blank");
AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
alertDialog.setTitle("Error");
alertDialog.setMessage("Check your internet connection and try again.");
alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "Try Again", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
finish();
startActivity(getIntent());
}
});
alertDialog.show();
super.onReceivedError(webView, errorCode, description, failingUrl);
}
});
}
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;
}
}
#Override
// This method is used to detect back button
public void onBackPressed() {
if(webView.canGoBack()) {
webView.goBack();
} else {
// Let the system handle the back button
super.onBackPressed();
}
}
}
In your WebViewClient, you can load only specific url that you want as below,
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url.equals("my url")) {
view.loadUrl(url);
}
return true;
}
Firstly you use web view you create web activity like this:
xml layout:-
<?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/webView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
WebView Activity:-
public class WebViewActivity extends AppCompatActivity {
#BindView(R.id.webView1)
WebView webView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web_view);
// i am using intent getting the value from like this
Intent intent2 = getIntent();
Bundle bundle = intent2.getExtras();
String link = bundle.getString("Agreement_URL");
Log.e("link---",""+link);
String file_type=bundle.getString("file_type");
if(file_type.equals("PDF"))
{
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("https://docs.google.com/gview?embedded=true&url="+link);
setContentView(webView);
}
else
{
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(link);
}
}
/** Method on BackPressed Button click*/
public void onBackPressed(){
super.onBackPressed();
/** Activity finish*/
finish();
}
pass the value like this from previous activity
Intent intent =new Intent(context, WebViewActivity.class);
intent.putExtra("Agreement_URL","http://54.183.245.32/uploads/"+ finalUploadDoc);
intent.putExtra("file_type","PDF");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Log.e("ggg",""+ finalUploadDoc);
context.startActivity(intent);
try this it helps you
I'm loading one URL in WebView, the URL page contains functionality of capturing image from device camera OR select image from gallery. If I open URL in Browser, everything works fine but, if I open the same URL in WebView then Camera and Gallery functionality do not works. I think I'm missing some WebView setting. Please check my below code and help me to solve my problem.
Below is my WebViewActivity--
public class WebViewActivity extends AppCompatActivity
{
WebView myWebView;
ProgressBar myProgress;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_webview);
findViews();
}
private void findViews()
{
myWebView= (WebView) findViewById(R.id.myWebView);
myProgress= (ProgressBar) findViewById(R.id.myProgress);
myWebView.setWebViewClient(new myWebClient());
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.getSettings().setLoadWithOverviewMode(true);
//Other webview settings
myWebView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
myWebView.setScrollbarFadingEnabled(false);
myWebView.getSettings().setBuiltInZoomControls(true);
myWebView.getSettings().setPluginState(WebSettings.PluginState.ON);
myWebView.getSettings().setAllowFileAccess(true);
myWebView.getSettings().setSupportZoom(true);
myWebView.loadUrl(SafeNestConstants.MEDLIFE_URL);
}
public class myWebClient extends WebViewClient
{
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon)
{
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
}
#RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request)
{
myProgress.setVisibility(View.VISIBLE);
view.loadUrl(request.getUrl().toString());
return true;
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
// TODO Auto-generated method stub
myProgress.setVisibility(View.VISIBLE);
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url)
{
// TODO Auto-generated method stub
super.onPageFinished(view, url);
myProgress.setVisibility(View.GONE);
}
}
}
Below is my activity_my_webview.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_terms_and_conditions"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.mypackage.WebViewActivity">
<WebView
android:id="#+id/myWebView"
android:layout_width="match_parent"
android:layout_height="match_parent">
</WebView>
<ProgressBar
android:id="#+id/myProgress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:indeterminateDrawable="#drawable/progress_medium"
android:visibility="visible" />
</RelativeLayout>
Also let me know if I can provide more information for the same. Thank you.
I am using webview with ProgressDialog. My issue is that on the Initial Launch, the progressDialog appears abd load properly but whenever I click a link (post links) inside webview, the progress is not showing. Here's the code -
public class Xyz extends WebViewClient
{
public void onLoadResource (WebView view , String url ) {
if (progressDialog == null ) {
progressDialog = new ProgressDialog(getActivity());
progressDialog . setMessage( "Loading..." );
progressDialog.setIndeterminate(true);
progressDialog . show();
}
}
public void onPageFinished(WebView view , String url ) {
if (progressDialog . isShowing()) {
progressDialog . dismiss();
}
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
view.loadUrl(url);
return true;
}
}
You are overriding the wrong method to achieve your goal. I've put up an Activity here which works as intended.
I'm using onPageStarted() instead of onLoadResource().
Activity MainActivity.java:
public class MainActivity extends AppCompatActivity {
ProgressDialog progressDialog;
WebViewClient mClient;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mClient = new WebViewClient() {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
if (progressDialog == null) {
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setMessage("Loading...");
progressDialog.setIndeterminate(true);
}
if(!progressDialog.isShowing()) {
progressDialog.show();
}
}
public void onLoadResource(WebView view, String url) {
}
public void onPageFinished(WebView view, String url) {
if (progressDialog!=null && progressDialog.isShowing()) {
progressDialog.dismiss();
}
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
view.loadUrl(url);
return true;
}
};
WebView webView = (WebView) findViewById(R.id.webview);
webView.setWebViewClient(mClient);
webView.loadUrl("http://www.google.com");
}
}
Layout activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.prasad.myapplication.MainActivity">
<WebView
android:id="#+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>
Try to track the webview load progress using webChoromeClient like this:
webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
progressBar.setProgress(progress);
if (progress == 100) {
progressBar.setVisibility(View.GONE);
} else {
progressBar.setVisibility(View.VISIBLE);
}
}
});
and replace your webview client as follwing
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
I wanna load webpage inside fragment, but there is some problem.
1.without overriding shouldOverrideUrlLoading method, the website is open with default browser Chrome.
2.when overriding shouldOverrideUrlLoading, nothing happens. just new blank activity is created.
This is my onCreateView method inside Fragment class
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View resultView = inflater.inflate(R.layout.container, container, false);
if (getArguments() != null) {
String url = getArguments().getString(KEY_URL);
WebView productDetailView = (WebView)resultView.findViewById(R.id.webView);
productDetailView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return super.shouldOverrideUrlLoading(view, url);
}
});
productDetailView.loadUrl(url);
}
return resultView;
}
targetSdkVersion is 19 and added INTERNET permission and XML file of Fragment is like below.
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.ProductDetailFragment">
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/webView"
android:layout_gravity="center" />
</FrameLayout>
what's the problem?
Try this code
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
webView.loadUrl(url);
and start your activity with FLAG_ACTIVITY_NEW_TASK
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
Intent intent = getIntent();
webView.setSaveEnabled(false);
String receivedName = (String) intent.getSerializableExtra("USERNAME");
Log.d("receivedName ----- ", receivedName);
webView.loadUrl("https://docs.google.com/gview?embedded=true&url=" +
receivedName);
webView.setDownloadListener(new DownloadListener() {
public void onDownloadStart(String url, String userAgent,
String contentDisposition, String mimetype,
long contentLength) {
Webview.this.finish();
}
});
Try this code...
Try this.
final ProgressDialog pd = ProgressDialog.show(ActivityA.this, "", "Loading...", true);
wv = (WebView) findViewById(R.id.webview);
wv.getSettings().setJavaScriptEnabled(true);
wv.loadUrl("<url");
wv.setWebViewClient(new WebViewClient() {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon)
{
pd.show();
}
#Override
public void onPageFinished(WebView view, String url) {
pd.dismiss();
}
});
Have you tried adding this <uses-permission android:name="android.permission.INTERNET" />
into Manifest file ?
https://developer.android.com/reference/android/webkit/WebView.html