I have created a webview app of my site in Android Studio. Could someone please look into the code and solve the issue. This is my code:
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(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url == null || url.startsWith("http://") || url.startsWith("https://")) return false;
try {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
view.getContext().startActivity(intent);
return true;
} catch (Exception e) {
return true;
}
}
});
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setDomStorageEnabled(true);
webView.loadUrl("https://xyz.in/admin_login.php");
}
public void onBackPressed(){
if(webView.canGoBack()){
webView.goBack();
}else{
super.onBackPressed();
}
}
When WhatsApp icon is clicked, it's opening in WhatsApp application,but when view location is clicked, the button is not responding. Please help on this issue.
Please find the attached image for your reference
Related
I just wanted to open a second activity (webview) when user click on links whitch ends like .html.
MainActivity, and SecondActivity are the same
Code looks as follow:
if (url.endsWith(".html")) {
try {
view.getContext().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
} catch (Exception e) {
// error
}
return true;
}
Of course this opens the content into webbrowser outside the app.
Can you please help me in this?
First of all add this to your manifest
<uses-permission android:name="android.permission.INTERNET"/>
And add this activity as well
<activity android:name=".WebViewClientDemoActivity"/>
Now in your mainActivity you create intent like this
startActivity(new Intent(this, WebViewClientDemoActivity.class));
Now create this class
public class WebViewClientDemoActivity extends Activity {
WebView web;
#Override #SuppressLint("SetJavaScriptEnabled")
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
web = findViewById(R.id.web_view);
web.setWebViewClient(new myWebClient());
web.getSettings().setJavaScriptEnabled(true);
//for example i used this web page
web.loadUrl("https://stackoverflow.com/questions/69032318/java-android-open-secondactivity-when-url-endswith-html-webview");
}
public static 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
public boolean onKeyDown(int keyCode, KeyEvent event){
if ((keyCode == KeyEvent.KEYCODE_BACK) && web.canGoBack()){
web.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
By the way there is a probelm using the emulator with webview you can use your phone or you can see this solution
It opens my link in Webview but when I try to open hyperlinks and content of the web or navigation link, it starts opening with the default browser.
public class MainActivity extends Activity {
private WebView mWebView;
#SuppressLint("SetJavaScriptEnabled")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = findViewById(R.id.activity_main_webview);
mWebView.setWebViewClient(new WebViewClient());
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mWebView.loadUrl("http://");
}
#Override
public void onBackPressed() {
if(mWebView.canGoBack()) {
mWebView.goBack();
} else {
super.onBackPressed();
}
}
}
class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Objects.requireNonNull(Uri.parse(url).getHost()).endsWith("http://www.ieltsmadeeasy.tk/")) {
return false;
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
view.getContext().startActivity(intent);
return true;
}
}
I want to browse the whole website inside the web view, not any browser please help me
Here is some documentation about WebViews: https://developer.android.com/guide/webapps/webview#java
The problem is, that you don't override the shouldOverrideUrlLoading(view, url) function of the WebViewClient, as you use the default one.
If you use this as the WebViewClient, it should work (copied from examples in documentation):
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("https://www.example.com")) {
// This is my website, so do not override; let my WebView load the page
return false;
}
// Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}
So, just take this class and change the line mWebView.setWebViewClient(new WebViewClient()); to mWebView.setWebViewClient(new MyWebViewClient());
Hi i've been searching a lot but couldn't find anything that could help.
I am creating an android app for our existing website. The website itself will load, but the main form where users can sign in won't load. Does anyone have any idea how this happens?
The website is https://app.one-2-ten.com/v2
p.s. This is the first time I am making an android app so sorry if its an obvious solution.
ProgressBar progressBar;
WebView webView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
webView = (WebView) findViewById(R.id.webView);
if (savedInstanceState != null) {
webView.restoreState(savedInstanceState);
}
else{
webView.getSettings() .setJavaScriptEnabled(true);
webView.getSettings() .setSupportZoom(false);
webView.getSettings() .setBuiltInZoomControls(false);
webView.getSettings() .setLoadWithOverviewMode(true);
webView.getSettings() .setUseWideViewPort(true);
webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
webView.setWebViewClient(new ourViewClient());
webView.Settings.AllowContentAccess = true;
webView.setWebChromeClient(new WebChromeClient(){
public void onProgressChanged (WebView view, int progress){
progressBar.setProgress(progress);
if(progress < 100 && progressBar.getVisibility() == ProgressBar.GONE){
progressBar.setVisibility(ProgressBar.VISIBLE);
}if(progress == 100){
progressBar.setVisibility(ProgressBar.GONE);
}
}
}
);}
String data = getIntent() .getDataString();
if(Intent.ACTION_VIEW.equals(getIntent() .getAction())){
webView.loadUrl(data);
}else{
webView.loadUrl("https://one2ten-system.parseapp.com/v2");
}
}
public class ourViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView v, String url) {
if (url.contains("one2ten-system.parseapp.com/v2")) {
v.loadUrl(url);
CookieManager.getInstance().setAcceptCookie(true);
} else {
Uri uri = Uri.parse(url);
startActivity(Intent.createChooser(new Intent(Intent.ACTION_VIEW, uri), "Choose browser"));
}
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
}
}
#Override
public void onBackPressed() {
if(webView.canGoBack()) {
webView.goBack();
} else {
super.onBackPressed();
}
}
#Override
public void onSaveInstanceState(Bundle outState){
super.onSaveInstanceState(outState);
webView.saveState(outState);
}
}
I've found what my issue was, I forgot to add the following line: webView.getSettings().setDomStorageEnabled(true);
This enables the browser to store user data from forms for example, which it needs because you land on a login page.
If my answer is incomplete please let me know!
Thanks
This is my splashscreen activity.
public class Splash extends Activity {
private static int SPLASH_TIME_OUT=10000;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
Timer r=new Timer();
new Handler().postDelayed(r,SPLASH_TIME_OUT);
}
class Timer implements Runnable{
#Override
public void run() {
// TODO Auto-generated method stub
Intent i=new Intent(Splash.this,MainActivity.class);
startActivity(i);
finish();
}
}
}
This is my MainActivity. This activity Should be performed in background while splashscreen is on the front. Is it advisable to use AsyncTask. How to do that?
If not AsyncActivity, What can I use?
public class MainActivity extends Activity {
private WebView webview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webview = (WebView)findViewById(R.id.output);
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setLoadsImagesAutomatically(true);
webview.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
webview.getSettings().setAppCacheEnabled(false);
webview.setWebViewClient(new WebViewClient(){
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
if(url.startsWith("tel:")) {
Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
startActivity(intent);
return true;
}
return false;
}
}
);
webview.loadUrl("http://www.example.com");
}
//this is to go back to previous pages if exists
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(event.getAction() == KeyEvent.ACTION_DOWN){
switch(keyCode)
{
case KeyEvent.KEYCODE_BACK:
if(webview.canGoBack()){
webview.goBack();
}else{
finish();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
}
If possible can I set the time for the splashscreen to view as long as the url is loaded without giving time?
The second attempt
public class MainActivity extends Activity {
WebView webview;
private boolean isSplashOn = true;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webview.setBackgroundColor(0);
webview.setBackgroundResource(R.drawable.activity_splash);
webview = (WebView)findViewById(R.id.output);
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setLoadsImagesAutomatically(true);
webview.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
webview.getSettings().setAppCacheEnabled(false);
webview.setWebViewClient(new WebViewClient(){
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
if(url.startsWith("tel:")) {
Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
startActivity(intent);
return true;
}
return false;
}
public void onPageFinished(WebView view, String url) {
if(isSplashOn) {
webview.setBackgroundDrawable(null);
webview.setBackgroundColor(0);
isSplashOn = false;
}
super.onPageFinished(view, url);
}
}
);
webview.loadUrl("http://www.example.com");
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(event.getAction() == KeyEvent.ACTION_DOWN){
switch(keyCode)
{
case KeyEvent.KEYCODE_BACK:
if(webview.canGoBack()){
webview.goBack();
}else{
finish();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
}
Guys. Still the problem is't solved. Need some help
You can use the simple trick -
I was showing a loading spinner till the page was being loaded in background.
Thus I had used two web views.
In your case, you can use ImageView or any other view.
XML file will like this -
<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"
tools:context="${relativePackage}.${activityClass}" >
//Actual webview
<WebView
android:id="#+id/actual_webView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
//Any view you want to show while webpage is loading
<WebView
android:id="#+id/spinner_webView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
...
</RelativeLayout>
Now, in your JAVA file-
//initially, make actual_webview invisible
actual_webview.setVisibility(View.INVISIBLE);
Also, in
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
// make spinner_webview invisible
spinner_webview.setVisibility(View.INVISIBLE);
// make actual_webview visible
actual_webview.setVisibility(View.VISIBLE);
}
Also,
shouldOverrideUrlLoading(...)
onPageStarted(...)
onPageFinished(...)
In all these methods, you get the URL and can check which URL is being loaded or finished loading.
According to the URL you can decide whether to show / hide the splash screen.
You can do it without AsyncTask and you can hide the splash screen when the page loaded without a timer.
WebViewClient class has a method onPageFinished() which will be called once the page has been loaded. You can make use of it.
In you project folder, place your splash screen images with name 'splash_screen.png' (or whatever name you want. If you want to use a different name then change this line webView.setBackgroundResource(R.drawable.splash_screen); to map to your splash screen image file) under res/drawable-xxx with corresponding resolutions.
For Eg:
I followed these resolutions for my app's splash screen.
hdpi - 400x800
ldpi - 240x320
mdpi - 320x480
xdpi - 640x960
And try this code:
public class MainActivity extends Activity {
final Activity activity = this;
private WebView webView;
private boolean isSplashOn = false;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = (WebView) findViewById(R.id.webView);
webView.setBackgroundColor(0);
webView.setBackgroundResource(R.drawable.splash_screen);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setLoadsImagesAutomatically(true);
webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
webView.getSettings().setAppCacheEnabled(false);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setPluginsEnabled(true);
webView.getSettings().setPluginState(PluginState.ON);
webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
webView.setScrollbarFadingEnabled(false);
webView.setWebViewClient(new MyWebClient());
webView.loadUrl("http://www.google.com");
}
public class MyWebClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
if(url.startsWith("tel:")) {
Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
startActivity(intent);
return true;
}
return false;
}
#Override
public void onPageFinished(WebView view, String url) {
if(!isSplashOn) {
webView.setBackgroundDrawable(null);
webView.setBackgroundColor(0);
isSplashOn = true;
}
super.onPageFinished(view, url);
}
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK) {
if(webView.canGoBack()) {
webView.goBack();
return true;
}else {
activity.finish();
}
}
return super.onKeyDown(keyCode, event);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<WebView
android:id="#+id/webView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitXY" />
</LinearLayout>
EDIT:
By the way i found the issue with your code. You are trying to set the splash screen to the webview before instantiating it.
Change your code from this:
webview.setBackgroundColor(0);
webview.setBackgroundResource(R.drawable.activity_splash);
webview = (WebView)findViewById(R.id.output);
to this:
webview = (WebView)findViewById(R.id.output);
webview.setBackgroundColor(0);
webview.setBackgroundResource(R.drawable.activity_splash);
Why does the splash has to be an activity?
You can make it a full screen ImageView in the bottom of your layout and hide it using a handler:
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
if (isDestroyed())
return;
mSplashView.setVisibility(View.GONE);
}
},2000);
I'm following (Building Web Apps in WebView) tutorial, but I have problem with (Navigating web page history) I'm runing it on my device and I click on a link but when I click the back button the application shuts down:
That's my activity:
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView myWebView = (WebView) findViewById(R.id.webView1);
myWebView.setWebViewClient(new MyWebViewClient());
myWebView.setWebViewClient(new WebViewClient());
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.loadUrl("http://www.google.com");
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("www.example.com")) {
// This is my web site, so do not override; let my WebView load the page
return false;
}
// Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
WebView myWebView = null;
// Check if the key event was the Back button and if there's history
if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {
myWebView.goBack();
return true;
}
// If it wasn't the Back key or there's no web page history, bubble up to the default
// system behavior (probably exit the activity)
return super.onKeyDown(keyCode, event);
}
}
public class MainActivity extends Activity {
WebView myWebView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myWebView = (WebView) findViewById(R.id.webView1);
myWebView.setWebViewClient(new MyWebViewClient());
myWebView.setWebViewClient(new WebViewClient());
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.loadUrl("http://www.google.com");
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("www.example.com")) {
// This is my web site, so do not override; let my WebView load the page
return false;
}
// Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// Check if the key event was the Back button and if there's history
if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {
myWebView.goBack();
return true;
}
// If it wasn't the Back key or there's no web page history, bubble up to the default
// system behavior (probably exit the activity)
return super.onKeyDown(keyCode, event);
}