Issue
i have seen many progress bar code but i was not able to apply it on my app , pls help me make a good progress bar . the progress bar should show until webpage load in webView in fragment . pls make a code according to my code , sorry but im new to android and just started pls help a newbie
fragment code
public class BlankFragment2 extends Fragment {
public Handler h;
TextView txt;
Bundle webViewBundle;
public AdView adView;
public WebView mWebView;
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mWebView.saveState(outState);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mWebView.restoreState(savedInstanceState);
}
public BlankFragment2() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view
= inflater.inflate(R.layout.blank_fragment2, container, false);
txt=(TextView)view.findViewById(R.id.hi);
Runnable r=new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
Log.e("bf", "fd");
h.postDelayed(this, 300);
if (txt.isShown()){
txt.setVisibility(View.INVISIBLE);
}
else{
txt.setVisibility(View.VISIBLE);
}
}
};
mWebView = (WebView) view.findViewById(R.id.hu);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setSupportZoom(true);
mWebView.getSettings().setBuiltInZoomControls(true);
mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
mWebView.getSettings().setDisplayZoomControls(true);
mWebView.setWebViewClient(new WebViewClient());
mWebView.setVerticalScrollBarEnabled(true);
mWebView.getSettings().setAllowFileAccess(true);
mWebView.setHorizontalScrollBarEnabled(true);
this.mWebView.getSettings().setDomStorageEnabled(true);
mWebView.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
mWebView.loadUrl("https://www.brainyquote.com/topics/love");
AdRequest adRequest = new AdRequest.Builder().build();
adView = (AdView) view.findViewById(R.id.ad9View);
adView.loadAd(adRequest);
adView = (AdView) view.findViewById(R.id.ad10View);
adView.loadAd(adRequest);
adView = (AdView) view.findViewById(R.id.ad13View);
adView.loadAd(adRequest);
adView = (AdView) view.findViewById(R.id.ad14View);
adView.loadAd(adRequest);
adView = (AdView) view.findViewById(R.id.ad11View);
adView.loadAd(adRequest);
adView = (AdView) view.findViewById(R.id.ad12View);
adView.loadAd(adRequest);
adView = (AdView) view.findViewById(R.id.earn1);
adView.loadAd(adRequest);
adView = (AdView) view.findViewById(R.id.earn2);
adView.loadAd(adRequest);
adView = (AdView) view.findViewById(R.id.earn3);
adView.loadAd(adRequest);
adView = (AdView) view.findViewById(R.id.earn4);
adView.loadAd(adRequest);
adView = (AdView) view.findViewById(R.id.earn5);
adView.loadAd(adRequest);
mWebView.setOnKeyListener(new View.OnKeyListener(){
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK
&& event.getAction() == MotionEvent.ACTION_UP
&& mWebView.canGoBack())
{
mWebView.goBack();
return true;
}
return false;
}
});
return view;
}
@Override
public void onPause() {
super.onPause();
webViewBundle = new Bundle();
mWebView.saveState(webViewBundle);
}
}
layout code
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
tools:context=".BlankFragment2">
<RelativeLayout android:orientation="horizontal"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/tr" android:layout_width="match_parent" android:layout_height="match_parent">
<WebView android:id="@+id/hu"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<com.google.android.gms.ads.AdView
android:orientation="horizontal"
android:id="@+id/ad9View"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/hi"
android:layout_centerHorizontal="true"
app:adSize="BANNER" app:adUnitId="@string/ad9View" />
<com.google.android.gms.ads.AdView
android:id="@+id/ad10View"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/ad9View"
android:layout_centerHorizontal="true"
android:orientation="horizontal"
app:adSize="BANNER"
app:adUnitId="@string/ad10View" />
<com.google.android.gms.ads.AdView
android:orientation="horizontal"
android:id="@+id/ad11View"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/ad10View"
app:adSize="BANNER"
android:layout_centerHorizontal="true"
app:adUnitId="@string/ad11View" />
<com.google.android.gms.ads.AdView
android:orientation="horizontal"
android:id="@+id/ad12View"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/ad11View"
app:adSize="BANNER"
android:layout_centerHorizontal="true"
app:adUnitId="@string/ad12View" />
<com.google.android.gms.ads.AdView
android:orientation="horizontal"
android:id="@+id/earn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/ad14View"
app:adSize="BANNER"
android:layout_centerHorizontal="true"
app:adUnitId="@string/earn" />
<com.google.android.gms.ads.AdView
android:orientation="horizontal"
android:id="@+id/earn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/earn1"
app:adSize="BANNER"
android:layout_centerHorizontal="true"
app:adUnitId="@string/earn2" />
<com.google.android.gms.ads.AdView
android:id="@+id/earn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/earn2"
android:layout_centerHorizontal="true"
android:orientation="horizontal"
app:adSize="BANNER"
app:adUnitId="@string/earn3" />
<com.google.android.gms.ads.AdView
android:orientation="horizontal"
android:id="@+id/earn4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/earn3"
app:adSize="BANNER"
android:layout_centerHorizontal="true"
app:adUnitId="@string/earn4" />
<com.google.android.gms.ads.AdView
android:orientation="horizontal"
android:id="@+id/earn5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/earn4"
app:adSize="BANNER"
android:layout_centerHorizontal="true"
app:adUnitId="@string/earn5" />
<com.google.android.gms.ads.AdView
android:orientation="horizontal"
android:id="@+id/ad13View"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/ad12View"
app:adSize="BANNER"
android:layout_centerHorizontal="true"
app:adUnitId="@string/ad10View" />
<com.google.android.gms.ads.AdView
android:orientation="horizontal"
android:id="@+id/ad14View"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/ad13View"
app:adSize="BANNER"
android:layout_centerHorizontal="true"
app:adUnitId="@string/ad9View" />
<TextView
android:id="@+id/hi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"
android:textSize="20sp"
android:layout_below="@id/hu"
android:text="@string/text" />
</RelativeLayout>
</android.support.v4.widget.NestedScrollView>
Solution
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.your fragment name, container, false);
progressBar = (ProgressBar) view.findViewById(R.id.progressBar);
progressBar.setMax(100);
progressBar.setProgress(0 );
and then
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
progressBar.setVisibility(View.VISIBLE); }
at last
@Override
public void onPageFinished(WebView view, String url) {
progressBar.setVisibility(View.GONE); }
Answered By - anshul raj
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.