I am using Navigation Drawer with TabLayout. I have a fragment in which is WebView but the webview doesn't take in full height when running. I tried changing webview / relative layout height but didn't work . In the designer the height fits perfectly with the device on screen navigation buttons.
My xml code
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="#+id/website_detail_1"
android:layout_width="match_parent"
android:layout_height="match_parent">
</WebView>
<ProgressBar
android:id="#+id/progressBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:indeterminate="false"/>
</RelativeLayout>
My Java Code
public class Tab1Fragment extends Fragment {
public WebView webView;
public ProgressBar progressBar;
public Tab1Fragment() {
// 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_tab1, container, false);
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
//
progressBar = (ProgressBar) view.findViewById(R.id.progressBar1);
// String url = "http://www.carsaleindiaofficial.com/?m=1";
webView = (WebView) view.findViewById(R.id.website_detail_1);
webView.setWebViewClient(new MyAppWebViewClient());
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setDomStorageEnabled(true);
//view.loadUrl(url);
webView.loadUrl("https://www.yahoo.com");
// view.loadUrl("file:///android_asset/web.html");
}
public class MyAppWebViewClient extends WebViewClient {
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
//view.findViewById(R.id.progressBar1).setVisibility(View.GONE);
Log.i("pageFinished", "yesss");
progressBar.setVisibility(View.INVISIBLE);
//progressBar.setVisibility(View.GONE);
}
#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;
}
}
}
Result
The issue lay with viewpager height.
I believe you need to set the progress bar to VISIBILITY GONE.
INVISIBLE still reserves the space.
Try
<activity
android:name="YourActivity"
android:configChanges="keyboardHidden|orientation|screenSize"
android:screenOrientation="portrait">
</activity>
Related
progress bar will not stop and will load indefinitely.
Referred Page:
progress bar in android webview
fragment code
public class testFragment extends Fragment {
private WebView mWebView;
private WebSettings mWebSettings;
private ProgressBar progressBar;
public testFragment() {
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#SuppressLint("SetJavaScriptEnabled")
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.testfragment, container, false);
mWebView = (WebView) v.findViewById(R.id.webView);
progressBar = (ProgressBar) v.findViewById(R.id.pro);
//progressBar.setVisibility(View.GONE);
mWebView.setWebViewClient(new WebViewClient() {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
progressBar.setVisibility(View.VISIBLE);
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
progressBar.setVisibility(View.GONE);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
mWebView.setWebViewClient(new WebViewClient());
mWebSettings = mWebView.getSettings();
mWebSettings.setJavaScriptEnabled(true);
mWebSettings.setSupportMultipleWindows(false);
mWebSettings.setJavaScriptCanOpenWindowsAutomatically(false);
mWebSettings.setLoadWithOverviewMode(true);
mWebSettings.setUseWideViewPort(true);
mWebSettings.setSupportZoom(false);
mWebSettings.setBuiltInZoomControls(true);
mWebSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
mWebSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
mWebSettings.setDomStorageEnabled(true);
mWebView.loadUrl("https://stackoverflow.com/");
return v;
}
}
Removing the annotation from the annotation will completely disappear the progress bar.
I also wanted to use #Override public void onProgressChanged (WebView view, int newProgress) on the page referenced, but it did not appear in the available methods.
The issue is,
you have set 2 web clients and the last statement was overriding the first one. So visible & hide was never executed.
Since in UI, you have set visibility for progress bar as visible .So it is always visible.
Remove mWebView.setWebViewClient(new WebViewClient()); line.
For onProgressChanged()
You have to use WebChromeClient. Since is available in Chrome Client, not on WebViewClient
mWebView.setWebChromeClient(new WebChromeClient() {
#Override
public void onProgressChanged(WebView view, int newProgress) {
super.onProgressChanged(view, newProgress);
}
});
I'm loading one URL in WebView, the URL page contains functionality of capturing image from device camera OR select image from gallery. If I open URL in Browser, everything works fine but, if I open the same URL in WebView then Camera and Gallery functionality do not works. I think I'm missing some WebView setting. Please check my below code and help me to solve my problem.
Below is my WebViewActivity--
public class WebViewActivity extends AppCompatActivity
{
WebView myWebView;
ProgressBar myProgress;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_webview);
findViews();
}
private void findViews()
{
myWebView= (WebView) findViewById(R.id.myWebView);
myProgress= (ProgressBar) findViewById(R.id.myProgress);
myWebView.setWebViewClient(new myWebClient());
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.getSettings().setLoadWithOverviewMode(true);
//Other webview settings
myWebView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
myWebView.setScrollbarFadingEnabled(false);
myWebView.getSettings().setBuiltInZoomControls(true);
myWebView.getSettings().setPluginState(WebSettings.PluginState.ON);
myWebView.getSettings().setAllowFileAccess(true);
myWebView.getSettings().setSupportZoom(true);
myWebView.loadUrl(SafeNestConstants.MEDLIFE_URL);
}
public class myWebClient extends WebViewClient
{
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon)
{
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
}
#RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request)
{
myProgress.setVisibility(View.VISIBLE);
view.loadUrl(request.getUrl().toString());
return true;
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
// TODO Auto-generated method stub
myProgress.setVisibility(View.VISIBLE);
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url)
{
// TODO Auto-generated method stub
super.onPageFinished(view, url);
myProgress.setVisibility(View.GONE);
}
}
}
Below is my activity_my_webview.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_terms_and_conditions"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.mypackage.WebViewActivity">
<WebView
android:id="#+id/myWebView"
android:layout_width="match_parent"
android:layout_height="match_parent">
</WebView>
<ProgressBar
android:id="#+id/myProgress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:indeterminateDrawable="#drawable/progress_medium"
android:visibility="visible" />
</RelativeLayout>
Also let me know if I can provide more information for the same. Thank you.
i'm finishing my app, is a simple app that contains some WebView Fragments and a Navigation Drawer, everything is OK, but I don't know how to add a progress bar while the WebView is loading a page, all I got is a blank screen before the page loads.
Here is my code...
HomeFragment.java
public class HomeFragment extends Fragment {
private WebView webView;
public HomeFragment() {
// Required empty public constructor
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View mainView = (View) inflater.inflate(R.layout.fragment_my_fragment1, container, false);
WebView webView = (WebView)mainView.findViewById(R.id.webview);
webView.setWebViewClient(new MantenerDominioWeb());
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setBuiltInZoomControls(false);
webView.getSettings().setSupportZoom(false);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webView.getSettings().setAllowFileAccess(true);
webView.getSettings().setDomStorageEnabled(true);
webView.canGoBack();
webView.goBack();
webView.loadUrl("http://www.lfcchile.com/");
return mainView;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
#Override
public void onDestroy() {
super.onDestroy();
}
#Override
public void onDetach() {
super.onDetach();
}
}
My WebViewClient class
public class MantenerDominioWeb extends WebViewClient {
private WebView webView;
public ProgressBar progressBarT4;
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().endsWith("lfcchile.com") || (Uri.parse(url).getHost().endsWith("thisisanfield.com")) || (Uri.parse(url).getHost().endsWith("liverpoolecho.co.uk")) || (Uri.parse(url).getHost().endsWith("liverpoolfc.com"))) {
return false;
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
view.getContext().startActivity(intent);
return true;
}
}
And my Frame XML
<FrameLayout 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="com.lfcchile.MyFragment1">
<!-- TODO: Update blank fragment layout -->
<TextView android:layout_width="match_parent" android:layout_height="match_parent"
android:gravity="center"
android:text="#string/homefrag" />
<WebView
android:id="#+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<ProgressBar
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:id="#+id/progressBar1"/>
</FrameLayout>
Any ideas please? I tried to put the progressBar "in front" of the WebView, but when the page is loaded the icon doesn't dissapear...
I believe it is not possible to put a progressbar because each site has a certain size, it would be difficult to measure it before finalizing the download page.
But you can place a pre-loader (loading) while the page is loaded and after the end you remove access for the user.
Use ProgressDialog:
public class HomeFragment extends Fragment {
private ProgressDialog progress;
private WebView webView;
public HomeFragment() {
// Required empty public constructor
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View mainView = (View) inflater.inflate(R.layout.fragment_my_fragment1, container, false);
WebView webView = (WebView)mainView.findViewById(R.id.webview);
webView.setWebViewClient(new MantenerDominioWeb());
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setBuiltInZoomControls(false);
webView.getSettings().setSupportZoom(false);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webView.getSettings().setAllowFileAccess(true);
webView.getSettings().setDomStorageEnabled(true);
webView.canGoBack();
webView.goBack();
webView.loadUrl("http://www.lfcchile.com/");
progress = ProgressDialog.show(this, "Aguarde...",
"Processando sua solicitação.", true);
webView.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
if (progress != null)
progress.dismiss();
}
});
return mainView;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
#Override
public void onDestroy() {
super.onDestroy();
}
#Override
public void onDetach() {
super.onDetach();
}
}
Set your ProgressBar to visibility="gone" to start
In your WebViewClient subclass, override onPageStarted() to set the visibility of the ProgressBar to VISIBLE
In your WebViewClient subclass, override onPageFinished() to set the visibility of the ProgressBar to GONE
Create a WebChromeClient subclass overriding onProgressChanged() to update the progress value of the ProgressBar
Add the WebChromeClient subclass to the WebView by calling WebView.setWebChromeClient()
I loaded the embedded youtube url into webview.But if i press play button its not perfoming any action but if i press somewhere other than play button the youtube video is getting played.Please some one help me.Thanks in advance.
HomeFragment.class:
public class HomeFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater,
#Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
// TODO Auto-generated method stub
View rootView = inflater.inflate(R.layout.activity_main1, container,
false);
WebView webview=(WebView)rootView.findViewById(R.id.webView1);
webview.loadUrl("https://www.youtube.com/embed/DXLnBMZM09g");
webview.getSettings().setJavaScriptEnabled(true);
webview.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onPageStarted(WebView view, String url,
Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
}
});
return rootView;
}
}
activity_main1:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:hardwareAccelerated="true" >
<WebView
android:id="#+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:layout_weight="1" />
I had the same problem few days ago, but now, video is playing as expected when play button is pressed. I guess it was a bug on Youtube's side...
I wanna load webpage inside fragment, but there is some problem.
1.without overriding shouldOverrideUrlLoading method, the website is open with default browser Chrome.
2.when overriding shouldOverrideUrlLoading, nothing happens. just new blank activity is created.
This is my onCreateView method inside Fragment class
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View resultView = inflater.inflate(R.layout.container, container, false);
if (getArguments() != null) {
String url = getArguments().getString(KEY_URL);
WebView productDetailView = (WebView)resultView.findViewById(R.id.webView);
productDetailView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return super.shouldOverrideUrlLoading(view, url);
}
});
productDetailView.loadUrl(url);
}
return resultView;
}
targetSdkVersion is 19 and added INTERNET permission and XML file of Fragment is like below.
<FrameLayout 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="com.example.ProductDetailFragment">
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/webView"
android:layout_gravity="center" />
</FrameLayout>
what's the problem?
Try this code
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
webView.loadUrl(url);
and start your activity with FLAG_ACTIVITY_NEW_TASK
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
Intent intent = getIntent();
webView.setSaveEnabled(false);
String receivedName = (String) intent.getSerializableExtra("USERNAME");
Log.d("receivedName ----- ", receivedName);
webView.loadUrl("https://docs.google.com/gview?embedded=true&url=" +
receivedName);
webView.setDownloadListener(new DownloadListener() {
public void onDownloadStart(String url, String userAgent,
String contentDisposition, String mimetype,
long contentLength) {
Webview.this.finish();
}
});
Try this code...
Try this.
final ProgressDialog pd = ProgressDialog.show(ActivityA.this, "", "Loading...", true);
wv = (WebView) findViewById(R.id.webview);
wv.getSettings().setJavaScriptEnabled(true);
wv.loadUrl("<url");
wv.setWebViewClient(new WebViewClient() {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon)
{
pd.show();
}
#Override
public void onPageFinished(WebView view, String url) {
pd.dismiss();
}
});
Have you tried adding this <uses-permission android:name="android.permission.INTERNET" />
into Manifest file ?
https://developer.android.com/reference/android/webkit/WebView.html