How to add progress bar line in webview - java

I am trying to add a progress bar or loading bar to my application which uses webview. I have to application set up to run all the links within the app but i am confused on how to implement a progress bar for whenever a link is clicked on
this is my code
public class MainActivity extends ActionBarActivity {
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);
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mWebView.loadUrl("http://caknowledge.in");
mWebView.setWebViewClient(new MyAppWebViewClient());
}
#Override
public void onBackPressed() {
if(mWebView.canGoBack()) {
mWebView.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.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);
}
private class MyAppWebViewClient extends WebViewClient {
}
And XML.JAVA Code is as below
<WebView android:id="#+id/activity_main_webview" android:layout_width="wrap_content"
android:layout_height="wrap_content" />

just use progressBar above your webview and when your web view start loading page make it visiible and when your webview page loaded hide the progressbar

you can add below code on your webviewclient to show progress dialog
private class MyAppWebViewClient extends WebViewClient {
#Override
public void onPageStarted(WebView p_view, String p_url, Bitmap p_favIcon)
{
try {
if (progressDialog != null && !isFinishing())
progressDialog.show();
} catch (Throwable ex) {
//ignore
}
super.onPageStarted(p_view, p_url, p_favIcon);
}
#Override
public void onPageFinished(WebView p_view, String p_url)
{
try {
if (progressDialog != null && progressDialog.isShowing()) {
progressDialog.dismiss();
}
} catch (Throwable ex) {
//ignore
}
super.onPageFinished(p_view, p_url);
}
}
Progress bar declaration in activity
progressDialog = new ProgressDialog(this);
progressDialog.setMessage("Loading..");
Hope it works for you !!

layout code
<ProgressBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/progress_bar"
style="?android:attr/progressBarStyleHorizontal"/>
<WebView
android:layout_below="#id/progress_bar"
android:id="#+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
java code
webView.setWebViewClient(new HelloWebViewClient());
progressBar.setVisibility(View.INVISIBLE);
private class HelloWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon)
{
// TODO show you progress image
progressBar.setVisibility(View.VISIBLE);
super.onPageStarted(view, url, favicon);
}
#Override
public void onPageFinished(WebView view, String url)
{ progressBar.setVisibility(View.INVISIBLE);
// TODO hide your progress image
super.onPageFinished(view, url);
}
}

Related

How to create a class or method for multiple instance of webview

I have 3 fragments with their 3 layouts in tablayout. All three layout have their individual webview, swipeRefreshLayout and a progress bar. Currently I copy paste the same code in all three fragments . I want to make a class or a single method such that the changes i make in it will reflect on all three webview setting in the fragment
Below is my fragment, I just copy and paste the code below in all three fragments & only change the url.
public class Yahoo extends Fragment {
final String url = "http://www.yahoo.com/";
private WebView webView;
private ProgressBar progressBar1;
private SwipeRefreshLayout mSwipeRefreshLayout1;
public Yahoo() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_tab13, container, false);
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
progressBar1 = (ProgressBar) view.findViewById(R.id.progressBar1);
webView = (WebView) view.findViewById(R.id.website_detail_1);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
progressBar1.setProgress(progress);
if (progress == 100) {
progressBar1.setVisibility(View.GONE);
if (mSwipeRefreshLayout1.isRefreshing()) {
mSwipeRefreshLayout1.setRefreshing(false);
}
} else {
progressBar1.setVisibility(View.VISIBLE);
}
}
});
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
webView.loadUrl(url);
mSwipeRefreshLayout1 = (SwipeRefreshLayout) view.findViewById(R.id.swipe1);
mSwipeRefreshLayout1.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
webView.loadUrl(url);
}
});
webView.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
webView.goBack();
return true;
}
return false;
}
});
}
#Override
public void onDestroy() {
super.onDestroy();
// Log.d("Yahoo webview", "destroy");
if (webView != null) {
webView.removeAllViews();
webView.destroy();
}
webView = null;
}
}
Could you help me out as how to create a class and and handle objects like webview , progress bar & swipeRefreshLayout such that I dont have to duplicate the same code in all three fragments ?
One thing I can suggest is that you create a single Fragment class, then create 3 instances of it using different urls.
So you will have one fragment class and one layout for it. And then you just instantiate them with different urls (if url is the only difference between them)

Link inside webpage always return "404 not found" error if opened from webview ~Android ~Java

I have a webview that opens up a webpage that contains a some links. However, if I click on each link, it will show "404 Not Found" error. I have done the shouldOverrideUrlLoading inside WebViewClient, but it results the same.
Please help. Thanks in advance.
this is how I set my webview:
WebView web_view = (WebView)view.findViewById(R.id.web_view);
WebSettings webSettings = web_view.getSettings();
web_view.setWebChromeClient(new WebChromeClient(){
#Override
public void onProgressChanged(WebView view, int newProgress) {
progressBar.setProgress(newProgress);
super.onProgressChanged(view, newProgress);
}
});
web_view.setWebViewClient(new WebViewClient(){
#Override
public void onPageFinished(WebView view, String url) {
progressBar.setVisibility(View.GONE);
super.onPageFinished(view, url);
}
#Override
public void shouldOverrideUrlLoading(Webview view, String url){
progressBar.setProgress(0);
progressBar.setVisibility(View.VISIBLE);
return super.shouldOverrideUrlLoading(view,url);
}
})
webSettings.setJavaScriptEnabled(true);
web_view.loadUrl("http://someurl.com");
I have also done this:
#Override
public void shouldOverrideUrlLoading(Webview view, String url){
progressBar.setProgress(0);
progressBar.setVisibility(View.VISIBLE);
view.loadUrl(url);
return true;
}
but both of them result in '404 not found'.
Any help please..
Prepare the layout to show when an error occurred instead of Web Page (a dirty 'page not found message') The layout has one button, "RELOAD" with some guide messages.
If an error occurred, Remember using boolean and show the layout we prepare.
If user click "RELOAD" button, set mbErrorOccured to false. And Set mbReloadPressed to true.
if mbErrorOccured is false and mbReloadPressed is true, it means webview loaded page successfully. 'Cause if error occurred again, mbErrorOccured will be set true on onReceivedError(...)
Here is my full source. Check this out.
public class MyWebViewActivity extends ActionBarActivity implements OnClickListener {
private final String TAG = MyWebViewActivity.class.getSimpleName();
private WebView mWebView = null;
private final String URL = "http://www.google.com";
private LinearLayout mlLayoutRequestError = null;
private Handler mhErrorLayoutHide = null;
private boolean mbErrorOccured = false;
private boolean mbReloadPressed = false;
#SuppressLint("SetJavaScriptEnabled")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_webview);
((Button) findViewById(R.id.btnRetry)).setOnClickListener(this);
mlLayoutRequestError = (LinearLayout) findViewById(R.id.lLayoutRequestError);
mhErrorLayoutHide = getErrorLayoutHideHandler();
mWebView = (WebView) findViewById(R.id.webviewMain);
mWebView.setWebViewClient(new MyWebViewClient());
WebSettings settings = mWebView.getSettings();
settings.setJavaScriptEnabled(true);
mWebView.setWebChromeClient(getChromeClient());
mWebView.loadUrl(URL);
}
#Override
public boolean onSupportNavigateUp() {
return super.onSupportNavigateUp();
}
#Override
public void onClick(View v) {
int id = v.getId();
if (id == R.id.btnRetry) {
if (!mbErrorOccured) {
return;
}
mbReloadPressed = true;
mWebView.reload();
mbErrorOccured = false;
}
}
#Override
public void onBackPressed() {
if (mWebView.canGoBack()) {
mWebView.goBack();
return;
}
else {
finish();
}
super.onBackPressed();
}
class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return super.shouldOverrideUrlLoading(view, url);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
#Override
public void onLoadResource(WebView view, String url) {
super.onLoadResource(view, url);
}
#Override
public void onPageFinished(WebView view, String url) {
if (mbErrorOccured == false && mbReloadPressed) {
hideErrorLayout();
mbReloadPressed = false;
}
super.onPageFinished(view, url);
}
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
mbErrorOccured = true;
showErrorLayout();
super.onReceivedError(view, errorCode, description, failingUrl);
}
}
private WebChromeClient getChromeClient() {
final ProgressDialog progressDialog = new ProgressDialog(MyWebViewActivity.this);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setCancelable(false);
return new WebChromeClient() {
#Override
public void onProgressChanged(WebView view, int newProgress) {
super.onProgressChanged(view, newProgress);
}
};
}
private void showErrorLayout() {
mlLayoutRequestError.setVisibility(View.VISIBLE);
}
private void hideErrorLayout() {
mhErrorLayoutHide.sendEmptyMessageDelayed(10000, 200);
}
private Handler getErrorLayoutHideHandler() {
return new Handler() {
#Override
public void handleMessage(Message msg) {
mlLayoutRequestError.setVisibility(View.GONE);
super.handleMessage(msg);
}
};
}
}
try this... this is the code from AS default webview activity, generated by AS.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.google);
initInstances();
WebView myWebView = (WebView) findViewById(R.id.webView1);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.loadUrl("http://www.google.com");
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.setWebViewClient(new WebViewClient());
}
sidenote :-->> iinitInstances() is referring to nagivation drawer on the left, could be ignored.

onBackPressed() crashes my webview app

I am new to android development, I have tried to override onBackPress() to implement webView.GoBack(). But on pressing back key my apps getting crashed. Here is my MainActivity.java code. Am I doing something wrong ??
public class MainActivity extends AppCompatActivity {
private WebView webview;
/** Called when the activity is first created. */
public void onBackPressed (){
if (webview.isFocused() && webview.canGoBack()) {
webview.goBack();
}
else {
super.onBackPressed();
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView view = (WebView) findViewById(R.id.webView);
view.getSettings().setJavaScriptEnabled(true);
view.setWebViewClient(new MyCustomWebViewClient());
view.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
view.loadUrl("http://url");
}
private class MyCustomWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
//hide loading image
findViewById(R.id.imageLoading1).setVisibility(View.GONE);
//show webview
findViewById(R.id.webView).setVisibility(View.VISIBLE);
}
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
if (view.canGoBack()) {
view.goBack();
} else {
view.loadUrl("file:///android_asset/index.html");
}
Toast.makeText(getBaseContext(), description, Toast.LENGTH_LONG).show();
}
}
}
Just because your webView is null. You are not referencing it anywhere. Modified your code.
public class MainActivity extends AppCompatActivity {
private WebView view;
/** Called when the activity is first created. */
public void onBackPressed (){
if (view.isFocused() && view.canGoBack()) {
view.goBack();
}
else {
super.onBackPressed();
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
view = (WebView) findViewById(R.id.webView);
view.getSettings().setJavaScriptEnabled(true);
view.setWebViewClient(new MyCustomWebViewClient());
view.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
view.loadUrl("http://url");
}
private class MyCustomWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
//hide loading image
findViewById(R.id.imageLoading1).setVisibility(View.GONE);
//show webview
findViewById(R.id.webView).setVisibility(View.VISIBLE);
}
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
if (view.canGoBack()) {
view.goBack();
} else {
view.loadUrl("file:///android_asset/index.html");
}
Toast.makeText(getBaseContext(), description, Toast.LENGTH_LONG).show();
}
}
}
In onCreate you have:
WebView view = (WebView) findViewById(R.id.webView);
The WebView you have defined at the top has not been initialized, and is the one being used by the onBackPressed method, to fix this all you need to do is remove WebView before initializing the variable in onCreate:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
view = (WebView) findViewById(R.id.webView);
view.getSettings().setJavaScriptEnabled(true);
view.setWebViewClient(new MyCustomWebViewClient());
view.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
view.loadUrl("http://url");
}
Actually you can not do directly inside the fragment. The onBackPressed can be overridden in the FragmentActivity. What you can do is:
1.Override the onBackPressed inside the activity.
2.When the onBackPressed is called, check if the instance of the current fragment is the instance showing the webview.
3.If it is, ask the fragment if the webview can go back.
4.If it is not, call the super or whatever you need
#Override
public void onBackPressed() {
Fragment webview = getSupportFragmentManager().findFragmentByTag("webview");
if (webview instanceof MyWebViewFragment) {
boolean goback = ((MyWebViewFragment)webview).canGoBack();
if (!goback)
super.onBackPressed();
}
}

Android WebView: A particular site doesn't render properly.

I am working with a WebView which is supposed to open any website.
I have 2 activities-CallingActivity and UrlBrowser. The webview is in UrlBrowser.
The CallingActivity has an EditText which takes the URL from a user and supplies it to the UrlBrowser activity via a bundle object passed as an extra with an Intent object. The UrlBrowser activity uses this URL to load into a webview.
I have to update a progress bar with the loading progress also for which I've used the onProgressChanged method to update values .
The problem I'm facing is that a particular website: www.flipkart.com is not rendering properly. A blank light brownish patch is all that is visible in the webview.
I tried changing the user agent of the WebView. On putting the user agent string of a desktop chrome browser all websites including flipkart opened perfectly, however, their desktop versions opened. I need the mobile version. As soon as I revert to using the user agent of a mobile browser, the same problem occurs. This led me to believe that maybe something is wrong with the user agents so I faked the user agent to chrome's user agent and the problem still persisted.
Here is the code for the two activities and other relevant files:
UrlBrowser.Java
public class UrlBrowser extends AppCompatActivity
{
WebView webView;
ProgressBar progressBar;
String finalUrl;
Context appcontext;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_url_browser);
webView=(WebView)findViewById(R.id.webView);
progressBar=(ProgressBar)findViewById(R.id.progressBar);
progressBar.setMax(100);
Bundle b= getIntent().getExtras();
String finalUrl="http://"+b.getString("url");
appcontext=getApplicationContext();
viewPage(finalUrl);
}
public class MyWebViewClient extends WebViewClient
{
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
Toast.makeText(appcontext,"Waiting for "+url ,Toast.LENGTH_SHORT).show();
view.loadUrl(url);
return true;
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon)
{
progressBar.setVisibility(view.VISIBLE);
}
#Override
public void onPageFinished(WebView view, String url)
{
progressBar.setVisibility(view.INVISIBLE);
}
}
public void viewPage(String url)
{
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setAppCacheEnabled(true);
webView.ge webView.getSettings().setAppCachePath("/data/data"+getPackageName()+"/cache");
webView.getSettings().setAllowFileAccess(true);
webView.setWebViewClient(new MyWebViewClient());
webView.setWebChromeClient(new WebChromeClient() {
#Override
public void onProgressChanged(WebView view, int newProgress) {
progressBar.setProgress(newProgress);
super.onProgressChanged(view, newProgress);
}
});
webView.loadUrl(url);
}
#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_url_browser, 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);
}
}
CallingActivity.java
public class CallingActivity extends AppCompatActivity{
Button goButton;
EditText urlField;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calling);
goButton=(Button)findViewById(R.id.goButton);
urlField=(EditText)findViewById(R.id.urlField);
}
public void onClick(View v)
{
String url=urlField.getText().toString();
Intent i=new Intent(this,UrlBrowser.class);
Bundle b=new Bundle();
b.putString("url",url);
i.putExtras(b);
startActivity(i);
}
#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_calling, 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);
}
}
How can I solve this? Any help appreciated

Android, WebView not playing YouTube videos [duplicate]

YouTube Video not playing in WebView.
It my first app ,I Want to do a webview.
When I open YouTube the video not playing, is loading but not playing.
Is loading all the time.
Thank you so much for helping.
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.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl("http://www.google.com");
mWebView.setWebViewClient(new HelloWebViewClient());
}
private class HelloWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView webview, String url)
{
webview.loadUrl(url);
return true;
}}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack())
{
mWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
} }
Xml:
<?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
Seems like duplicate of play youtube video in WebView and YouTube Video not playing in WebView - Android
To make it work via WebView, I used the following two steps (version 4.2.2/Nexus 4):
On shouldOverrideUrlLoading I added webview.setWebChromeClient(new WebChromeClient());
In AndroidManifest.xml for activity tag I added android:hardwareAccelerated="true"
AndroidManifest.xml should also contain Internet permission.
While exact steps with playing video via WebView can be specific to device and Android version, there are also other alternatives to WebView, like VideoView or using Youtube Android Player API.
EDIT1
As for pause and full screen, this is actually a second link I mentioned in the beginning. To make it easier, I am posting code that worked on my Nexus.
In activity_main.xml
<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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<FrameLayout
android:id="#+id/fullscreen_custom_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF000000"/>
<LinearLayout
android:id="#+id/linearlayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<WebView
android:id="#+id/webView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
</RelativeLayout>
In MainActivity class:
public class MainActivity extends Activity {
private WebView mWebView;
private LinearLayout mContentView;
private FrameLayout mCustomViewContainer;
private WebChromeClient.CustomViewCallback mCustomViewCallback;
FrameLayout.LayoutParams COVER_SCREEN_GRAVITY_CENTER = new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.CENTER);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContentView = (LinearLayout) findViewById(R.id.linearlayout);
mWebView = (WebView) findViewById(R.id.webView);
mCustomViewContainer = (FrameLayout) findViewById(R.id.fullscreen_custom_content);
WebSettings webSettings = mWebView.getSettings();
webSettings.setPluginState(WebSettings.PluginState.ON);
webSettings.setJavaScriptEnabled(true);
webSettings.setUseWideViewPort(true);
webSettings.setLoadWithOverviewMode(true);
mWebView.loadUrl("http://www.google.com");
mWebView.setWebViewClient(new HelloWebViewClient());
}
#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;
}
private class HelloWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView webview, String url)
{
webview.setWebChromeClient(new WebChromeClient() {
private View mCustomView;
#Override
public void onShowCustomView(View view, WebChromeClient.CustomViewCallback callback)
{
// if a view already exists then immediately terminate the new one
if (mCustomView != null)
{
callback.onCustomViewHidden();
return;
}
// Add the custom view to its container.
mCustomViewContainer.addView(view, COVER_SCREEN_GRAVITY_CENTER);
mCustomView = view;
mCustomViewCallback = callback;
// hide main browser view
mContentView.setVisibility(View.GONE);
// Finally show the custom view container.
mCustomViewContainer.setVisibility(View.VISIBLE);
mCustomViewContainer.bringToFront();
}
});
webview.loadUrl(url);
return true;
}
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack())
{
mWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
EDIT2 - adding back button support
public class MainActivity extends Activity {
private WebView mWebView;
private LinearLayout mContentView;
private FrameLayout mCustomViewContainer;
private View mCustomView;
private WebChromeClient.CustomViewCallback mCustomViewCallback;
FrameLayout.LayoutParams COVER_SCREEN_GRAVITY_CENTER = new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.CENTER);
private WebChromeClient mWebChromeClient;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContentView = (LinearLayout) findViewById(R.id.linearlayout);
mWebView = (WebView) findViewById(R.id.webView);
mCustomViewContainer = (FrameLayout) findViewById(R.id.fullscreen_custom_content);
mWebChromeClient = new WebChromeClient() {
#Override
public void onShowCustomView(View view, WebChromeClient.CustomViewCallback callback)
{
// if a view already exists then immediately terminate the new one
if (mCustomView != null)
{
callback.onCustomViewHidden();
return;
}
// Add the custom view to its container.
mCustomViewContainer.addView(view, COVER_SCREEN_GRAVITY_CENTER);
mCustomView = view;
mCustomViewCallback = callback;
// hide main browser view
mContentView.setVisibility(View.GONE);
// Finally show the custom view container.
mCustomViewContainer.setVisibility(View.VISIBLE);
mCustomViewContainer.bringToFront();
}
#Override
public void onHideCustomView()
{
if (mCustomView == null)
return;
// 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);
}
};
WebSettings webSettings = mWebView.getSettings();
webSettings.setPluginState(WebSettings.PluginState.ON);
webSettings.setJavaScriptEnabled(true);
webSettings.setUseWideViewPort(true);
webSettings.setLoadWithOverviewMode(true);
mWebView.loadUrl("http://www.google.com");
mWebView.setWebViewClient(new HelloWebViewClient());
}
#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;
}
private class HelloWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView webview, String url)
{
webview.setWebChromeClient(mWebChromeClient);
webview.loadUrl(url);
return true;
}
}
#Override
protected void onStop() {
super.onStop();
if (mCustomView != null)
{
if (mCustomViewCallback != null)
mCustomViewCallback.onCustomViewHidden();
mCustomView = null;
}
}
#Override
public void onBackPressed() {
super.onBackPressed();
if (mCustomView != null)
{
mWebChromeClient.onHideCustomView();
} else
{
finish();
}
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack())
{
mWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
Adding webchrome client solves the issue,
web = (WebView) vi.findViewById(R.id.offer_webView1);
web.loadUrl(getResources().getString(R.string.videolink));
web.getSettings().setJavaScriptEnabled(true);
// web.getSettings().setDomStorageEnabled(true);
web.getSettings().setAllowContentAccess(true);
WebSettings webSettings = web.getSettings();
webSettings.setPluginState(WebSettings.PluginState.ON);
webSettings.setUseWideViewPort(true);
webSettings.setLoadWithOverviewMode(true);
web.canGoBack();
web.setWebChromeClient(new WebChromeClient() {});

Categories