OnTouchEvent does not work on Edittext field, is there an alternative? - android

I have a edittext field which prefilled with the value "comment please..." Now I want to delete this content as soon as the User touches this field. In the documentation I saw that there is the possibility to add an onTouchEvent to a edittext field. But its not working because compiler error occurs with
The method onTouchEvent(MotionEvent) in the type TextView is not applicable for the arguments (new View.OnTouchListener(){})
My code is:
comment.onTouchEvent(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (comment.getText().equals( R.string.comment_content )){
comment.setText( "" );
return true;
}
return false;
}});
thanks

Don't do this manually. Android already provides this functionality: "hint text".

MyEditor.setOnTouchListener(new OnTouchListener(){
#Override
public boolean onTouch(View v, MotionEvent event) {
int inType = MyEditor.getInputType(); // backup the input type
MyEditor.setInputType(InputType.TYPE_NULL); // disable soft input
MyEditor.onTouchEvent(event); // call native handler
MyEditor.setInputType(inType); // restore input type
return true; // consume touch even
}
});
This one is disable your input and set on touch work ..

Related

How to restrict ExposedDropdownMenu to enter text in AutocompleteTextViw

I am using ExposedDropdownMenu in the application where I don't want to enter a text in it.It should only choose items from the dropdownlist.It works fine in the normal case but if I tap on other
EditText and quickly tap on ExposedDropdownMenu and type text,the text get reflected in the ExposedDropdownMenu.How to fix this issue
I solved this issue by adding TouchListener and showDropDown() in it.
binding.etType.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
binding.etType.showDropDown();
return false;
}
});

want to do some task in editText.setOnTouchListener only once and not each time

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;
}

How to unsubscribe from onTouch() event

When application is started I run a custom pop-up till a user touches the screen. When screen is touched I catch it with event onTouch() and cancel the pop-up. From this point I don't need the event anymore.
The problem is the event is alive and continues to jump up every time a user touches the screen.
Is there any way to unsubscribe from this event? Something like in c# -= eventName.
The code is below:
#Override
public boolean onTouch(View v, MotionEvent event) {
if (!_stopToast)
{
_hintToast.cancel();
_stopToast = true;
}
return false;
}
There's no such method (lets say removeTouchListener or similar) which will help you to remove an already defined touch listener from a view. Setting null to setOnTouchListener won't help too. What you can do is to create a new object reference of OnTouchListener class which does nothing and set it in setOnTouchListener. For example:
public final OnTouchListener dummyOnTouchListener = new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent rawEvent) {
return false;
}
};
And simply use it as below:
yourView.setOnTouchListener(dummyOnTouchListener);

Android turning clicks into touches

I have an on click listener:
whiteKeyPressedArray[i].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
}}
I see that this allows touches:
public boolean onTouch(View v, MotionEvent event) {
//Switch case for type of touch
}
But how can I detect touch rather than click on my whiteKeyPressedArray[i]?
Thanks!
OnTouch will fire many many times :), actually onTouch will be trigered over and over again as long as you keep your finger to that element (as long as you touch that element). Where onClick will be fire just ones but ONLY if you return false from your onTouch handler.
I don't know what the whiteKeyPressedArray[i] is, but have you tried:
whiteKeyPressedArray[i].setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
return true; // or false if you want the event to pass on
}
});
Maybe this is what you are looking for?

Detect touch on EditText, but not interrupt it?

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!");
}
}
});

Categories

Resources