Troubles using webview with tawk.to in android - android

I am using tawk.to to insert an online chat in my app, with a simple WebView to load the chat, but when I put the url of tawk.to it does not display something. With any other url it works fine and loads the page, but not the chat. I also tried the page 'tawk.to' and it loads.
public class ChatOnlineActivity extends AppCompatActivity {
WebView chatOnlineWebVIew;
ProgressBar loadingChat;
/*MORE CODE HERE*/
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat_online);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
String url="https://tawk.to/chat/597771645dfc8255d623ede4/default?fbclid=IwAR3QB_DOoEu9ePTSPJbayOcTIaWtXjuJMUn46qLFTRVNnpPZBTZhsOy6lrs";
//String url="https://tawk.to/"; //JUST TEST URLS
loadingChat=(ProgressBar)findViewById(R.id.loadingChatOnline);
chatOnlineWebVIew = (WebView) findViewById(R.id.chatWebView);
WebSettings chatSetting=chatOnlineWebVIew.getSettings();
chatSetting.setJavaScriptEnabled(true);
//chatSetting.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
chatOnlineWebVIew.setWebViewClient(new chatWebClient());
//chatOnlineWebVIew.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE);
chatOnlineWebVIew.loadUrl(url);
/*MORE CODE HERE*/
private class chatWebClient extends WebViewClient{
#Override
public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
loadingChat.setVisibility(View.VISIBLE);
return super.shouldOverrideKeyEvent(view, event);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
return super.shouldOverrideUrlLoading(view, request);
}
#Override
public void onPageFinished(WebView view, String url) {
loadingChat.setVisibility(View.GONE);
chatOnlineWebVIew.setVisibility(View.VISIBLE);
Toast.makeText(getApplicationContext(),url,Toast.LENGTH_LONG).show(); //just massage to test if the urls is correct
super.onPageFinished(view, url);
}
}
}

I was also facing the same issue, my WebView was loading the url but not displaying but now i have fixed my issue by adding these following lines of code
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);
webSettings.setLoadWithOverviewMode(true);
webSettings.setUseWideViewPort(true);
webSettings.setBuiltInZoomControls(true);
webSettings.setDisplayZoomControls(false);
webSettings.setSupportZoom(true);
webSettings.setDefaultTextEncodingName("utf-8");
and my final activity is look like below
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import org.jetbrains.annotations.NotNull;
public class TawkToChatWebView extends AppCompatActivity {
private WebView webView;
Activity activity ;
private ProgressDialog progDailog;
#SuppressLint({"NewApi", "SetJavaScriptEnabled"})
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tawktochat);
if (getSupportActionBar()!=null)
{
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
webView=findViewById(R.id.wv_chat);
activity = this;
progDailog = ProgressDialog.show(activity, "Loading","Please wait...", true);
progDailog.setCancelable(false);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);
webSettings.setLoadWithOverviewMode(true);
webSettings.setUseWideViewPort(true);
webSettings.setBuiltInZoomControls(true);
webSettings.setDisplayZoomControls(false);
webSettings.setSupportZoom(true);
webSettings.setDefaultTextEncodingName("utf-8");
webView.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
progDailog.show();
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, final String url) {
progDailog.dismiss();
Log.e("loading","finished");
}
});
webView.loadUrl("ADD_YOUR_DIRECT_CHAT_LINK_HERE");
}
#Override
public boolean onOptionsItemSelected(#NonNull #NotNull MenuItem item) {
if (item.getItemId()==android.R.id.home)
{
finish();
}
return super.onOptionsItemSelected(item);
}
}

Related

Why page is not loading in webview in android

I ma newbie to android and making a demo on webview,I have a test url which is working fine with my laptop browser and i have set it in my webview,but it is not loading and it says "Webpage not available",Please help me to figure it out,need help,
code
package namo.jims.com.webviewapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends AppCompatActivity {
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 MyBrowser());
webview.loadUrl("http://rchitecture.in/app1/");
webview.getSettings().setLoadWithOverviewMode(true);
webview.getSettings().setUseWideViewPort(true);
webview.getSettings().setJavaScriptEnabled(false);
webview.getSettings().setAllowFileAccess(true);
webview.getSettings().setAllowContentAccess(true);
webview.setScrollbarFadingEnabled(false);
}
private class MyBrowser extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
}
You need to add the Internet Permission
<uses-permission android:name="android.permission.INTERNET"/>
You need to follow my answer here...
Snapchat Url load issue
WebSettings settings = webview.getSettings();
settings.setJavaScriptEnabled(true);
//ws.setJavaScriptEnabled(true);
settings.setJavaScriptCanOpenWindowsAutomatically(true);
webview.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
webview.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if( url.startsWith("http:") || url.startsWith("https:") ) {
return false;
}
// Log.i(TAG, "Processing webview url click..."+url);
// view.loadUrl(url);
return true;
}
public void onPageFinished(WebView view, String url) {
Log.e(TAG, "Finished loading URL: " + url);
}
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
}
});
webview.loadUrl("http://rchitecture.in/app1/");

Making a News reading app in Android Studio (WebView). but when ever i click a video in a selected article it does not play

Is their something i'm doing wrong? Do i have to include some lib?
I've tried adding the Youtube Api but that didn't help.
Edit#1: This is the WebView code
On some instance the Audio Playback works but instead of video it's just a rotating circle animation trying to load the video.
package com.PAKGN.Mizzy;
import android.app.Activity;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
import android.webkit.WebSettings;
private WebView webView1;
private Toolbar toolbar;
private String title;
private String url;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_webview);
// this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
WebView myWebView = (WebView) findViewById(R.id.webView1);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
title = getIntent().getExtras().getString("title");
url = getIntent().getExtras().getString("url");
getSupportActionBar().setTitle(title);
if (savedInstanceState != null) {
((WebView) findViewById(R.id.webView1)).restoreState(savedInstanceState);
} else {
webView1 = (WebView) findViewById(R.id.webView1);
webView1.getSettings().setJavaScriptEnabled(true);
webView1.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
final Activity activity = this;
webView1.setWebViewClient(new WebViewClient()
{
#Override
public boolean shouldOverrideUrlLoading(WebView view,
String url) {
// TODO Auto-generated method stub
view.loadUrl(url);
return true;
}
});
webView1.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
// Activities and WebViews measure progress with different scales.
// The progress meter will automatically disappear when we reach 100%
activity.setProgress(progress * 1000);
}
});
webView1.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show();
}
});
runOnUiThread(new Runnable() {
#Override
public void run() {
// Code for WebView goes here
webView1.loadUrl(url);
}
});
}
}
#Override
protected void onSaveInstanceState(Bundle outState ){
((WebView) findViewById(R.id.webView1)).saveState(outState);
}
#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) && webView1.canGoBack()) {
webView1.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);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
.
You need to enable javaScript.WebView myWebView = (WebView) findViewById(R.id.webview);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
EDIT:
You are setting the WebClient for webview twice.
`webView1.setWebViewClient(new WebViewClient()
{
#Override
public boolean shouldOverrideUrlLoading(WebView view,
String url) {
// TODO Auto-generated method stub
view.loadUrl(url);
return true;
}
});`
You aren't really doing anything here thats different from the original implementation and this code isn't even running because you replace the webview with another webclient so delete this.
` runOnUiThread(new Runnable() {
#Override
public void run() {
// Code for WebView goes here
webView1.loadUrl(url);
}
});
`
Delete this too, just type webview1.loadUrl(url) no need for a runOnUiThread.
` webView1.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
Delete this too, its this line that is causing your issue. `

Android - Add progress bar WebView

I am creating a simple app Android webview, but also following the guides, I can not implement a progress bar to my code.
how can I implement a loading bar that will disappear after the page loads?
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView view = (WebView) this.findViewById(R.id.webView);
view.getSettings().setJavaScriptEnabled(true);
view.setWebViewClient(new MyBrowser());
view.loadUrl("http://www.abcdefcsadfg.org");
}
private class MyBrowser extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
}
You can initialize a Progressbar in onCreate, like below and set its initial visibility to View.GONE.
progress = (ProgressBar) findViewById(R.id.progressBar);
progress.setVisibility(View.GONE);
And your MyBrowser should look like this
private class MyBrowser extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
progress.setVisibility(View.GONE);
super.onPageFinished(view, url);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
progress.setVisibility(View.VISIBLE);
super.onPageStarted(view, url, favicon);
}
}
UPDATE:
Check this link
You should declare progress bar in xml file and refer to it in the main activity so that it appears in your application.
Set its progress in MyBrowser.
Refer this link, it will help you.
http://javatechig.com/android/progressbar-while-loading-webview

Android webview page unavailable

I developed a small android webview app to access an internal (local network) PHP based site.
Here is my code:
package com.CheckInventory;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.WindowManager;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebStorage;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
#SuppressWarnings("unused")
public class CheckInventoryActivity extends Activity {
WebView webview;
String username;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setContentView(R.layout.main);
webview = (WebView) findViewById(R.id.webview);
WebView webView = (WebView) findViewById(R.id.webview);
webView.setBackgroundColor(0);
webView.setBackgroundResource(R.drawable.myimage);
webView.addJavascriptInterface(new JavaScriptInterface(this), "Android");
WebSettings webSettings = webview.getSettings();
webSettings.setLoadWithOverviewMode(true);
webview.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
webSettings.setJavaScriptEnabled(true);
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
webSettings.setDatabasePath("/data/data/"+this.getPackageName()+"/databases/");
webSettings.setDomStorageEnabled(true);
webview.setWebChromeClient(new WebChromeClient());
webview.loadUrl("http://192.168.0.124/android");
webview.setWebViewClient(new HelloWebViewClient());
}
private class HelloWebViewClient extends WebViewClient {
#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) && webview.canGoBack()){
//webview.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
The site has authentication, but I would like to add some authentication between the app and the site (How do I do this? pass a parameter maybe when the url is invoked), secondly and more importantly, where specifically do I put the onReceivedError so the user never sees the url or the page down if they walk away from the building or loose connection.
public void onReceivedError(WebView view, int errorCod,String description, String failingUrl) {
Toast.makeText(Webform.this, "Your Internet Connection May not be active Or " + description , Toast.LENGTH_LONG).show();
}
I saw this explanation in Detecting Webview Error and Show Message but I do not know where to implement it.
Thank you in advance
You need to create class which extends WebViewClient and implements method OnReceivedError
like this
class myWebClient extends WebViewClient {
#Override
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
Toast.makeText(Webform.this, "Your Internet Connection May not be active Or " + description , Toast.LENGTH_LONG).show();
super.onReceivedError(view, errorCode, description, failingUrl);
}
}
and then you need to set new WebViewClient to your WebView
web = (WebView) findViewById(R.id.webview);
web.setWebViewClient(new myWebClient());
Hope this helps
You can add onReceivedError in Your HelloWebViewClient class and handle what you want to handle when you get error.
private class HelloWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error){
//Your code to do
Toast.makeText(getActivity(), "Your Internet Connection May not be active Or " + error , Toast.LENGTH_LONG).show();
}
}

Android webview loading dialog

I'm trying to implement a loading dialog in my webview application but whatever I do it just won't work.
What I want is the following, everytime someone clicks a link in the webview the loading dialog has to popup like this:
(source: wordpress.com)
And when the loading is done it has to disappear. Can anyone show me how to do it in my own source? I've tried a lot of things but there's always an issue so I really don't know how to.
This is my WebviewActivity:
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class WebviewActivity extends MainActivity {
private WebView myWebView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
myWebView = (WebView)findViewById(R.id.webview);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.setWebViewClient(new WebViewClient());
myWebView.requestFocus(View.FOCUS_DOWN);
myWebView.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_UP:
if (!v.hasFocus()) {
v.requestFocus();
}
break;
}
return false;
}
});
}
#Override
public void onResume() {
super.onResume();
if ( isOnline() == true )
myWebView.loadUrl(webLink);
else if ( isOnline() == false )
showNoConnectionDialog();
}
#Override
public void onConfigurationChanged(Configuration newConfig){
super.onConfigurationChanged(newConfig);
}
}
Thank you very much for your help!!
You have to override the WebViewClient like this,
webview.setWebViewClient(new WebViewClient(){
#Override
public void onPageFinished(WebView view, final String url) {
progressDialog.cancel();
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
progressDialog.show();
}
});
webview.loadUrl(TwitterUrl);
This is my final solution:
myWebView.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
progressDialog.show();
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, final String url) {
progressDialog.dismiss();
}
});
This can be possible with WebChromeClient. It does worked for me.
Code snippet :
progressDialog.show(); // Add before page load
webView.setWebChromeClient(new HelpWebChromeClient());
private class HelpWebChromeClient extends WebChromeClient {
#Override
public void onProgressChanged(WebView view, int newProgress) {
if(newProgress == 100)
progressDialog.dismiss();
}
}

Categories

Resources