How to restrict ExposedDropdownMenu to enter text in AutocompleteTextViw - android

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

Related

How to show a toast when user click a disabled checkbox in android

I don't want the click event change the status of checkbox.Instead, I want to show a toast. So I use checkbox.setEnabled(false), but I can't show the toast if I do this. I don't have any ideas now
I'd say it's bad UX to do what you want.
People should be able to see that your checkbox is disabled from its color for example. They don't expect to be able to interact with it.
Use OnTouchListener For the whole activity then detect the action is Touch down and check the coordinates so that it conform to the checkbox position
this.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
//check e.getX() and e.getY() so that it is inside the checkbox area
return true; //only if the touch is on the check box
//return false otherwise
}
return false;
}
});

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

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 ..

How to disable the virtual key pad in Android?

I would like to disable the virtual key pad that appears when the focus falls on a edit text element.
I've tried it with the following code:
((EditText)findViewById(R.id.EditTextYears)).setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
return false;
}
});
This works partially. The virtual key pad doesn't appear, but I can't focus on the edit text element as well, which I need to be able to do.
probably this should help
http://www.androidpeople.com/android-hide-virtual-keyboard-through-code/

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

how to block virtual keyboard while clicking on edittext in android?

How to block virtual keyboard while clicking on edittext in android
Here is a website that will give you what you need.
As a summary, it provides links to InputMethodManager and View from Android Developers. It will reference to the getWindowToken inside of View and hideSoftInputFromWindow() for InputMethodManager.
A better answer is given in the link, hope this helps.
EDIT
From the link posted above, here is an example to consume the onTouch event:
editText.setOnTouchListener(otl);
private OnTouchListener otl = new OnTouchListener() {
public boolean onTouch (View v, MotionEvent event) {
return true; // the listener has consumed the event
}
};
Here is another example from the same website. This claims to work but seems like a bad idea since your EditBox is NULL it will be no longer an editor:
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 event
}
});
Hope this points you in the right direction!
A simpler way, is to set focusable property of EditText to false.
In your xml layout:
<EditText
...
android:focusable="false" />
Another simpler way is adding android:focusableInTouchMode="false" line to your EditText's xml. Hope this helps.
For cursor positioning you can use Selection.setSelection(...), i just tried this and it worked:
final EditText editText = (EditText) findViewById(R.id.edittext);
editText.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
//change the text here
Selection.setSelection(editText.getText(), editText.length());
return true;
}
});
The best way to do this is by setting the flag textIsSelectable in EditText to true. This will hide the SoftKeyboard permanently for the EditText but also will provide the added bonus of retaining the cursor and you'll be able to select/copy/cut/paste.
You can set it in your xml layout like this:
<EditText
android:textIsSelectable="true"
...
/>
Or programmatically, like this:
EditText editText = (EditText) findViewById(R.id.editText);
editText.setTextIsSelectable(true);
For anyone using API 10 and below, hack is provided here : https://stackoverflow.com/a/20173020/7550472

Categories

Resources