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/
Related
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;
}
});
I tried to capture key event for android. Here is the source code i tried:
text.setOnKeyListener(new OnKeyListener() {
#Override
public boolean onKey(View view, int keyCode, KeyEvent event) {
if(keyCode==KeyEvent.KEYCODE_A){
Toast.makeText(MainActivity.this, "A Pressed!!", Toast.LENGTH_LONG).show();
}
return false;
}
});
But it's doesn't work or show any error.
How can i handle the key event for android keyboard?
Software keyboards don't generate onKey events. Only hardware keyboards do. If you want to see changes from a software keyboard, use a TextWatcher on the view.
So you need to use TextChangedListener, Refer here, based on the text, do your work.
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 ..
Though i have asked the question before but dint get any proper answer. I have an EditText when edit text is focused android virtual keyboard pops up, i have added Done button to the keyboard using ime option from property window. Now i want to perform some action by pressing the Done button. How to do this? Please any body help.
you an do it by firest finding reference of edittext and than set belows code for ur edittext:
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
performSearch();//do here your stuff f
return true;
}
return false;
}
});
here i have taken editText name which is ref. editextbox which u added and you have to add actionId==type of ime options which u have setted
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