Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto Inc/Dec on long press #27

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import android.os.Build;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
Expand Down Expand Up @@ -113,9 +114,85 @@ public void onClick(View mView) {
setNumber(String.valueOf(num + 1), true);
}
});

subtractBtn.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
dec_pressed = true;
touchUpdateHandler.post( new RptUpdater() );
return true;
}
});
addBtn.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
inc_pressed = true;
touchUpdateHandler.post( new RptUpdater() );
return true;
}
});
subtractBtn.setOnTouchListener(TouchListener);
addBtn.setOnTouchListener(TouchListener);
a.recycle();
}


//################### Auto Increment on Long Press ########################
private boolean inc_pressed = false;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Camel Case these variables

private boolean dec_pressed = false;
private int _DELAY = 100;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not a good idea to initialize a variable with underscore in Java. Also it's ideal for constants to be declared in a separate class to hold the constants as the changes will be less.

private Handler touchUpdateHandler = new Handler();
//private boolean mAutoIncrement = false;
//private boolean mAutoDecrement = false;
//public int mValue;
class RptUpdater implements Runnable {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not a good idea to declare a sub-class within a class to perform an operation. Try to separate the class out and try to pass the variables instead of using global variables

public void run() {
if( inc_pressed ){
increment();
touchUpdateHandler.postDelayed( new RptUpdater(), _DELAY );
} else if( dec_pressed ){
decrement();
touchUpdateHandler.postDelayed( new RptUpdater(), _DELAY );
}
}
}


public void decrement(){
int num = Integer.valueOf(textView.getText().toString());
setNumber(String.valueOf(num-1), true);
}

public void increment(){
int num = Integer.valueOf(textView.getText().toString());
setNumber(String.valueOf(num+1), true);
}

private View.OnTouchListener TouchListener = new View.OnTouchListener() {

@Override
public boolean onTouch(View pView, MotionEvent pEvent) {
pView.onTouchEvent(pEvent);

if (inc_pressed) {
if( (pEvent.getAction()==MotionEvent.ACTION_UP || pEvent.getAction()==MotionEvent.ACTION_CANCEL)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea to use object.equals instead of ==. Ideally, the snippet should be MotionEvent.ACTION_UP.equals(pEvent.getAction())

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea to use object.equals instead of ==. Ideally, the snippet should be MotionEvent.ACTION_UP.equals(pEvent.getAction())

it will be null safe as it is compared to a constant , but still .equals is better idea

&& inc_pressed ){
inc_pressed = false;
}
}
else if(dec_pressed)
{
if( (pEvent.getAction()==MotionEvent.ACTION_UP || pEvent.getAction()==MotionEvent.ACTION_CANCEL)
&& dec_pressed ){
dec_pressed = false;
}
}

return true;
}
};

//############# ENDS - Auto Increment on Long Press ###############
private void callListener(View view) {
if (mListener != null) {
mListener.onClick(view);
Expand Down