Detect end of fling on ScrollView - android

I've overridden ScrollView to pass MotionEvents to a GestureDetector to detect fling events on the ScrollView. I need to be able to detect when the scrolling stops. This doesn't coincide with the MotionEvent.ACTION_UP event because this usually happens at the start of a fling gesture, which is followed by a flurry of onScrollChanged() calls on the ScrollView.
So basically what we are dealing with here is the following events:
onFling
onScrollChanged, onScrollChanged, onScrollChanged, ... , onScrollChanged
There's no callback for when the onScrollChanged events are done firing. I was thinking of posting a message to the event queue using a Handler during onFling and waiting for the Runnable to execute to signal the end of the fling, unfortunately it fires after the first onScrollChanged call.
Any other ideas?

I've combined a few of the answers from here to construct a working listener that resembles the way AbsListView does it. It's essentially what you describe, and it works well in my testing.
Note: you can simply override ScrollView.fling(int velocityY) rather than use your own GestureDetector.
import android.content.Context;
import android.util.AttributeSet;
import android.widget.ScrollView;
public class CustomScrollView extends ScrollView {
private static final int DELAY_MILLIS = 100;
public interface OnFlingListener {
public void onFlingStarted();
public void onFlingStopped();
}
private OnFlingListener mFlingListener;
private Runnable mScrollChecker;
private int mPreviousPosition;
public CustomScrollView(Context context) {
this(context, null, 0);
}
public CustomScrollView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public CustomScrollView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
mScrollChecker = new Runnable() {
#Override
public void run() {
int position = getScrollY();
if (mPreviousPosition - position == 0) {
mFlingListener.onFlingStopped();
removeCallbacks(mScrollChecker);
} else {
mPreviousPosition = getScrollY();
postDelayed(mScrollChecker, DELAY_MILLIS);
}
}
};
}
#Override
public void fling(int velocityY) {
super.fling(velocityY);
if (mFlingListener != null) {
mFlingListener.onFlingStarted();
post(mScrollChecker);
}
}
public OnFlingListener getOnFlingListener() {
return mFlingListener;
}
public void setOnFlingListener(OnFlingListener mOnFlingListener) {
this.mFlingListener = mOnFlingListener;
}
}

Thank you #PaulBurke +1
Xamarin Solution
using Android.Content;
using Android.Runtime;
using Android.Util;
using Android.Widget;
using System;
public class CustomScrollView : ScrollView
{
public event EventHandler FlingEnded;
public event EventHandler FlingStarted;
private Action ScrollChecker;
private int PreviousPosition;
private const int DELAY_MILLIS = 100;
public CustomScrollView(Context context) : base(context) => Init();
public CustomScrollView(Context context, IAttributeSet attrs) : base(context, attrs) => Init();
public CustomScrollView(Context context, IAttributeSet attrs, int defStyleAttr) : base(context, attrs, defStyleAttr) => Init();
public CustomScrollView(Context context, IAttributeSet attrs, int defStyleAttr, int defStyleRes) : base(context, attrs, defStyleAttr, defStyleRes) => Init();
public CustomScrollView(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer) { }
private void Init()
{
ScrollChecker = () =>
{
int position = ScrollY;
if (PreviousPosition - position == 0)
{
FlingEnded?.Invoke(this, new EventArgs());
RemoveCallbacks(ScrollChecker);
}
else
{
PreviousPosition = ScrollY;
PostDelayed(ScrollChecker, DELAY_MILLIS);
}
};
}
public override void Fling(int velocityY)
{
base.Fling(velocityY);
FlingStarted?.Invoke(this, new EventArgs());
Post(ScrollChecker);
}
}
Usage:
myCustomScrollView.FlingEnded += myCustomScrollView_FlingEnded;
protected void myCustomScrollView_FlingEnded(object sender, EventArgs e) =>
{
//Do onFlingEnded code here
};

Related

Xamarin Android Editext wrapped text

I have a multine edittet control and need to get the text of this control with a carriage return. The problem is that i do not know where the text is wrapped.
Is ther a possibility to get the text formatted as seen in the control?
Or is there a OnWrapped event so that i can append then the /r/n on my own?
Thank you!
I found a dirty possibility which does nearly what I need
public sealed class CustomEditText : EditText
{
private int _prevLineCounter = 1;
public static bool DelIsPressed { get; set; }
private CustomEditText(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer)
{
}
public CustomEditText(Context context) : base(context)
{
BeforeTextChanged += CustomEditText_BeforeTextChanged;
SetOnKeyListener(new KeyListner());
}
private void CustomEditText_BeforeTextChanged(object sender, TextChangedEventArgs e)
{
// not loaded yet
if (LineCount == 0)
return;
if (DelIsPressed)
{
DelIsPressed = false;
return;
}
// text got wrapped
if (_prevLineCounter < LineCount)
{
_prevLineCounter = LineCount;
// find last space an enter carriage return
int spaceIndex = Text.LastIndexOf(' ');
// just one long string
if (spaceIndex == -1)
spaceIndex = Text.Length - 2;
Text = Text.Insert(spaceIndex, "\r\n");
// set cursor to the end
SetSelection(Text.Length);
}
_prevLineCounter = LineCount;
}
public CustomEditText(Context context, IAttributeSet attrs) : base(context, attrs)
{
}
public CustomEditText(Context context, IAttributeSet attrs, int defStyleAttr) : base(context, attrs,
defStyleAttr)
{
}
public CustomEditText(Context context, IAttributeSet attrs, int defStyleAttr, int defStyleRes) : base(context,
attrs, defStyleAttr, defStyleRes)
{
}
}
public class KeyListner: Java.Lang.Object, View.IOnKeyListener
{
public bool OnKey(View v, Keycode keyCode, KeyEvent e)
{
CustomEditText.DelIsPressed = e.KeyCode == Keycode.Del;
return false;
}
}

ClearableEdittext for xamarin android

I have visited this link and many other links on the stack but I'm unable to find a similar solution for xamarin android :
https://stackoverflow.com/a/14470930/7462031
I have implemented the frame layout solution but I want this solution throughout my application So the Clearable Edittext Looked interesting but the droid parts library is not available for xamarin so I am wondering if anyone has a solution for this problem in case you do kindly Help.!
I achieved it by doing the following it might not be the best solution but I guess it is the only solution for Xamarin.Android available for this:
public class ClearableEditext : EditText
{
Context mContext;
Drawable imgX;
public ClearableEditext(Context context) : base(context)
{
init(context, null);
}
public ClearableEditext(Context context, Android.Util.IAttributeSet attrs) : base(context, attrs)
{
init(context, attrs);
}
public ClearableEditext(Context context, Android.Util.IAttributeSet attrs, int defStyleAttr) : base(context, attrs, defStyleAttr)
{
init(context, attrs);
}
public ClearableEditext(Context context, Android.Util.IAttributeSet attrs, int defStyleAttr, int defStyleRes) : base(context, attrs, defStyleAttr, defStyleRes)
{
init(context, attrs);
}
public void init(Context ctx, Android.Util.IAttributeSet attrs)
{
mContext = ctx;
imgX = ContextCompat.GetDrawable(ctx, Android.Resource.Drawable.PresenceOffline);
imgX.SetBounds(0, 0, imgX.IntrinsicWidth, imgX.IntrinsicHeight);
manageClearButton();
this.SetOnTouchListener(new TouchHelper(this, imgX));
this.AddTextChangedListener(new TextListener(this));
}
public void manageClearButton()
{
if (this.Text.ToString().Equals(""))
removeClearButton();
else
addClearButton();
}
public void addClearButton()
{
this.SetCompoundDrawables(this.GetCompoundDrawables()[0],
this.GetCompoundDrawables()[1],
imgX,
this.GetCompoundDrawables()[3]);
}
public void removeClearButton()
{
this.SetCompoundDrawables(this.GetCompoundDrawables()[0],
this.GetCompoundDrawables()[1],
null,
this.GetCompoundDrawables()[3]);
}
}
public class TouchHelper : Java.Lang.Object, View.IOnTouchListener
{
ClearableEditext Editext;
public ClearableEditext objClearable { get; set; }
Drawable imgX;
public TouchHelper(ClearableEditext editext, Drawable imgx)
{
Editext = editext;
objClearable = objClearable;
imgX = imgx;
}
public bool OnTouch(View v, MotionEvent e)
{
ClearableEditext et = Editext;
if (et.GetCompoundDrawables()[2] == null)
return false;
// Only do this for up touches
if (e.Action != MotionEventActions.Up)
return false;
// Is touch on our clear button?
if (e.GetX() > et.Width - et.PaddingRight - imgX.IntrinsicWidth)
{
Editext.Text = string.Empty;
if (objClearable != null)
objClearable.removeClearButton();
}
return false;
}
}
public class TextListener : Java.Lang.Object, ITextWatcher
{
public ClearableEditext objClearable { get; set; }
public TextListener(ClearableEditext objRef)
{
objClearable = objRef;
}
public void AfterTextChanged(IEditable s)
{
}
public void BeforeTextChanged(ICharSequence s, int start, int count, int after)
{
}
public void OnTextChanged(ICharSequence s, int start, int before, int count)
{
if (objClearable != null)
objClearable.manageClearButton();
}
}
To change the x icon as your custom one change the image in init()

Android custom control with translate animation

I'm pretty new in the android world.
I try to create in my app a custom control like the following description:
- Objects appears one by one (timer) at the right of that custom controls and are translated (with animation) to the left of the screen.
The rendering is the following right now:
And the code is like:
public abstract class SheetBase : SurfaceView
{
protected override void OnDraw(Canvas canvas)
{
base.OnDraw(canvas);
this.Update(canvas);
}
}
public class MusicSheet : SheetBase, ISurfaceHolderCallback
{
public MusicSheet(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer)
{
}
public MusicSheet(Context context) : base(context)
{
}
public MusicSheet(Context context, IAttributeSet attrs) : base(context, attrs)
{
}
public MusicSheet(Context context, IAttributeSet attrs, int defStyleAttr) : base(context, attrs, defStyleAttr)
{
}
public MusicSheet(Context context, IAttributeSet attrs, int defStyleAttr, int defStyleRes) : base(context, attrs, defStyleAttr, defStyleRes)
{
}
protected override void Update(Canvas canvas)
{
// Logic to draw staf on Canvas
}
public void SurfaceChanged(ISurfaceHolder holder, Format format, int width, int height)
{
this.Invalidate();
}
public void SurfaceCreated(ISurfaceHolder holder)
{
this._thread = new Thread(this.Loop);
this._thread.Start();
}
public void SurfaceDestroyed(ISurfaceHolder holder)
{
this._thread?.Abort();
}
private void Loop()
{
while (true)
{
Canvas c = null;
try
{
c = this.Holder.LockCanvas();
c.DrawColor(Color.Transparent, PorterDuff.Mode.Clear);
// Draw content at the new position
}
finally
{
if (c != null)
this.Holder.UnlockCanvasAndPost(c);
}
Thread.Sleep(10);
}
}
}
What do you think about the solution / performances ?
You can use ViewPropertyAnimator to translate a View.
For example,
view.animate()
.translationX(100) // horizontal translation
.translationY(100) // vertical translation
To animate from right to left:
view.animate()
.translationX( - screenWidth )
Refer the doc for more info.

How to implement "reverse click" event with "buttondown" as normal state?

My Android app needs a special button whose normal state is MouseDown. The Click event is fired when MouseUp event is fired.
So this button is actually opposite of normal button
It doesn't needs a DoubleClick event. Tried search all over the web; couldn't find anything.
Here is how I implemented the "ReverseButton":
import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.Button;
public class CustomButton extends Button {
private CustomButtonListener mListener;
private boolean mPressedState;
public CustomButton(Context context) {
super(context);
}
public CustomButton(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomButton(Context context, AttributeSet attrs, int defStyleAttr) {
this(context, attrs, defStyleAttr, 0);
}
#TargetApi(Build.VERSION_CODES.LOLLIPOP)
public CustomButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
#Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mPressedState = true;
break;
case MotionEvent.ACTION_UP:
if(mPressedState) {
mListener.onRelease(event);
}
mPressedState = false;
break;
}
return super.onTouchEvent(event);
}
public void setOnReleaseListener(CustomButtonListener listener) {
mListener = listener;
}
public interface CustomButtonListener{
void onRelease(MotionEvent event);
}
}

Make uneditable EditText respond to first click

I'm trying to make a popup menu appear when the user clicks on an EditText but I don't want the EditText itself to be editable. I've tried many things like setting its KeyListener to null, setting it's InputType to null, but what always happens is that the first click gives the View focus and the second click actually registers with my OnClickListener. So user has to click twice to get the menu to popup. Any ideas?
public class PopupEditText extends EditText implements OnClickListener
{
private PopupMenu mMenu;
private Context mContext;
public PopupEditText(Context context)
{
super(context);
init(context);
}
public PopupEditText(Context context, AttributeSet attrs)
{
super(context, attrs);
init(context);
}
public PopupEditText(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
init(context);
}
private void init(Context context)
{
mContext = context;
setKeyListener(null);
mMenu = new PopupMenu(context, this);
}
#Override
public void onClick(View view)
{
mMenu.show();
}
You're not far off. I'd recommend just overriding onTouchEvent() directly, and responding only to ACTION_UP events. Optionally, force it to be disabled and non-focusable. For example:
public class UnmodifiableEditText extends EditText {
private PopupMenu mPopupMenu;
public UnmodifiableEditText(Context context) {
super(context);
init(context);
}
public UnmodifiableEditText(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public UnmodifiableEditText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context);
}
private void init(Context ctx) {
super.setEnabled(false);
super.setFocusable(false);
mPopupMenu = new PopupMenu(ctx, this);
}
#Override public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
Toast.makeText(getContext(), "Showing Popup", Toast.LENGTH_SHORT).show();
mPopupMenu.show();
}
return true;
}
#Override public void setEnabled(boolean enabled) {
// Do not allow enabling the EditText
}
#Override public void setFocusable(boolean focusable) {
// Do not allow focusability changes
}
}

Categories

Resources