I have created a custom View which I usually attach an onClickListener to. I'd like to have some button like behavior: if it is pressed, it should alter its appearance which is defined in the onDraw() method. However, this code does not work:
//In my custom View:
#Override
protected void onDraw(Canvas canvas)
{
boolean pressed = isPressed();
//draw depending on the value of pressed
}
//when creating the view:
MyView.setClickable(true);
pressed always has the value false. What's wrong?
Thanks a lot!
hey buddy,your fault is you are not implementing click or touch evnt for ur custom view.thr is no click evnt for view.you can use touch event instead of this:so below code work 4 u:
myView.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_UP:
break;
case MotionEvent.ACTION_MOVE:
break;
}
return true;
}
});
in this code use action_up for click and you get it worked for you
have you considered just using a button to do what you want? you could use ToggleButton and write a short selector in xml that will allow you to specify an image to use when pressed or not. this question may be of some help to you.
To make your new custom button draw on clicks don't forget to invalidate the form as necessary. This is a little gotcha. E.g.
#Override
public boolean onTouch(View v, MotionEvent event)
{
/* Parse Event */
this.invalidate();
return true;
}
Related
I am using custom animations at ACTION_DOWN and ACTION_UP events of my button.
However, right now, I am manually setting OnTouchListener to each Button manually with code like this:
myButton.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View yourButton, MotionEvent theMotion) {
Animation animation;
switch (theMotion.getAction()) {
case MotionEvent.ACTION_DOWN:
animation = AnimationUtils.loadAnimation(NickNameScreenActivity.this, R.anim.button_down);
myButton.startAnimation(animation);
break;
case MotionEvent.ACTION_UP:
animation = AnimationUtils.loadAnimation(NickNameScreenActivity.this, R.anim.button_up);
myButton.startAnimation(animation);
break;
}
return false;
}
});
This is cumbersome as I have to repeat this for all Buttons.
Is there a way to set it automatically for all buttons or a tidy XML way of doing this?
You can write a custom view extending Button and use it in xml. Maybe you can replace NickNameScreenActivity.this with getContext().
I want to highlight text and then drag and drop it to a specific area. I know how basic drag and drop works and how to get the selected area to a String.
But I don't know how to to attach a setOnClickListener on a String? Anyone have any idea or is that a total wrong approach?
The string is in an editView
et =(EditText)findViewById(R.id.et);
int startSelection=et.getSelectionStart();
int endSelection=et.getSelectionEnd();
test = et.getText().toString().substring(startSelection, endSelection);
As the commenters pointed out, you can not set a a touch listener on a String, you have to do it on your EditText. You would do something like this:
et.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
et.setFocusable(true);
et.requestFocus();
// Do what you want
break;
case MotionEvent.ACTION_UP:
// Do what you want
break;
default:
break;
}
return true;
}
});
But still, I do not understand why do you want to use a OnTouchListener and not a OnDragListener, which seems to serve your purpose better...
Ok... in my app i update the layout on MotionEvent.ACTION_DOWN and then i check the motion event coordinates to locate my buttons. I can show a toast when finger is released on different buttons. The problem is i need a long touch on my buttons to call another action without conflicting with the MotionEvent.ACTION_UP. Implemented a long click handler but since i don't 'click' its not working. Hope you guys understand my problem.
Whats the best way to get my app working as intended?
My class implements OnTouchListener, OnGestureListener
#Override
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
// UPDATE LAYOUT
break;
case MotionEvent.ACTION_UP:
// GET BUTTON X Y
if (x and y match the button location){
// DO ACTION
}else{
// DO NOTHING
}
// CHANGE LAYOUT TO INITIAL STATE
break;
case MotionEvent.ACTION_MOVE:
break;
}
return false;
mybutton.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
// DO STUFF
return true;
}
});
}
just try to return false in your onTouch(...) method and use onLongClickListener(...) as usual
I call this function in my activity :
#Override
public boolean dispatchTouchEvent(MotionEvent touchEvent)
That allows me to process action before any components get focused or even deny the focus to these elements.
PROBLEM : I was wondering how I could know what component (View) has been touched in this function, then I could choose if I want to consumme the event or not.
UGLY SOLUTION : I'm currently having an ugly solution which is : I know the position of the component that is allowed to get the event, and I do a plenty of condition to approximately decide if the user clicked on this component.
Thanks.
You probably want to use the OnTouchListener
private OnTouchListener mOnTouchListener= new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
switch(v.getId()){
case R.id.id1):
// Do stuff
break;
case R.id.id2:
// Do stuff
break;
}
return false/true;
}
};
view.getid==R.id.//id in layout// condition can be checked for the required view is clicked
I have written a code to implement onclick events of buttons in android.
I have written the following line to set the listener in oncreate method.
I want to recognize the touch event of a button in the application.i am getting a class cast exception in android.
mybtn[i].setOnClickListener( (OnClick Lisener)this);
for the alternatives to recognize the ontouch event i have looked at the SetKeylistener() , setonkeylistener() and setontouchlistener() methods. but i am not getting which one to use exactly.
What exactly i want to achieve is TTS should speak the number when the button is touched, and on the key release that number should be added to some textbox. so what methods should i use to accomplish these things.
You should accomplish the effect by doing this:
mybtn[i].setOnTouchListener(new OnTouchListener()
{
#Override
public boolean onTouch(View arg0, MotionEvent arg1)
{
switch (arg1.getAction())
{
case MotionEvent.ACTION_DOWN:
// Do some stuff
break;
case MotionEvent.ACTION_UP:
// Do some stuff
break;
}
return false;
}
});
use View.OnClickListener if you use setOnClickListener.
To accomplish your purpose you can btn.setOnTouchListener and do something according to the MotionEvent,such as event.getAction=MotionEvent.ACTION_UP ...
for example:
btn.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
}
if (event.getAction() == MotionEvent.ACTION_DOWN) {
}
return false;
}
});
Implement interface in your activity class which ever listener you want to listen on button, like for onClickListener
public class A extends Activity implments OnClickListener