Am working on my first project in android, Since am creating a browser i added progress bar and swipe to refresh option.
when i scrolled to page bottom then i tied to scroll to the top of the page but swipe refresh
option is triggered, when i tried to scroll to top.
Problem:
- unable to scroll to top of the page.
someone help me solve this issue
Main.xml
<?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:id="@+id/mySwipeLayout" tools:context=".MainActivity"> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:id="@+id/myLinearLayout" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="3dp"> <ProgressBar android:id="@+id/myProgressBar" android:layout_weight="0.1" style="@style/Base.Widget.AppCompat.ProgressBar.Horizontal" android:layout_width="match_parent" android:layout_height="match_parent" /> <ImageView android:id="@+id/myImageView" android:src="@mipmap/ic_launcher" android:layout_weight="0.9" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout> <WebView android:id="@+id/myWebView" android:layout_width="match_parent" android:layout_height="match_parent"></WebView> </LinearLayout> </android.support.v4.widget.SwipeRefreshLayout>
mainactivity.java
package com.sanoj.goproxy; import android.graphics.Bitmap; import android.support.v4.widget.SwipeRefreshLayout; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.webkit.WebChromeClient; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.ProgressBar; import android.widget.Toast; public class MainActivity extends AppCompatActivity { ProgressBar superProgressBar; ImageView superImageView; WebView superWebView; LinearLayout superLinearLayout; SwipeRefreshLayout superSwipeLayout; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); superSwipeLayout = findViewById(R.id.mySwipeLayout); superLinearLayout = findViewById(R.id.myLinearLayout); superProgressBar = findViewById(R.id.myProgressBar); superImageView = findViewById(R.id.myImageView); superWebView = findViewById(R.id.myWebView); superProgressBar.setMax(100); superWebView.loadUrl("http://safebrowser.tk"); superWebView.getSettings().setJavaScriptEnabled(true); superWebView.setWebViewClient(new WebViewClient(){ @Override public void onPageStarted(WebView view, String url, Bitmap favicon) { superLinearLayout.setVisibility(View.VISIBLE); super.onPageStarted(view, url, favicon); } @Override public void onPageFinished(WebView view, String url) { superLinearLayout.setVisibility(View.GONE); superSwipeLayout.setRefreshing(false); super.onPageFinished(view, url); } }); superWebView.setWebChromeClient(new WebChromeClient(){ @Override public void onProgressChanged(WebView view, int newProgress) { super.onProgressChanged(view, newProgress); superProgressBar.setProgress(newProgress); } @Override public void onReceivedTitle(WebView view, String title) { super.onReceivedTitle(view, title); getSupportActionBar().setTitle(title); } @Override public void onReceivedIcon(WebView view, Bitmap icon) { super.onReceivedIcon(view, icon); superImageView.setImageBitmap(icon); } }); superSwipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { @Override public void onRefresh() { superWebView.reload(); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater menuInflater = getMenuInflater(); menuInflater.inflate(R.menu.super_menu, menu); return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()){ case R.id.menu_back: onBackPressed(); break; case R.id.menu_forward: onForwardPressed(); break; case R.id.menu_refresh: superWebView.reload(); break; } return super.onOptionsItemSelected(item); } private void onForwardPressed(){ if (superWebView.canGoForward()){ superWebView.goForward(); } else { Toast.makeText(getApplicationContext(), "Can't Go Further..!", Toast.LENGTH_LONG).show(); } } @Override public void onBackPressed() { if (superWebView.canGoBack()){ superWebView.goBack(); }else { finish(); } } }
Advertisement
Answer
I used mScrollView.fullScroll(mScrollView.FOCUS_DOWN);
to scroll to top,
and thanks to https://stackoverflow.com/a/8684666/3836908 credit to the answer