I want to call a method after my button get touched and call another method after touch finished,
is it possible?
Try This Code it May help you
yourButton.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
// Do what you want
return true;
}
return false;
if(event.getAction() == MotionEvent.ACTION_UP){
// Do what you want
return true;
}
return false;
}
});
It seems like you have some research to do. What kind of view do you have? You need something like this:
#Override
public void onClick(View v) {
//Call your method here
}
But it is impossible to know what exactly you need, without knowing anything about what you are doing.
EDIT: You changed your question. This is what you're looking for.
Related
I have a button that runs a code that changes the background to a random color on each click. I would also like to give the user the ability to keep on changing the background color as long as the button is clicked. I believe that the onTouchListener will be my best bet. However, I do not now how to implement the code correctly.
I tried on the onLongClickListener but found out that onLongClickListener doesn't work that way.
Incomplete code for the onTouchListener (randomize is the name of my button):
randomize.setOnTouchListener(new Button.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
// start the thread
return true;
} else if(event.getAction() == MotionEvent.ACTION_UP){
// stop the thread
return true;
}
return false;
}
});
So, what I aim to be able to do is to keep on pressing the button and having the background continuously change while still preserving the onclick method of the button. So, onclick changes the background once and continuous click changes the background continuously.
Thank you so much folks :)
Ps. I'm just a beginner to android so I'm sorry if I do not know much. :)
try this:
btn.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
count++;
Toast.makeText(getApplicationContext(),Integer.toString(count) , Toast.LENGTH_SHORT).show();
return true;
}
});
How to make an app which will not listen to touch events and back/home pressing but will listen only to the power button.
I've tried this but it wasn't successful.
#Override
public boolean onKeyDown(int keyCode, android.view.KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_POWER) {
return true;
}else if ((keyCode == KeyEvent.KEYCODE_HOME)|| (keyCode == KeyEvent.KEYCODE_BACK )){
return false;
}
return false;
}
You could wrap you class with a custom View of yours and override its OnClickListener and OnTouchListener methods. Override them with blank methods.
Also, keep your code to set how your app should work when the Power Button is clicked.
For instance, if you wrap your layout with a RelativeLayout or a general View, you could use something like
relativeLayout.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
return false;
}
});
relativeLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
return false;
}
});
To block user touch event try this solution. And if you want to disable back and home try to override this method of Activity:
#Override
public void onAttachedToWindow() {
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
super.onAttachedToWindow();
}
three weeks ago, i want also to catch the home button, but i faied. i do some research, i find the android system don't support to modify it.you can visit this
Overriding the Home button - how do I get rid of the choice?, about homebutton.
as for the touchEvent, i advice you can try this method ,override onTouchEvent (if you dont add onTouchListener, make it retrun fasle and do nothing, at the same time ,the activity also dont handle the onTounEvent or OnClick(whole layout).
i want to do something like when user click on editText then it gets enabled to be edit (by default it gets disabled in xml) and a toast(and some more buttons) should appear only once, not every time.
i create following logic for this:-
edt_title.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (MotionEvent.ACTION_UP == event.getAction()) {
Crouton.showText(ShowActivity.this, "Editing Mode Enabled",Style.INFO);
welcome.animate(getApplicationContext(), gallery, R.anim.fab_jump_from_down, 300);
edt_title.setFocusableInTouchMode(true);
}
return false;}
});
problem is that it gets called everytime when user tap on editText,
is there any way to do it only once when editText gets clicked.
May be it possible with use sharedPreference but it will be better if i can do it without using of sharedPreference.
Thanks in advance :)
Nishu
A simple solution would be:
edt_title.setOnTouchListener(new View.OnTouchListener() {
boolean called = false;
#Override
public boolean onTouch(View v, MotionEvent event) {
if (!called && MotionEvent.ACTION_UP == event.getAction()) {
Crouton.showText(ShowActivity.this, "Editing Mode Enabled",Style.INFO);
welcome.animate(getApplicationContext(), gallery, R.anim.fab_jump_from_down, 300);
edt_title.setFocusableInTouchMode(true);
called = true;
}
return false;
}
});
declare a global var that you use like flag and use for one simple time
boolean flagOneTime = true;
and add this inside your listener
if( flagOneTime )
{
// do something and disable flag
flagOneTime = false;
}
I have created a custom view - on the view class itself, I do some calculations during the OnTouch event - it appears as follows:
#SuppressLint("ClickableViewAccessibility") #Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
pointJoyPos.set((int)event.getX(),(int)event.getY());
UpdateJoy();
} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
pointJoyPos.set((int)event.getX(),(int)event.getY());
UpdateJoy();
} else if (event.getAction() == MotionEvent.ACTION_UP) {
pointJoyPos.set(pointJoyCentre.x,pointJoyCentre.y);
UpdateJoy();
}
return true;
}
When I add this view to my XML to display to the user, I want to display the latest values, e.g. of pointJoyPos, so I wrote the following ...
THE_CUSTOM_VIEW.setOnTouchListener(new View.OnTouchListener(){
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
ValX = THE_CUSTOM_VIEW.GetValOfX();
return false;
}
}
);
...Where GetValOfX is located in the custom view class as is meant to to return pointJoyPos.X.
The problem I have is that I am getting values one step delayed, I think what is happening is that onTouchEvent of the custom view is running on the main activity before on the custom view itself.
I hope this is clear!
Any suggestions on how to overcome this such that the latest values are displayed?
Thank you.
Incase anyone is having the same trouble, I just sorted this by creating a custom Event ...
https://gist.github.com/pipiscrew/10004381
Is there any way to know when user touch the screen?
Actually i want to know user is using the phone or not. And i thought if user doesn't touch the screen he/she doesn't use the phone. I've already goggled it but i couldn't find any proper things, that's why i want to ask this question in here, and i couldn't find any similar question in here.
I find some code like this :
#Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
return super.onTouchEvent(event);
}
but i don't know how to use properly.
thanks in advance.
You can check the last touch of screen using
System.curTimeMillis();
And use
long lastTouchTime;
#Override
public boolean onTouchEvent(MotionEvent event) {
lastTouchTime = System.curTimeMillis();
return true;
}
Or you can check is screen touched now or not :
boolean isScreenTouchedNow;
#Override
public boolean onTouchEvent(MotionEvent event) {
switch(event.getAction())
{
case MotionEvent.ACTION_DOWN :
isScreenTouched = true;
break;
case MotionEvent.ACTION_UP:
isScreenTouched = false;
break;
}
return true;
}
public boolean isScreenTouchedNow()
{
return isScreenTouched;
}