I am trying to make some field with text (no button) what I click on to open datepicker dialog.
I am totally begginer and I am trying it on easy example.
firstbirth.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
Toast.makeText(getApplicationContext(), "ahooj", Toast.LENGTH_LONG).show();
return false;
}
});
firstbirth is spinner, I tried it with Edittext and the result was same.
Can anybody help me?
Thanks in advance!
Use an OnClickListener instead.
setOnClickListener(View.OnClickListener l)
OnTouchListeners will trigger on multiple touch events (like touch down, touch up etc.) whereas the OnClickListener will only get fired once (onClick :) )
If you, for some reason, want to use an OnTochListener you can make sure that its only called once.
Try:
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_UP { // another option would be ACTION_DOWN for example
Toast.makeText(getApplicationContext(), "ahooj", Toast.LENGTH_LONG).show();
return true;
}
return false;
}
Thanks it sounds logical but when I use OnClick event application felt down while initializing.
firstbirth.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
DialogFragment dd1 = new DatePickerFragment();
dd1.show(getFragmentManager(), "Select a date");
}
});
Problem is not with DialogFragment becasue it works when I use onTouch event.
Thanks :)
Related
I have a RecyclerViewClickListener.RecyclerTouchListener and on that #onClick override method, I sat Intent to go another activity.
But the problem is on that RecyclerViewClickListener.RecyclerTouchListener there is one image view also and I implemented onClickListener of that InmageView so that it will open one dialog box.
Now, when I click on that ImageView, 2 things happened at same time.
1.Opened Dialog box
2.It is going to another activity also, because of Intent.
How can I fix?
Try this
holder.hamburgerMenu.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_UP){
if (menuInterface != null) {
menuInterface.onClickHamburger(position);
}
return true;
}
return false;
}
});
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;
}
});
I have a relativelayout and have coded the onTouchListener to highlight background as follows:
relative3.setOnTouchListener(new OnTouchListener()
{
#Override
public boolean onTouch(View arg0, MotionEvent event)
{
if(event.getAction()==MotionEvent.ACTION_DOWN )
{
relative3.setBackgroundColor(getResources().getColor(R.color.tran_grey));
}
if((event.getAction()==MotionEvent.ACTION_UP || event.getAction()==MotionEvent.ACTION_CANCEL))
{
relative3.setBackgroundColor(getResources().getColor(android.R.color.transparent));
}
return true;
}
});
and to perform an action upon onClick
relative3.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
custom_toast("Redirecting...");
}
});
Question:
The relativelayout could show highlight upon ACTION_DOWN and turn back to transparent upon ACTION_UP. Yet it is clicked, the toast simply does not appear as coded.
How could that be modified? Thanks!
Make sure the layout has the property android:clickable="true"
The proper way to do the color change is to use a background drawable xml with a pressed and normal state. If you do this in code, use getActionMasked() instead of getAction()
Return false so so that your OnTouchListener doesn't consumed the event, so the clicklistener will work..
relative3.setOnTouchListener(new OnTouchListener()
{
#Override
public boolean onTouch(View arg0, MotionEvent event)
{
if(event.getAction()==MotionEvent.ACTION_DOWN )
{
relative3.setBackgroundColor(getResources().getColor(R.color.tran_grey));
}
if((event.getAction()==MotionEvent.ACTION_UP || event.getAction()==MotionEvent.ACTION_CANCEL))
{
relative3.setBackgroundColor(getResources().getColor(android.R.color.transparent));
}
// change to false.
return false;
}
});
More info : http://developer.android.com/reference/android/view/View.OnTouchListener.html#onTouch(android.view.View, android.view.MotionEvent)
You need to return false in the onTouch method which tells the OnTouchListener that the click has not been consumed. Afterwards, it forwards the click event to onClick method of OnClickListener.
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
I need to know when the user touches/taps/clicks the edittext in my activity.
How can I do this without interrupting the events, so the keypad still displays properly?
(And I need to know about it before the OS displays the keypad...if possible)
txtEdit.setOnTouchListener(new View.OnTouchListener(){
public boolean onTouch(View view, MotionEvent motionEvent) {
// your code here....
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
return false;
}
});
View.OnTouchListener onTouchListener = new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_UP){
...
}
You should be able to do this by attaching an OnClickListener to your EditText. If you're concerned about blocking the UI thread in your OnClickListener, you can spawn a new Thread and do your work in there - though, if you do that, there's no guarantee the work will be done before the keypad shows up.
Less verbosity
The same Mathias Conradt's approach, but using kotlin:
txtEdit.setOnTouchListener({ view, motionEvent ->
// your code here....
false
})
This line getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE); is unnecessary since the keyboard shown when you tap the editText view.
OnTouchListener was getting called twice for me (on Pixel 2 running Pie). So I used
OnFocusChangeListener instead:
txtEdit.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
Log.d(TAG,"Focused Now!");
}
}
});