How to enable editText on touch - java

I have an editText which is disabled and I want to enable it when I touch it.
Here is what I've done but it doesn't work :
if(textmail.isEnabled() == false){
textmail.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
textmail.setEnabled(true);
return true;
}
});
}

i read your use-case carefully you want to disable your TextView and want to disable it onTouch() (Correct me if i am wrong)
Here's a solution instead of using setEnabled() as if you set it onTouch() is not called so better to do it manually by declaring a global variable(boolean) in your (View/activity/fragment/class) where you are writing the logic.
//this as global variable
private boolean isTextViewDisabled=false;
//place this code on onCreate()
textmail.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction()==MotionEvent.ACTION_DOWN ){
if(isTextViewDisabled){
return false;
}
}
return true;
}
});
Set your isTextViewDisabled to true and false when needed as per your use-case.
But confusion here is condition for enabling and disabling should be something else not the touch event as according to your use case code should be like below:
//place this code on onCreate()
textmail.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction()==MotionEvent.ACTION_DOWN ){
if(isTextViewDisabled){
isTextViewDisabled=false;
}
}
return true;
}
});
The above code doesn't make a lot of sense until you explain why you want to disable the textView

If you disable the Edittext, it will not trigger the events i.e. onTouch/onClick. In order to tackle this issue, you need to avoid using disable and use clickable instead. textmail.setClickable(false); within your onCreate method.
To make the component look like it has been disabled, you can also work with the Alpha value where you are to reduce the opacity of the component to make it look faded away.
textmail.setAlpha(0.5f);
Now it should response to your listener...
textmail.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
textmail.setClickable(true);
textmail.setAlpha(1f);
return false;
}
});

try this i hope this help
textmail.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(textmail.isEnabled() == false){
textmail.setEnabled(true);
}
return true;
}
});

Related

AlertDialog appearing twice

When showTimeDialog() is called, dialog appears twice. When I click 'ok', there is another dialog waiting.
public void showTimeDialog(){
final TimePicker timePicker = new TimePicker(this);
timePicker.setIs24HourView(true);
timePicker.setCurrentHour(20);
timePicker.setCurrentMinute(15);
timeDialog=new AlertDialog.Builder(this)
.setTitle("Test")
.setPositiveButton(android.R.string.ok, new OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Log.d("Picker", timePicker.getCurrentHour() + ":"
+ timePicker.getCurrentMinute());
dialog.dismiss();
}
})
.setNegativeButton(android.R.string.cancel,
new OnClickListener() {
#Override
public void onClick(DialogInterface dialog,
int which) {
Log.d("Picker", "Cancelled!");
dialog.dismiss();
}
}).setView(timePicker).show();
}
In onCreate(), I have:
editText_time.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View arg0, MotionEvent arg1) {
showTimeDialog();
return false;
}
});
This looks like in the code you are calling showTimeDialog() method you expect it to not return unless dialog is dismissed. If it is so (your question is not complete so I am partially guessing), then this is wrong as dialogs are asynchronous.
EDIT
It works fine as your dialog IS fired twice. It's because you are doing this in onTouch() then first dialog is started then you touch the screen (as this triggers touch event ACTION_DOWN) and then you release the finger which triggers ACTION_UP. To avoid this, you may want to add condition to your code, and I'd suggest reacting on ACTION_UP, like this:
public boolean onTouch(View v, MotionEvent event) {
if (event.getActionMasked() == MotionEvent.ACTION_UP) {
showTimeDialog();
}
}
Are you certain it's being called only once? Have you done any log output? My guess is that because you are calling the method in onTouch, the method is being called more times than you think.
onTouch(View, MotionEvent) is not only called when the view is touched. There are a number of times it is called with a single press, and you can check the MotionEvent docs to see all of the possible actions which invoke this method.
Probably what is happening is the showTimeDialog() method is being opened when ACTION_DOWN event occurs, then again when a ACTION_UP or ACTION_CANCEL event occurs because your dialog is now blocking the view. Try changing your onTouch method as follows:
public boolean onTouch(View arg0, MotionEvent arg1) {
if (arg1.getActionMasked() == MotionEvent.ACTION_UP)
showTimeDialog();
return false;
}

Holding down a button for repeated action -- is there an android:onClick for holds? (in XML files)

Hopefully the question says it all... I've been searching the web for an easy solution, I'm surprised to see there isn't? I expected this to be a basic functionality that I could just add into the XML file? Alternatives are welcome, but I already know they're out there. I just want to know if this simple method is possible (couldn't seem to find anything in the documentation but who knows)
There isn't any default way of doing this. But it's fairly easy accomplishing this using the onTouch() Event. The following code will repeat a task every 0.5 sec starting the time you touch a button. The tasks will be cancelled whenever you release the button.
Handler repeatedHandler = new Handler();
Runnable newRunnable = new Runnable() {
#Override
public void run() {
//Code for the task that needs to be repeated
//...
//...
repeatedHandler.postDelayed(newRunnable, 500);
}
};
button.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
repeatedHandler.postDelayed(newRunnable, 500);
break;
case MotionEvent.ACTION_UP:
repeatedHandler.removeCallbacks(newRunnable);
break;
}
}
});
you can set OnLongClickListener like
btn.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
---code here---
return false;
}
});
or you can set onTouchListner on button
btn.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction())
{
case MotionEvent.ACTION_UP:
---code here---
break;
case MotionEvent.ACTION_DOWN:
---code here---
break;
}
});
I don't know how to set onLongClickListner from xml
You can use OnLongClickListener() for Button
Follow this link to read more about it...
View.OnLongClickListener
Example :
Examples for android.view.View.OnLongClickListener

How to detect when button pressed and released on android

I would like to start a timer that begins when a button is first pressed and ends when it is released (basically I want to measure how long a button is held down). I'll be using the System.nanoTime() method at both of those times, then subtract the initial number from the final one to get a measurement for the time elapsed while the button was held down.
(If you have any suggestions for using something other than nanoTime() or some other way of measuring how long a button is held down, I'm open to those as well.)
Thanks!
Andy
Use OnTouchListener instead of OnClickListener:
// this goes somewhere in your class:
long lastDown;
long lastDuration;
...
// this goes wherever you setup your button listener:
button.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;
}
return true;
}
});
This will definitely work:
button.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN) {
increaseSize();
} else if (event.getAction() == MotionEvent.ACTION_UP) {
resetSize();
}
return true;
}
});
In onTouchListener start the timer.
In onClickListener stop the times.
calculate the differece.

Which items are sensitive to MotionEvent.ACTION_MOVE?

I am setting OnTouchListener to a Button, I can check if motion type is MotionEvent.ACTION_MOVE. On the other hand if it is TextView, I cannot get any moment of MotionEvent.ACTION_MOVE
For example take a look at the code below:
public boolean onTouch(View view, MotionEvent me) {
if (me.getAction() == MotionEvent.ACTION_DOWN) {
Log.e(TAG,"1");
}
if (me.getAction() == MotionEvent.ACTION_UP) {
Log.e(TAG,"2");
} else if (me.getAction() == MotionEvent.ACTION_MOVE) {
Log.e(TAG,"3");
}
return false;
}
if I bind this listener to a Button, I can see "3" in my logs, on the other hand, if I bind this to an ImageView or etc. I cannot see any "3". but "1" and "2" are acting normal for both situation.
So the question is (if I am not mistaken) which items are MotionEvent.ACTION_MOVE sensitive?
Try implementing OnClickListener also..like this..and put onClick empty.. I think it then detects ACTION_MOVE, along with ACTION_UP and ACTION_DOWN..
class MyActivity implements View.OnTouchListerner, View.OnClickListener{
.....
public void onClick(View v) {}
.....
}
Textview doen't hava any MotionEvent ActionMove, http://developer.android.com/reference/android/widget/TextView.html..
Thanks...

How do I set an OnTouchListener for a relativeLayout?

I want to capture two events from a relativeLayout. When the user clicks it and when the user releases his click (like mousedown/mouseup)
rl.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
//
}
});
I tried setting up something like:
if(event == MotionEvent.ACTION_DOWN) {
}
But eclipse just throws an error about an incompatible operand type. Anyone who knows how to do this?
Try...
if (event.getAction() == MotionEvent.ACTION_DOWN) {
...
}

Categories