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
Related
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;
}
When i use an onTouchListener in the getView of my adapter the line
android:listSelector="#drawable/circle"
immediately stops working, if I set onTouch to return false it works again, however then the ACTION_DOWN ACTION_UP dosent work properly.
Heres what i have in onTouch
image.setOnTouchListener(new View.OnTouchListener() {
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
Assets.playMusic(songID, false);
} else if (event.getAction() == MotionEvent.ACTION_UP) {
Assets.mediaPlayer.stop();
Assets.mediaPlayer = null;
}
return true;
}
});
Its suppose to play music for as long as you have a finger held on the item and when you release it should stop the music. And it works well when returning true. However for some reason the circle stops appearing behind the tapped items. If it is set to false the circle appears, but then action_up dosent stop the music
ive tried using .setSelected .setActivated .setEnabled and none of them work
please help
Also i want it to work kinda like snapchats camera button, tap it and it does one thing, hold it and it does something for duration of your hold. I was going to use time variables in the Action up and down. but if anyone knows another way to do this id appreciate info about that too
In this situation is not encouraged to attach an OnTouchListener to the image, but to the items of the GridView instead.
You should have something like this:
GridView gridview = (GridView) findViewById(R.id.the_gridview_id);
gridview.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, final View v, int position, long id) {
doSomething();
}
});
EDIT:
In order to know how much time a view is pressed, you can do something like this:
// this goes somewhere in your class:
long lastDown;
long lastDuration;
...
// this goes wherever you setup your button listener:
gridview.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN) {
lastDown = System.currentTimeMillis();
} else if (event.getAction() == MotionEvent.ACTION_UP) {
lastDuration = System.currentTimeMillis() - lastDown;
}
}
};
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.
I want to do some action when button is pressed and stop those action when button is releasd. I have used the following code but it does not work. you may check my code here-
ok = (Button)findViewById(R.id.button1);
ok.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
my_var=true;
if (event.getAction() == MotionEvent.ACTION_DOWN) {
while(my_var) do_something();
}else if (event.getAction() == MotionEvent.ACTION_UP) {
my_var=false;
}
return true;
}
});
Is there any alternative solution for the above. Thanx in advance.
I think what you want is an AutoRepeatButton
Basically it's a class that extends Button and adds some scheduled callbacks (you can adjust the interval) that happen as long as the ACTION_DOWN MotionEvent is occurring.
There are a few implementations floating around but I like the following one the best:
https://stackoverflow.com/a/8463940/833647
To achieve so, you have to set my_var to false somewhere in the do_something method.
I have the following code which creates an ImageButton and plays a sound when clicked:
ImageButton SoundButton1 = (ImageButton)findViewById(R.id.sound1);
SoundButton1.setImageResource(R.drawable.my_button);
SoundButton1.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN ) {
mSoundManager.playSound(1);
return true;
}
return false;
}
});
The problem is that I want the image on the ImageButton to change when you press it. The OnTouchListener appears to be overriding the touch and not allowing the images to change. As soon as I remove the OnTouchListener, the ImageButton swaps to a different image when pressed. Any ideas on how I can have the images change on the ImageButton while still using the OnTouchListener? Thank you very much!
SoundButton1.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN ) {
mSoundManager.playSound(1);
btnmode.setImageResource(R.drawable.modeitempressed);
}
elseif (event.getAction() == MotionEvent.ACTION_UP ) {
btnmode.setImageResource(R.drawable.modeitemnormal);
}
return false;
}
});
I think the solution is simple: remove the return true from the ontouchlistener. Since that blocks all further operations that respond to touch and input. Make it return false too.
This way it will allow other actions to also respond to the touch.