pass a link in a webview - java

I am trying when someone click for example : eee.com , to open it in my Webview app
public class MainActivity extends AppCompatActivity {
public static WebView webView;
#RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = (WebView) findViewById(R.id.mainWebview);
webView.setWebViewClient(new WebViewClient());
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.loadUrl("https://www.eee.com/");
webView.setWebViewClient(new WebViewClient(){
#RequiresApi(api = Build.VERSION_CODES.N)
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url.contains("eee.com")) {
view.loadUrl(url);
} else {
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(i);
}
return true;
}
});
}
#Override
protected void onResume() {
super.onResume();
Uri uri = this.getIntent().getData();
webView.loadUrl(uri.toString());
}
but the app crashes on open it right away , I tried this : :
#Override
protected void onResume() {
super.onResume();
Uri uri = this.getIntent().getData();
if(getIntent().getData() != null){
webView.loadUrl(uri.toString());
}
}
app works but on link click it just open the app without passing the link

I solved it my self with :
#Override
protected void onNewIntent(Intent intent)
{
super.onNewIntent(intent);
Uri x = intent.getData();
webView.loadUrl(x.toString());
}

Related

android webview to download pdf in webview app

I have created an app with android webview. It loads google chrome website. How can I restrict it to open other links in the same browser app and not go to phone's default browser? Also with the below code I am able to open and download random pdf by search on google(webview). I need to be able to open in same webview and also be able to download pdf in the same webview app. How?
public class WebViewPage extends AppCompatActivity {
WebView webView;
Button closeweb;
WebViewClient client;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web_view_page);
webView =findViewById(R.id.webview);
closeweb = findViewById(R.id.closeweb);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
WebViewClientImpl webViewClient = new WebViewClientImpl(this);
webView.setWebViewClient(webViewClient);
webView.loadUrl("https://www.google.in");
closeweb.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(WebViewPage.this, HomePage.class);
startActivity(intent);
}
});
}
#Override
public void onBackPressed() {
}
}
WebViewClientImpl.class
public class WebViewClientImpl extends WebViewClient {
private Activity activity = null;
public WebViewClientImpl(Activity activity) {
this.activity = activity;
}
#Override
public boolean shouldOverrideUrlLoading(WebView webView, String url) {
if(url.indexOf("https://www.google.in") > -1 ) return false;
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
activity.startActivity(intent);
return true;
}
}

Back button not working when Interstitial ad not available

I have placed an interstitial with click on back button in my Web View app. However , back button doesn't work when ad is not available. However, it seems to work fine after an interstitial ad has been loaded once. I am not a professional developer. Any help is much appreciated. Thank you.
#SuppressLint("SetJavaScriptEnabled")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
//admobinterstital
MobileAds.initialize(this, new OnInitializationCompleteListener() {
public void onInitializationComplete(InitializationStatus initializationStatus) {
}
});
InterstitialAd.load(this, "ca-app-pub-3940256099942544/1033173712", new AdRequest.Builder().build(), new InterstitialAdLoadCallback() {
public void onAdLoaded(InterstitialAd interstitialAd) {
InterstitialAd unused = MainActivity2.this.mInterstitialAd = interstitialAd;
Log.i("TAG", "onAdLoaded");
}
public void onAdFailedToLoad(LoadAdError loadAdError) {
Log.i("TAG", loadAdError.getMessage());
InterstitialAd unused = MainActivity2.this.mInterstitialAd = null;
}
});
webView = findViewById(R.id.webview);
webView.setWebViewClient(new WebViewClient() {
});
//WEB VIEW SETTINGS
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setSupportZoom(true);
webSettings.setBuiltInZoomControls(true);
webSettings.setDisplayZoomControls(false);
webSettings.setUseWideViewPort(true);
webSettings.setLoadWithOverviewMode(true);
//LIST VIEW
int position = getIntent().getIntExtra("story_key", 0);
//FOR MORE URL
}
// ENABLE BACK PRESS
public void onBackPressed() {
InterstitialAd interstitialAd = this.mInterstitialAd;
if (interstitialAd != null) {
interstitialAd.show(this);
if (this.webView.canGoBack()) {
this.webView.goBack();
} else {
super.onBackPressed();
}
}
}
}

Loading created by ProgressDialog wouldn't stop , how to hide progressbar when page is loaded

I'm new to Java and was trying to create a web-view app. The loading widget was created using Progress Dialog but it wouldn't stop. I don't know what is wrong. Please help.
public class MainActivity extends AppCompatActivity {
private WebView mywebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ProgressDialog pd = ProgressDialog.show(this, "", "Loading...",true);
mywebView = (WebView) findViewById(R.id.webview);
mywebView.getSettings().setJavaScriptEnabled(true);
mywebView.getSettings().setSupportZoom(true);
mywebView.getSettings().setBuiltInZoomControls(true);
mywebView.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
if(pd!=null && pd.isShowing())
{
pd.dismiss();
}
}
});
mywebView.loadUrl("https://google.com");
mywebView.setWebViewClient(new WvClient());
}
private class WvClient extends WebViewClient
{
#Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError er) {
handler.proceed();
}
}
#Override
public void onBackPressed() {
if(mywebView.canGoBack())
{
mywebView.goBack();
}
else
{
super.onBackPressed();
}
}
}
You have declared setWebViewClient twice , ie after load url you again
declared the setWebViewClient with your custom client , correct answer is
public class MainActivity extends AppCompatActivity {
private WebView mywebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ProgressDialog pd = ProgressDialog.show(this, "", "Loading...",true);
mywebView = (WebView) findViewById(R.id.webview);
mywebView.getSettings().setJavaScriptEnabled(true);
mywebView.getSettings().setSupportZoom(true);
mywebView.getSettings().setBuiltInZoomControls(true);
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
pd.show();
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
if(pd!=null && pd.isShowing())
{
pd.dismiss();
}
}
#Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
super.onReceivedError(view, request, error);
if(pd!=null && pd.isShowing())
{
pd.dismiss();
}
}
});
mywebView.loadUrl("https://google.com");
}
#Override
public void onBackPressed() {
if(mywebView.canGoBack())
{
mywebView.goBack();
}
else
{
super.onBackPressed();
}
}
}
Place a breakpoint in
onPageFinished
and see if the listener actually get called?

Webview set overrideurl not in use

Im trying to use the shouldOverrideUrlLoading to handle some tel: links on my webview, but i get this error:
error: method does not override or implement a method from a supertype
How can i fix that?
public class MainActivity extends AppCompatActivity {
private WebView myWebView;
#Override
protected 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("https://**************/");
myWebView.setWebViewClient(new WebViewClient());
final ProgressBar Pbar;
Pbar = (ProgressBar) findViewById(R.id.pB1);
myWebView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress)
{
if(progress < 100 && Pbar.getVisibility() == ProgressBar.GONE){
Pbar.setVisibility(ProgressBar.VISIBLE);
}
Pbar.setProgress(progress);
if(progress == 100) {
Pbar.setVisibility(ProgressBar.GONE);
myWebView.requestFocus(View.FOCUS_DOWN);
}
}
#SuppressWarnings("deprecated")
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.startsWith("tel:")) {
Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
startActivity(intent);
view.reload();
return true;
}
view.loadUrl(url);
return true;
}
});
}
}
the override is grayed out but i cant figure how to fix it, im not very familiar with java and its driving me crazy, thanks in advance!
This is because there is no shouldOverrideUrlLoading in the WebChromeClient that you are trying to override.
You can override the shouldOverrideUrlLoading of WebChromeClient as it is present in it

Prevent Double Tap on Android WebView

I have a fullscreen WebView and I want to;
If my user double tap the screen, prevent second tap.
I am very very new on Android, please be very clear. I pasted my codes;
MainActivity.java
public class MainActivity extends Activity {
private WebView mWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = (WebView) findViewById(R.id.activity_main_webview);
// Force links and redirects to open in the WebView instead of in a browser
mWebView.setWebViewClient(new WebViewClient());
// Enable Javascript
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
// REMOTE RESOURCE
mWebView.loadUrl("https://www.example.com");
mWebView.setWebViewClient(new MyWebViewClient());
}
// Prevent the back-button from closing the app
#Override
public void onBackPressed() {
if(mWebView.canGoBack()) {
mWebView.goBack();
} else {
super.onBackPressed();
}
}
}
MyWebViewClient.java
public class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().endsWith("www.example.com")) {
return false;
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
view.getContext().startActivity(intent);
return true;
}
}

Categories