mirror of
https://github.com/kiwix/kiwix-android.git
synced 2025-09-28 07:31:36 -04:00
new "back to the top" feature
This commit is contained in:
parent
c40a1aad93
commit
f878e0f1f5
@ -49,11 +49,13 @@ import android.widget.TextView;
|
|||||||
import android.widget.TextView.OnEditorActionListener;
|
import android.widget.TextView.OnEditorActionListener;
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
import android.view.View.OnClickListener;
|
import android.view.View.OnClickListener;
|
||||||
|
import android.view.animation.AnimationUtils;
|
||||||
|
|
||||||
public class KiwixMobileActivity extends Activity {
|
public class KiwixMobileActivity extends Activity {
|
||||||
/** Called when the activity is first created. */
|
/** Called when the activity is first created. */
|
||||||
|
|
||||||
private WebView webView;
|
private SharedPreferences mySharedPreferences;
|
||||||
|
private KiwixWebView webView;
|
||||||
private ArrayAdapter<String> adapter;
|
private ArrayAdapter<String> adapter;
|
||||||
protected boolean requestClearHistoryAfterLoad;
|
protected boolean requestClearHistoryAfterLoad;
|
||||||
protected boolean requestInitAllMenuItems;
|
protected boolean requestInitAllMenuItems;
|
||||||
@ -65,6 +67,8 @@ public class KiwixMobileActivity extends Activity {
|
|||||||
private AutoCompleteTextView articleSearchtextView;
|
private AutoCompleteTextView articleSearchtextView;
|
||||||
private LinearLayout articleSearchBar;
|
private LinearLayout articleSearchBar;
|
||||||
private Menu menu;
|
private Menu menu;
|
||||||
|
//Tracks the user preference of of showing the button or not
|
||||||
|
private boolean isButtonEnabled = true;
|
||||||
private boolean isFullscreenOpened;
|
private boolean isFullscreenOpened;
|
||||||
private ImageButton exitFullscreenButton;
|
private ImageButton exitFullscreenButton;
|
||||||
|
|
||||||
@ -146,7 +150,14 @@ public class KiwixMobileActivity extends Activity {
|
|||||||
this.setProgressBarVisibility(true);
|
this.setProgressBarVisibility(true);
|
||||||
|
|
||||||
setContentView(R.layout.main);
|
setContentView(R.layout.main);
|
||||||
webView = (WebView) findViewById(R.id.webview);
|
//Locate and hook up the Back to top button
|
||||||
|
findViewById(R.id.btn_back_to_top).setOnClickListener(new View.OnClickListener() {
|
||||||
|
@Override
|
||||||
|
public void onClick(View view) {
|
||||||
|
webView.pageUp(true);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
webView =(KiwixWebView) findViewById(R.id.webview);
|
||||||
articleSearchBar = (LinearLayout) findViewById(R.id.articleSearchBar);
|
articleSearchBar = (LinearLayout) findViewById(R.id.articleSearchBar);
|
||||||
articleSearchtextView = (AutoCompleteTextView) findViewById(R.id.articleSearchTextView);
|
articleSearchtextView = (AutoCompleteTextView) findViewById(R.id.articleSearchTextView);
|
||||||
|
|
||||||
@ -298,6 +309,37 @@ public class KiwixMobileActivity extends Activity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void onPageFinished(WebView view, String url) {
|
public void onPageFinished(WebView view, String url) {
|
||||||
|
|
||||||
|
//register a {@KiwixWebView.OnPageChangeListener} to get changes in scroll position
|
||||||
|
webView.registerOnPageChangedListener(new KiwixWebView.OnPageChangeListener() {
|
||||||
|
@Override
|
||||||
|
public void onPageChanged(int page, int maxPages) {
|
||||||
|
if (isButtonEnabled)
|
||||||
|
{
|
||||||
|
//Don't go on the values these are observed to be working :p
|
||||||
|
//Simple logic if scrolled to more than the threshold then show the button.
|
||||||
|
if (((double)page/maxPages) >= 0.1)
|
||||||
|
{
|
||||||
|
if (KiwixMobileActivity.this.findViewById(R.id.btn_back_to_top).getVisibility() == View.INVISIBLE)
|
||||||
|
{
|
||||||
|
KiwixMobileActivity.this.findViewById(R.id.btn_back_to_top).setVisibility(View.VISIBLE);
|
||||||
|
//U said you wanted fancy huh,then this might just do it.
|
||||||
|
KiwixMobileActivity.this.findViewById(R.id.btn_back_to_top).startAnimation(AnimationUtils.loadAnimation(KiwixMobileActivity.this,android.R.anim.fade_in));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (KiwixMobileActivity.this.findViewById(R.id.btn_back_to_top).getVisibility() == View.VISIBLE)
|
||||||
|
{
|
||||||
|
KiwixMobileActivity.this.findViewById(R.id.btn_back_to_top).setVisibility(View.INVISIBLE);
|
||||||
|
//U said you wanted fancy huh,then this might just do it.
|
||||||
|
KiwixMobileActivity.this.findViewById(R.id.btn_back_to_top).startAnimation(AnimationUtils.loadAnimation(KiwixMobileActivity.this,android.R.anim.fade_out));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
String title = getResources().getString(R.string.app_name);
|
String title = getResources().getString(R.string.app_name);
|
||||||
if (webView.getTitle()!=null && !webView.getTitle().isEmpty())
|
if (webView.getTitle()!=null && !webView.getTitle().isEmpty())
|
||||||
title = webView.getTitle();
|
title = webView.getTitle();
|
||||||
@ -360,11 +402,11 @@ public class KiwixMobileActivity extends Activity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void loadPref(){
|
private void loadPref(){
|
||||||
SharedPreferences mySharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
|
mySharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
|
||||||
String pref_zoom = mySharedPreferences.getString("pref_zoom", "automatic");
|
String pref_zoom = mySharedPreferences.getString("pref_zoom", "automatic");
|
||||||
Boolean pref_zoom_enabled = mySharedPreferences.getBoolean("pref_zoom_enabled", false);
|
Boolean pref_zoom_enabled = mySharedPreferences.getBoolean("pref_zoom_enabled", false);
|
||||||
Boolean pref_nightmode= mySharedPreferences.getBoolean("pref_nightmode", false);
|
Boolean pref_nightmode= mySharedPreferences.getBoolean("pref_nightmode", false);
|
||||||
|
isButtonEnabled = mySharedPreferences.getBoolean("pref_top_button",isButtonEnabled);
|
||||||
if (pref_zoom.equals("automatic")) {
|
if (pref_zoom.equals("automatic")) {
|
||||||
setDefaultZoom();
|
setDefaultZoom();
|
||||||
} else if (pref_zoom.equals("medium")) {
|
} else if (pref_zoom.equals("medium")) {
|
||||||
@ -384,6 +426,15 @@ public class KiwixMobileActivity extends Activity {
|
|||||||
webView.getSettings().setBuiltInZoomControls(true);
|
webView.getSettings().setBuiltInZoomControls(true);
|
||||||
webView.getSettings().setDisplayZoomControls(pref_zoom_enabled);
|
webView.getSettings().setDisplayZoomControls(pref_zoom_enabled);
|
||||||
|
|
||||||
|
Log.d("kiwix","pref_top_button value ("+pref_zoom_enabled+")");
|
||||||
|
//DONT register a onSharedPrefenceChangedListener as the activity is haulted when on
|
||||||
|
//PreferenceFrag. so it might cause issues
|
||||||
|
if (!isButtonEnabled)
|
||||||
|
{
|
||||||
|
if (findViewById(R.id.btn_back_to_top).getVisibility() == View.VISIBLE)
|
||||||
|
findViewById(R.id.btn_back_to_top).setVisibility(View.INVISIBLE);
|
||||||
|
}
|
||||||
|
|
||||||
//Night mode status
|
//Night mode status
|
||||||
Log.d("kiwix","pref_nightmode value ("+pref_nightmode+")");
|
Log.d("kiwix","pref_nightmode value ("+pref_nightmode+")");
|
||||||
if(NightMode!=pref_nightmode)
|
if(NightMode!=pref_nightmode)
|
||||||
|
55
src/org/kiwix/kiwixmobile/KiwixWebView.java
Normal file
55
src/org/kiwix/kiwixmobile/KiwixWebView.java
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
package org.kiwix.kiwixmobile;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.util.AttributeSet;
|
||||||
|
import android.util.Log;
|
||||||
|
import android.webkit.WebView;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by sakchham on 20/11/13.
|
||||||
|
*/
|
||||||
|
/*
|
||||||
|
* Custom version of link{@android.webkit.WebView}
|
||||||
|
* to get scroll positions for implimenting the Back to top
|
||||||
|
*/
|
||||||
|
public class KiwixWebView extends WebView
|
||||||
|
{
|
||||||
|
OnPageChangeListener changeListener = null;
|
||||||
|
|
||||||
|
public KiwixWebView(Context context) {
|
||||||
|
super(context);
|
||||||
|
}
|
||||||
|
|
||||||
|
public KiwixWebView(Context context, AttributeSet attrs) {
|
||||||
|
super(context, attrs);
|
||||||
|
}
|
||||||
|
|
||||||
|
public KiwixWebView(Context context, AttributeSet attrs, int defStyle) {
|
||||||
|
super(context, attrs, defStyle);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
|
||||||
|
super.onScrollChanged(l, t, oldl, oldt);
|
||||||
|
int windowHeight = getMeasuredHeight();
|
||||||
|
int pages = getContentHeight()/windowHeight;
|
||||||
|
int page = t/windowHeight;
|
||||||
|
//alert the listeners
|
||||||
|
if (changeListener != null);
|
||||||
|
changeListener.onPageChanged(page,pages);
|
||||||
|
}
|
||||||
|
|
||||||
|
public interface OnPageChangeListener {
|
||||||
|
public void onPageChanged(int page,int maxPages);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* We wouldn't be needing more than one so doesn't matter if it wont work for more
|
||||||
|
* than one.
|
||||||
|
*/
|
||||||
|
public void registerOnPageChangedListener(OnPageChangeListener listener)
|
||||||
|
{
|
||||||
|
changeListener = listener;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user