// Actually, 4.1 has issues on javascript. If you want to load web view on 4.1 .you can android web view by using this code
public class MainActivity extends AppCompatActivity {
private final String url = "Your Url";
private WebView mWebView;
private CustomTabsClient mClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (AppStatus.getInstance(this).isOnline(this)) {
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion >= Build.VERSION_CODES.LOLLIPOP) {
setContentView(R.layout.noraml);
mWebView = (WebView) findViewById(R.id.webView);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setWebViewClient(new WebViewClient() {
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
//this is for avoiding web page not found
view.loadUrl("file:///android_asset/index.html");
Toast.makeText(getBaseContext(), "Oops!!! Please check your internet connection", Toast.LENGTH_SHORT).show();
}
});
mWebView.loadUrl(url);
} else {
setContentView(R.layout.activity_main);
CustomTabsServiceConnection mConnection = new CustomTabsServiceConnection() {
@Override
public void onCustomTabsServiceConnected(ComponentName componentName, CustomTabsClient customTabsClient) {
mClient = customTabsClient;
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
mClient = null;
}
};
String packageName = "com.android.chrome";
CustomTabsClient.bindCustomTabsService(this, packageName, mConnection);
}
}
else {
setContentView(R.layout.error);
}
}
public void loadCustomTabs(View view) {
CustomTabsIntent.Builder mBuilder = new CustomTabsIntent.Builder(getSession());
mBuilder.setToolbarColor(ContextCompat.getColor(this, R.color.indigo_500));
mBuilder.setCloseButtonIcon(BitmapFactory.decodeResource(getResources(),
R.mipmap.ic_arrow_back_white_24dp));
mBuilder.addMenuItem("Share", setMenuItem());
mBuilder.setStartAnimations(this, R.anim.slide_in_right, R.anim.slide_out_left);
mBuilder.setExitAnimations(this, R.anim.slide_in_left, R.anim.slide_out_right);
CustomTabsIntent mIntent = mBuilder.build();
mIntent.launchUrl(this, Uri.parse(url));
}
private CustomTabsSession getSession() {
return mClient.newSession(new CustomTabsCallback() {
@Override
public void onNavigationEvent(int navigationEvent, Bundle extras) {
super.onNavigationEvent(navigationEvent, extras);
}
});
}
private PendingIntent setMenuItem() {
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject For you website");
shareIntent.putExtra(Intent.EXTRA_TEXT, url);
return PendingIntent.getActivity(this, 0, shareIntent, 0);
}
}
//Custom web view
public class CustomWebView extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_custom_web_view);
class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals(getIntent().getStringExtra("url")))
return false;
else
return super.shouldOverrideUrlLoading(view, url);
}
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
view.loadUrl("file:///android_asset/index.html");
Toast.makeText(getBaseContext(), "Oops!!! Please check your internet connection", Toast.LENGTH_SHORT).show();
}
}
WebView webView = (WebView) findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(getIntent().getStringExtra("url"));
webView.setWebViewClient(new MyWebViewClient());
}
}