WebView detect button click event - android

I am loading a web URL in webview. Like below :-
mWebView = (WebView)findViewById(R.id.id__web_view);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setWebViewClient(new WebViewClient());
mWebView.loadUrl("http://www.google.com");
How can I detect a button click event inside the loaded page. Say for example I want to show a toast "Clicked" when user clicks on "Images" tab after the search is done.(Please note that the detection of button click is one page ahead of the page that I loaded).
What I have tried:-
I did some research on how to do this, but none of these were fruitfull.
Below is one among them :-
WebSettings ws = wv.getSettings();
ws.setJavaScriptEnabled(true);
wv.addJavascriptInterface(new Object()
{
public void performClick()
{
// Deal with a click on the OK button
}
}, "ok");
<button type="button" onclick="ok.performClick();">OK</button>
But this requires adding onclick in the html, I am not sure is it possible to do it on a 3rd party website. Please throw some light into the problem if any one has any clue.

Checkout below code.. its working fine for me
#SuppressLint("SetJavaScriptEnabled")
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.webView1);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setLoadsImagesAutomatically(true);
mWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
mWebView.setWebViewClient(new MyBrowser());
mWebView.loadUrl("file:///android_asset/index.html");
}
private class myWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
view.addJavascriptInterface(new Object()
{
#JavascriptInterface
public void performClick() throws Exception
{
Log.d("LOGIN::", "Clicked");
Toast.makeText(MainActivity.this, "Login clicked", Toast.LENGTH_LONG).show();
}
}, "login");
return true;
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}

Related

I check all the forum but not find a solution | "Open external links of my webview in browser and not in app" does not work

thanks for read my question :) I am not able to solve this problem after check all the forum. I am working on my first webview. Nothing special. It's a webview with progress bar. My code in "shouldOverrideUrlLoading" method is not working. I want that all external links of my site open in browser and not in app, but it does not work. Can anyone please check my code?
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
private WebView wv;
private ProgressBar progressBar;
private FrameLayout frameLayout;
SwipeRefreshLayout swipe;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
frameLayout = (FrameLayout) findViewById(R.id.frameLayout);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
progressBar.setMax(100);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
swipe = (SwipeRefreshLayout) findViewById(R.id.swipe);
swipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
String webUrl = wv.getUrl();
wv.loadUrl(webUrl);
}
});
LoadWeb();
}
public void LoadWeb(){
wv = (WebView)findViewById(R.id.webView);
WebSettings settings = wv.getSettings();
settings.setJavaScriptEnabled(true);
wv.loadUrl("example.com");
swipe.setRefreshing(true);
wv.setWebViewClient(new HelpClient());
wv.setWebViewClient(new MyAppWebViewClient());
wv.setWebChromeClient(new WebChromeClient(){
public void onProgressChanged(WebView view, int progress){
frameLayout.setVisibility(View.VISIBLE);
progressBar.setProgress(progress);
if (progress == 100){
frameLayout.setVisibility(View.GONE);
}
super.onProgressChanged(view, progress);
}
});
progressBar.setProgress(0);
wv.setWebViewClient(new WebViewClient(){
public void onPageFinished(WebView view, String url){
swipe.setRefreshing(false);
}
});
}
private class HelpClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url){
view.loadUrl(url);
frameLayout.setVisibility(View.VISIBLE);
return true;
}
}
private class MyAppWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(Uri.parse(url).getHost().endsWith("example.com")) {
return false;
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
view.getContext().startActivity(intent);
return true;
}
}
#Override
public void onBackPressed() {
if(wv.canGoBack()) {
wv.goBack();
} else {
super.onBackPressed();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
return super.onOptionsItemSelected(item);
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_home) {
wv.loadUrl("example.com");
} else if (id == R.id.nav_facebook) {
try {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("fb://page/idfbpage"));
startActivity(intent);
} catch (Exception e) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.facebook.com/idfbpage"));
startActivity(intent);
}
} else if (id == R.id.nav_instagram) {
Uri uri = Uri.parse("http://instagram.com/_u/idinstapage");
Intent likeIng = new Intent(Intent.ACTION_VIEW, uri);
likeIng.setPackage("com.instagram.android");
try {
startActivity(likeIng);
} catch (Exception e) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://instagram.com/idinstapage")));
}
} else if (id == R.id.nav_star) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("market://details?id=idpackage"));
startActivity(intent);
} else if (id == R.id.nav_informazioni) {
wv.loadUrl("example.com/info-app");
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
In your method LoadWeb() there is an error because your WebView should have only one WebViewClient and only one WebChromeClient. So what happens is, if you use the method wv.setWebViewClient(new YourWebViewClient()); more than once what happens is that all other methods set before that are ignored because you can set it only once.For your case:
You set the WebViewClient Three (3) times:
wv.setWebViewClient(new HelpClient()); //First time
wv.setWebViewClient(new MyAppWebViewClient()); //Second time
And the last time you set it down in your same method you set another like:
wv.setWebViewClient(new WebViewClient(){
public void onPageFinished(WebView view, String url){
swipe.setRefreshing(false);
}
});
So what happens is that the two previous methods were ignored and the only method that was working was the last one (the third) only. That's why I asked you to put the second method down to setWebClient((new MyAppWebViewClient()); so as to ignore the previous!
THE SOLUTION
Now you know we have to set only one WebViewClient for your webView we have to make only ONE Class that will be used to and do the job of all three other WebViewClient by overriding all the required methods. Make this class inside your activity but oustide your any method (just like how you have done when making in HelpClient() Class). Here is Our Class Please make sure you read the Comments in it!
private class NewWebViewClient extends WebViewClient {
//Now we have only one class to do a job of all the other three!
#Override
public void onPageFinished(WebView view, String url) {
/*I here do whatever you want to do when the page has finished loading! Do it here inside this method and it will work may be control the framelayout visibility For example lets setRefreshing false to our swipe()*/
swipe.setRefreshing(false);
//.....add more methods you want here when the page loading is finished
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
/*In this method we are going to tell the android system what kind of link we want and what kind of link we dont want for that what we want we will handle for the extra the browser should do so!*/
if (Uri.parse(url).getHost().endsWith("example.com")) {
//if the code gets here this is your link then right? Lets do whatever we want with our link and logic for example lets set framelayout visible or doing anything this link's host is example.com!
frameLayout.setVisibility(View.VISIBLE);
return false;
} else {
//Ooops This link is not yours lets just open browsers right
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
view.getContext().startActivity(intent);
return true;
}
}
}
After your class is beautiful then lets just set our WebViewClient only ONCE.
wv.setWebViewClient(new NewWebViewClient());
You can just ignore the WebChromeClient because you set it only once you are good. Comment below if you find any difficulties!
I hope removing the this line
wv.setWebViewClient(new MyAppWebViewClient());
will solve the problem for you.
You are using two instances of WebViewClient ,remove one ,
private class MyAppWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.d("TAG",url);
if(Uri.parse(url).getHost().equals("example.com")) {
return false;
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
view.getContext().startActivity(intent);//add the brakpoint here
Log.d("TAG","Starting activity");
return true;
}
}
just use this

Add custom android webview error page

In my app i want an error activity "NOT A ERROR HTML PAGE". Which contain refresh button and a layout. I have search for it but i found is just a custom html error page from assets folder.
I have created a some code which is below,
i want is that when ever network error occurs it will open an activity like this and user can easily refresh page using button.
I want to make error page like this one..
This is not a html error page. This contains layout with refresh button,
public WebView mWebview ;
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map);
mWebview = new WebView(this);
mWebview.getSettings().setJavaScriptEnabled(true); // enable javascript
mWebview.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
// Do Something Here, I don't know what to do.. :(
}
});
mWebview.loadUrl("google.com");
setContentView(mWebview);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.option, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.refresh:
startActivity(new Intent(this, CeS1.class));
finish();
return true;
case R.id.about:
startActivity(new Intent(this, Credits_activity.class));
finish();
return true;
case R.id.gotomain:
startActivity(new Intent(this, MenuActivity.class));
return true;
default:
return super.onOptionsItemSelected(item);
}
}
call this in your code
webview.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
//place you error code here
}
});
You can create a layout with 2 layers. One is WebView, another is your error layout. When your WebView encounters network error, you can hide your WebView and show errorView with retry button.
first, you put your HTML error page on the asset folder, then
You need override onReceivedError according to the following code,
private class MyWebViewClient extends WebViewClient {
.
.
.
// api<23
#Override
public void onReceivedError(WebView view, int errorCode, String description, String url) {
view.stopLoading();
webview.loadUrl("file:///android_asset/error/error.html");
}
// api> 23
#Override
#TargetApi(android.os.Build.VERSION_CODES.M)
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
view.stopLoading();
webview.loadUrl("file:///android_asset/error/error.html");
// super.onReceivedError(view, request, error);
}
.
.
.
}

Override back button while viewing webview (fragment)

I created a WebView using Fragment; here is my code:
public class MyWebView extends Fragment {
private WebView webView;
#SuppressLint("SetJavaScriptEnabled")
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Bundle args = getArguments();
String url = args.getString("url");
webView = (WebView) getView().findViewById(R.id.webView);
webView.setWebViewClient(new MyBrowser());
WebSettings webSettings = webView.getSettings();
webSettings.setSaveFormData(true);
webSettings.setUseWideViewPort(true);
webSettings.setLoadWithOverviewMode(true);
webSettings.setSupportZoom(true);
webSettings.setBuiltInZoomControls(true);
webSettings.setLoadsImagesAutomatically(true);
webSettings.setJavaScriptEnabled(true);
webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
webView.loadUrl(url);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.web_view, container, false);
view.setOnKeyListener(new OnKeyListener() {
#Override
public boolean onKey( View v, int keyCode, KeyEvent event ) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
if (webView.canGoBack()) {
webView.goBack();
return true;
}
}
return false;
}
});
return view;
}
private class MyBrowser extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
}
I tried to override onBackPressed() but it seems not to work with Fragment. So I used setOnKeyListener() but it doesn’t work.
Can you tell me why the back button closes the app instead of going back to the previous web page?
The Fragment doesn't receive a callback of the OnBackPressed event. You'll have to make your Activity state aware when your fragment is displayed and communicate back and forth between the activity and the fragment.
You could make your code you're calling in on key a public boolean function for instance and if the fragment is present you interact with it, you continue to return true or false in a similar way which allows you to know on the activity if you should continue with it naturally if there are no more pages to go back.

Button back in my Webview cause output

Very good at all.
I have a fragment is called a drawerlist. Within this fragment I have a webView showing a web page.
I can surf the web, but the problem comes when I want to return to the previous page, and pulse on the back button.
The result I get is that pressing the back button, the application closes. And what I want is to return to the previous page.
Code fragment is as follows:
public class BClasesWebViewFragment extends Fragment {
private WebView webView;
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
WebSettings webSettings = this.webView.getSettings ();
webSettings . setJavaScriptEnabled ( true );
webSettings . setDomStorageEnabled ( true );
this . webView . setWebViewClient ( new WebViewClient ());
this . webView . setWebChromeClient ( new WebChromeClient ());
webSettings . setLoadWithOverviewMode ( true );
webSettings . setUseWideViewPort ( true );
webView.setInitialScale(1);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
webView.setScrollbarFadingEnabled(false);
webView.getSettings().setBuiltInZoomControls(true);
webView.loadUrl("http://google.com/");
webView.setDownloadListener(new DownloadListener() {
public void onDownloadStart(String url, String userAgent,
String contentDisposition, String mimetype,
long contentLength) {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
});
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_bclases_webview, container, false);
webView = (WebView)view.findViewById(R.id.webViewBClases);
return view;
}
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.actionbar_menu, menu);
super.onCreateOptionsMenu(menu,inflater);
}
}
Thank you very much everyone.
I learned a lot on this site
Try this
webView.setOnKeyListener(new OnKeyListener()
{
#Override
public boolean onKey(View v, int keyCode, KeyEvent event)
{
if(event.getAction() == KeyEvent.ACTION_DOWN)
{
WebView webView = (WebView) v;
switch(keyCode)
{
case KeyEvent.KEYCODE_BACK:
if(webView.canGoBack())
{
webView.goBack();
return true;
}
break;
}
}
return false;
}
});
this will work perfectly.

Is it possible to play a Youtube video using WebView in Android

I've gone though many tutorials found on Google and none of the answers really work. I would like the video to play in the WebView instead of going though the YouTube app. Any help would be awesome. Thanks.
Code:
public class Youtube extends Activity {
WebView myWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.website);
myWebView = (WebView) findViewById(R.id.webView);
myWebView.loadUrl("http://www.youtube.com");
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.addJavascriptInterface(new WebAppInterface(this), "Android");
myWebView.setWebViewClient(new WebViewClient());
myWebView.getSettings().setBuiltInZoomControls(true);
myWebView.getSettings().setSupportZoom(true);
myWebView.getSettings().setUseWideViewPort(true);
myWebView.getSettings().setLoadWithOverviewMode(true);
getActionBar().setDisplayHomeAsUpEnabled(true);
}
public class WebAppInterface {
Context mContext;
/** Instantiate the interface and set the context */
WebAppInterface(Context c) {
mContext = c;
}
/** Show a toast from the web page */
#JavascriptInterface
public void showToast(String toast) {
Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
}
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost()
.equals("http://www.youtube.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);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
Use the new YouTube Android Player API - https://developers.google.com/youtube/android/player
"The API defines methods for loading and playing YouTube videos (and playlists) and for customizing and controlling the video playback experience."
So for using with a Channel, you'd use the regular YoutTube API to get the metadata, and play the videos/playlists with the Player API.
Kindly try working around this code after defining the webView in your layout's xml file:
In the MainActivity, get the instances of WebView controls and convert the iFrame code of the embed YouTube video to a string. Then enable Javascript and load the video iFrame string to the webview instance by using the loaddata() of the WebView.
public class MainActivity extends ActionBarActivity {
private MyWebChromeClient mWebChromeClient = null;
private View mCustomView;
private RelativeLayout mContentView;
private FrameLayout mCustomViewContainer;
private WebChromeClient.CustomViewCallback mCustomViewCallback;
private WebView myWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myWebView = (WebView) findViewById(R.id.webView);
mWebChromeClient = new MyWebChromeClient();
myWebView.setWebChromeClient(mWebChromeClient);
myWebView.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}
});
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.loadUrl("https://www.youtube.com/watch?v=7bDLIV96LD4");
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public class MyWebChromeClient extends WebChromeClient {
FrameLayout.LayoutParams LayoutParameters = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
#Override
public void onShowCustomView(View view, CustomViewCallback callback) {
// if a view already exists then immediately terminate the new one
if (mCustomView != null) {
callback.onCustomViewHidden();
return;
}
mContentView = (RelativeLayout) findViewById(R.id.activity_main);
mContentView.setVisibility(View.GONE);
mCustomViewContainer = new FrameLayout(MainActivity.this);
mCustomViewContainer.setLayoutParams(LayoutParameters);
mCustomViewContainer.setBackgroundResource(android.R.color.black);
view.setLayoutParams(LayoutParameters);
mCustomViewContainer.addView(view);
mCustomView = view;
mCustomViewCallback = callback;
mCustomViewContainer.setVisibility(View.VISIBLE);
setContentView(mCustomViewContainer);
}
#Override
public void onHideCustomView() {
if (mCustomView == null) {
return;
} else {
// Hide the custom view.
mCustomView.setVisibility(View.GONE);
// Remove the custom view from its container.
mCustomViewContainer.removeView(mCustomView);
mCustomView = null;
mCustomViewContainer.setVisibility(View.GONE);
mCustomViewCallback.onCustomViewHidden();
// Show the content view.
mContentView.setVisibility(View.VISIBLE);
setContentView(mContentView);
}
}
}
#Override
public void onBackPressed() {
if (mCustomViewContainer != null)
mWebChromeClient.onHideCustomView();
else if (myWebView.canGoBack())
myWebView.goBack();
else
super.onBackPressed();
}
}
The entire tutorial is found here: http://inducesmile.com/android/how-to-play-youtube-video-inside-android-webview-using-video-url/

Categories

Resources