I have an activitiy where I have a button and when a click on this button I want to set a TextView with some value, so I used onClickListening and it is working:
ButtonPlus.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
ponts = ponts + 1;
resultadoTextView.setText(Integer.toString(ponts));
}
});
But the problem is that I want to keep increasing this textView's value while the button keep being pressed so I tried to use the OnTouchLister:
ButtonPlus.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
ponts = ponts + 1;
resultTextView.setText(Integer.toString(ponts));
}
});
the problem is that when I give a fast click in the button it increments the TextView's value too much and I want the onTouchListener to be activated just after some time that the button was pressed.
any help please?
Use some additional counter.
int additionalCounter = 0;
ButtonPlus.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
++additionalCounter;
if (additionalCounter % X == 0) {
ponts = ponts + 1;
resultTextView.setText(Integer.toString(ponts));
}
}
});
You can set X as you want, i.e. setting it to 5 would make touch event working 5 times slower.
Try this code.
ButtonPlus.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
ponts = ponts + 1;
resultTextView.setText(Integer.toString(ponts));
ButtonPlus.setClickable(false);
//wait 1 second
ButtonPlus.postDelayed(new Runnable() {
#Override
public void run() {
ButtonPlus.setClickable(true);
}
}, 1000);
return false;
}
});
Related
I am creating a video player in android studio. I want to hide buttons, layout and media controller after 5 seconds and I am using gestures for different properties but the issue is that when I apply 2 to 3 gestures in a certain time, after 5 seconds the buttons and media controller start blinking. I use this code for stying on screen
centerlayout.setOnTouchListener(new LinearLayout.OnTouchListener(){
#Override
public boolean onTouch(View v, MotionEvent m) {
if (gestureDetectorc.onTouchEvent(m)) {
if(m.getAction()==MotionEvent.ACTION_UP){
handler.postDelayed(new Runnable() {
#Override
public void run() {
hide();
}
},5000);
}
}
return true;
}
});
Declare the runnable in global
Runnable mRunnable = new Runnable() {
#Override
public void run() {
hide();
}
};
call removeCallbacks before you call postDelay
centerlayout.setOnTouchListener(new LinearLayout.OnTouchListener(){
#Override
public boolean onTouch(View v, MotionEvent m) {
if (gestureDetectorc.onTouchEvent(m)) {
if(m.getAction()==MotionEvent.ACTION_UP){
handler.removeCallbacks(mRunnable);//add this
handler.postDelayed(mRunnable, 5000);
}
}
return true;
}
});
I would like to add a dollar sign ($) automatically when the editText field is clicked. I've attempted to use addTextChangedListener but it doesn't work as expected.
I do not want to add it after the use has finishes entering all the text. Just want it to be added once the field is clicked or becomes the focus.
EditText editTextAd;
editTextAd.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
editTextAd.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_DEL){
keyDel = true;
}else{
keyDel = false;
}
return false;
}
});
if (!keyDel) {
String str = s.toString();
if (s.length() == 0) {
str += "$";
editTextAd.setText(str);
editTextAd.setSelection(str.length());
//keyDel=false;
}
}
}
#Override
public void afterTextChanged(Editable s) {
}
});
setOnKeyListener is listening keys from Keyboard(hardware).
Change EditText.text in onTextChanged will cause a never-end loop which actually won't work.
when the editText field is clicked
The event is according to setOnClickListener or setOnTouchListener.
becomes the focus
The event is setOnFocusChangeListener.
Step - 1: First call edittext.setOnfocusChangeListner so that on user click on your editText it detect its focus.
Step - 2: Call edittext.setText("$"); which set $ sign in first position on editText.
Step - 3: Call editText.setSelection(1) so that your cursor will start at position 1 not 0.
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus){
editText.setText("$");
editText.setSelection(1);
}
}
});
I have a Button that I have tried to make touchable so when I hold it down it moves an ImageView constantly for the amount of time it is held down, but instead, it only moves the ImageView once each time I press it.
final ImageView box = (ImageView) findViewById(R.id.box);
currentX = (int) box.getX();
Button rightarrow = (Button) findViewById(R.id.right);
rightarrow.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, final MotionEvent e) {
v.performClick();
if (e.getAction() == MotionEvent.ACTION_DOWN) {
v.setPressed(true);
handlerRight.postDelayed(new Runnable() {
#Override
public void run() {
if (e.getAction() == MotionEvent.ACTION_DOWN) {
currentX += 5;
box.setX(currentX);
handlerRight.postDelayed(this, 100);
} else {
}
}
}, 0);
} else {
v.setPressed(false);
}
return false;
}
});
In that new Thread that you run there, you need to create a while loop that will run while the button is pressed. In that while loop, just move the image. Here is the code:
final ImageView box = (ImageView) findViewById(R.id.box);
currentX = (int) box.getX();
Button rightarrow = (Button) findViewById(R.id.right);
rightarrow.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, final MotionEvent e) {
v.performClick();
if (e.getAction() == MotionEvent.ACTION_DOWN) {
v.setPressed(true);
handlerRight.postDelayed(new Runnable() {
#Override
public void run() {
while (v.isPressed()){
currentX += 5;
box.setX(currentX);
//handlerRight.postDelayed(this, 100);
}
}
}, 0);
} else {
v.setPressed(false);
}
return false;
}
});
Note: I'm not sure where that
handlerRight.postDelayed(this, 100);
is supposed to be because I'm not familiar with it so you just put it. But this is it I hope!
I'm currently creating a custom double tap using the onClickListener with the following code:
newImage.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
counterTap++;
Timer t = new Timer("Double tap counter");
t.schedule(new TimerTask() {
#Override
public void run() {
counterTap = 0;
}
}, 0, 300);
if(counterTap >= 2) {
newImage.setVisibility(FrameLayout.GONE);
counterTap = 0;
}
}
});
The problem I'm facing is as follows:
Whenever I tap the ImageView the event does fire. However, the second time I tap the ImageView, the above code only executes when clicking on the exact same position on the ImageView as before.
Rather use a onTouchListener. In the touch listener in the onTouch method you can return false the first time you tapped which means the event was not handled, the second time you return true and the touch listener will handle the event as finished. See my example below, you can use a similar method
new OnTouchListener() {
boolean doubleTap = false;
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_UP:
if (!doubleTap) {
doubleTap = true;
return false;
} else {
return true;
}
}
}
};
this might solve your issue.
EDIT : This is a better opotion
private class GestureListener extends GestureDetector.SimpleOnGestureListener {
#Override
public boolean onDown(MotionEvent e) {
return true;
}
// double tap event
#Override
public boolean onDoubleTap(MotionEvent e) {
return true;
}
}
This question might also help you get to the best answer
I'm trying to catch the event of removing keyboard from the screen and i'm using the following code:
searchTextField.setOnEditorActionListener(new OnEditorActionListener()
{
public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
{
System.out.println("ACTION ID " + actionId);
if(actionId == EditorInfo.IME_ACTION_DONE)
{
System.out.println("ACTION DONE!!!!!!!!!!");
return true;
}
return false;
}
searchTextField.setOnFocusChangeListener( new View.OnFocusChangeListener()
{
public void onFocusChange(View v, boolean hasFocus)
{
if (hasFocus)
System.out.println("HAS FOCUS");
else
System.out.println("FOCUS LOST");
}
});
But unfortunately it doesn't work. onEditorAction just never called, no matter if i start editing or finish. Regarding onFocusChange method it's called just for the very first time when the keyboard goes up. When the keyboard goes down or when it goes up for the second time it's not called. Can anyone explain what am i doing wrong?
I used GlobalLayoutListener on the activity rootView for checking that whether keyboard is hidden or visible:
It works as follows:
final View activityRootView = findViewById(R.id.activityRoot);
activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
int heightDiff = activityRootView.getRootView().getHeight() - activityRootView.getHeight();
if (heightDiff > 100) { // if more than 100 pixels, its probably a keyboard...
... do something here
}
}
});