I have set a button to focusable and focusableInTouchMode to true (I need it to be focusable). Now when I click the button, the onCilck event cannot be triggered (it is gaining focus and ignore onClick event), only when I cilck again the onClick event will be triggered.
I found a solutions like this:
input.setOnFocusChangeListener(new View.OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
v.performClick();
}
}
});
But this solution will cause some side effect as well, for example, if we click 'next' in the keyboard and jump focus to the button, onClick will fire. This is not what I want.
This issue is really anoying, is this a bug? Or it is intentional behaviour? Anyway to solve this?
set focusableInTouchMode false. Or just don't include that tag at all. Let it take its default value, whatever it is supposed to.
Related
I have a TextView that has the isSelectable attribute set to TRUE but i also have a onClickListener on it because. I want if a person holds on the text the text to be selected and he can copy it but if he just clicks on it i want a screen to be opened.
this.subtitle.setTextIsSelectable(true);
this.subtitle.setOnClickListener(v -> openMyScreen());
So what happens is that the selection works fine but if you click on it, the first event is consumed somewhere and only when i click for the second time it works. Does any 1 have any idea how i can fix this.
So the issue is, when you click it once, the textview gets focused. That's what's consuming your click event. The only work around I have been able to find is using setOnFocusChangeListener on the textview, then check if the texview got focused, and use that as a click event.
textview.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
// Handle click event
}
}
});
Please note that you will still have to use OnClickListener as well to handle click events post focusing
I have a project in which I have overridden onTouchEvent() and register taps and moves through it. I also have buttons on the screen. I'm trying to connect methods to the buttons ie:
public void onButtonClick(View view) {
System.out.println("Here");
}
I can connect the method to the button, and the connection appears in the xml:
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onButtonClick"
android:text="Button"
tools:layout_editor_absoluteX="89dp"
tools:layout_editor_absoluteY="139dp" />
But the button click does not appear to be registered. Is it because I'm also overriding onTouchEvent()? If so should I be registering clicks through MotionEvents? If so, what's a clean way to differentiate clicks coming from multiple buttons?
But the button click does not appear to be registered. Is it because
I'm also overriding onTouchEvent()?
Yes. When onTouch() method returns true, Android will consider the event has been consumed and will not pass it on to the other various touch handlers (I think it includes the onClickListener).
If so should I be registering clicks through MotionEvents?
You can do that. Or depending on your condition, simply return false from the method and let onClickListener do the job.
If so, what's a clean way to differentiate clicks coming from
multiple buttons?
onTouch(final View v, final MotionEvent event)
As you can see the onTouch() method has View type as parameter. You can check the Id of view by getting v.getId() and compare this id with the Button's Id, which you are expecting. You can do as follows:
#Override
public boolean onTouch(final View v, final MotionEvent event) {
if(v.getId()==R.id.button1){
//Click is coming from Button with id button1 (specified in layout.xml as android:id="#+id/button1").
//Do something for click
}
...
}
I want to disappear the keyboard when the focus goes out of the TextEdit box. I have throughly searched stackoverflow and google for an answer and have tried every single response/article I have seen but nothing seems to work for me.
As many, I am very new to Android development. I must be overlooking some minor detail to no avail.
My current implementation (again, I have tried many solutions) is as follows:
My class implements OnFocusChangeListener. OnCreate after calling super and setContentView I do:
EditText editText = (EditText) findViewById(R.id.text_box);<br> editText.setOnFocusChangeListener(this);
and
#Override
public void onFocusChange(View v, boolean hasFocus) { Log.d(TAG, "--> onFocusChange"); }
But I never see that Log message.
Sometimes I think that onFocusChange is not what I think it is. My idea is that when we touch the textedit field this view gets the focus, and when you touch any other area of the screen, say a button/listbox/etc the textedit losses the focus and onFocusChange should be called, but it is not, I never see the log entry.
Perhaps it is useful to clarify that I am using Android Studio and created an app that uses fragments.
So, I am doing this on the Detail Activity which in turn is part of a fragment. I have also tried to do it on the onActivityCreated of the fragment.
Neither approach works for me.
Any ideas what I can be missing here?
I would really appreciate your comments.
edittext.requestFocus();
or
Try this :
EditText editText = (EditText) findViewById(R.id.text_box);
editText.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus)
{
if (hasFocus == true){
edittext.requestFocus();
}
else{
InputMethodManager imm = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
}
}
});
editText.setFocusableInTouchMode(true);
try doing that first?
You need to have the field be focusable before you can have the Focus Events fire on it. But i guess it didn't help you in this case :(
onFocusChangeListener is an interface which when you try to implement it as you did you also need to pass the context you wanna be Observed, so in practice one of the view elements of your ViewHolder must be Observed. so you can do it like:
(Assuming that you want to check when your EditText got focused)
editText.onFocusChangeListener = this
even if you are in an Adapter or ViewHolder while you are implementing OnFocusChangeListener you can assign that corresponding class to that Context you wanna be supervised in case of focus changes.
I'm trying to implement a custom keyboard in Android. I want to input some text in my Webview. Since I don't want to display the keyboard when I hit everywhere in the Webview I was thinking of a listener of something like this:
webview.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if (((WebView) v).getHitTestResult().getType() == EDIT_TEXT_TYPE)
showMyKeyboardHere();
return false;
}
});
is my listener, it seems to return EDIT_TEXT_TYPE on the key press I do AFTER I've pressed the edit text field. So when I press the edit text field it returns UNKNOWN_TYPE and the click after that when pressed somewhere else returns EDIT_TEXT_TYPE. My guess would be that the OnTouchListener happens before the touch even gets sent on to the Webview it won't have registered it. Any way to change that?
Now to my question: Since I will only want my custom keyboard in this application, is there anyway to listen to whatever event is called to bring up the normal keyboard and immediately hide it and show my own?
I have a simple edittext that I've added to my layout. I added a simple OnKeyListener as follows in the onCreate method:
final EditText simpleEditText = (EditText) findViewById(R.id.editText1);
simpleEditText.setOnKeyListener(new OnKeyListener() {
#Override
public boolean onKey(View arg0, int arg1, KeyEvent arg2) {
System.out.println("HELLO");
return false;
}
});
When the application starts, everything works correctly. For every key pressed, HELLO is printed twice - once for DOWN action and once for UP action.
Now when I click on the edittext with my mouse, the edittext no longer runs the code in my onKey method. What am I doing wrong? I'd still like to receive key events after the user has clicked in the edittext. I don't care about the mouse click, I'm only using that to reposition the cursor. After the cursor is repositioned, the key events are no longer registered.
Please see the attached image to see more information.
Thanks
Zamil
http://i.stack.imgur.com/j2DP7.jpg
As per android API, onKey works only for the physical key that was pressed. You may need to consider OnClickListener for mouse clicks. Here is API documentation.
Click events are not key events. You won't (and shouldn't) receive onKey events for anything except key events.