Jaison_Joseph
Jaison_Joseph

Reputation: 333

I want to check my webview is scrolled down or not

I have a WebView Fragment in my application, I want to check, the user scrolled the WebView down or not ,for example if the user scrolled down my WebView , I want show a Toast message that "you scrolled down" I'm trying this for the past 3 days, I didn't get any proper solution. I have seen many tutorials and discussions, none of them are working for me. Is there any one who can help ?

This is my fragment java code

public class tab1 extends Fragment {

 public ProgressBar bar;
 public FrameLayout frameLayout;
 public tab1()
 {

 }

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
     View rootView = inflater.inflate(R.layout.fragment_tab1, null);

     final SwipeRefreshLayout swipe =(SwipeRefreshLayout)rootView.findViewById(R.id.swiperefresh1);
     frameLayout=(FrameLayout) rootView.findViewById(R.id.frame1);
     bar=(ProgressBar)rootView.findViewById(R.id.progressBar1);
     final WebView view=(WebView) rootView.findViewById(R.id.webview);

     bar.setMax(100);
     view.loadUrl("http://facebook.com");
     view.getSettings().setJavaScriptEnabled(true);
     view.setWebViewClient(new MyWebViewClient());
     view.setWebChromeClient(new WebChromeClient(){

         public void onProgressChanged(WebView view1,int progress){

            frameLayout.setVisibility(View.VISIBLE);
            bar.setProgress(progress);
            if (progress==100){

            frameLayout.setVisibility(View.GONE);
                swipe.setRefreshing(false);
            }

      super.onProgressChanged(view1,progress);
        }
    });

    view.getSettings().setBuiltInZoomControls(true);
    view.getSettings().setDisplayZoomControls(false);
    bar.setProgress(0);

 swipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {

            swipe.setColorSchemeResources(
                    R.color.pink, R.color.indigo, R.color.lime);

            String webUrl = view.getUrl();

           view.loadUrl(webUrl);
        }
 });
    return rootView;
}
}

This is my xml file

<LinearLayout 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:orientation="vertical"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="com.hackerinside.jaisonjoseph.polysocial.tab1">

<FrameLayout
    android:id="@+id/frame1"
    android:layout_width="match_parent"
    android:layout_height="3dp"
    android:background="@android:color/transparent">

    <ProgressBar
        android:id="@+id/progressBar1"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="fill_parent"
        android:layout_height="3dp"
        android:background="@android:color/transparent"
       android:foregroundGravity="top"
        android:progressDrawable="@drawable/custom_progress"
        android:progress="20"/>

</FrameLayout>


<android.support.v4.widget.SwipeRefreshLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/swiperefresh1"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<WebView
    android:id="@+id/webview"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

</WebView>

</android.support.v4.widget.SwipeRefreshLayout>

Error message after adding answer

Cast from null to OnScrollChangeListener requires API level 23 (current min is 15) less... (Ctrl+F1) 

This check scans through all the Android API calls in the application and warns about any calls that are not available on all versions targeted by this application (according to its minimum SDK attribute in the manifest). If you really want to use this API and don't need to support older devices just set the minSdkVersion in your build.gradle or AndroidManifest.xml files. If your code is deliberately accessing newer APIs, and you have ensured (e.g. with conditional execution) that this code will only ever be called on a supported platform, then you can annotate your class or method with the @TargetApi annotation specifying the local minimum SDK to apply, such as @TargetApi(11), such that this check considers 11 rather than your manifest file's minimum SDK as the required API level. If you are deliberately setting android: attributes in style definitions, make sure you place this in a values-vNN folder in order to avoid running into runtime conflicts on certain devices where manufacturers have added custom attributes whose ids conflict with the new ones on later platforms. Similarly, you can use tools:targetApi="11" in an XML file to indicate that the element will only be inflated in an adequate context

Upvotes: 0

Views: 1402

Answers (1)

Abdul Kawee
Abdul Kawee

Reputation: 2727

ok try this

view.setOnScrollChangeListener(new View.OnScrollChangeListener() {
        @Override
        public void onScrollChange(View v, int scrollX, int scrollY, int 
      oldScrollX, int oldScrollY) {

        }
    });

here view is your WebView object. :) its native method so it will work fine.

Upvotes: 1

Related Questions