I am trying to add an onClick on the soft keyboard for an activity. The reason why is that i want to check if the user is currently active. So what i have done is that if the user clicks on the app i will reset a inactivity timer. The problem is that when a user interacts with the soft keyboard it doesn't call the function onUserInteraction()
which is a function I override in the activity. So i need help to find a way to keep track if the soft keyboard has been clicked for every textfield etc I have in the activity. (I know that i can insert a onclick listerner on every EditText field but i rather not do that, because if I would use many EditText fields it would not be so nice)

- 41
- 6
-
Can you be little elaborate. What does currently active mean - if the app is open or if user is performing actions on the screen? – Praga Apr 06 '17 at 09:08
-
@Praga It means that the user is performing actions on the screen. – Linussjo Apr 06 '17 at 09:10
-
Then you need not rely on the keyboard actions. You can go with Faa km's answer. – Praga Apr 06 '17 at 09:12
-
@Praga well, the problem is when i write on the soft keyboard it does not call the `onKeyUp()`. (I overrode the method in my activity) – Linussjo Apr 06 '17 at 09:14
4 Answers
So this is what i ended up with. I was hoping for something else, but this solves the problem. Thanks for the help!
public class ActivityEditText extends android.support.v7.widget.AppCompatEditText {
private TextWatcher tw;
public ActivityEditText(Context c)
{
super(c);
this.setOurTCL();
}
public ActivityEditText(Context context, AttributeSet attrs) {
super(context, attrs);
this.setOurTCL();
}
public ActivityEditText(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.setOurTCL();
}
private void setOurTCL()
{
this.tw = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
InactivityManager.resetTime();
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
}
};
this.addTextChangedListener(this.tw);
}
@Override
public void removeTextChangedListener(TextWatcher watcher) {
if(!watcher.equals(this.tw))
super.removeTextChangedListener(watcher);
}
}

- 41
- 6
To handle an individual key press, implement onKeyDown() or onKeyUp() as appropriate. Usually, you should use onKeyUp() if you want to be sure that you receive only one event. If the user presses and holds the button, then onKeyDown() is called multiple times.

- 2,539
- 3
- 18
- 26
-
Tried to implementing it on the activity. But it is not called when interacting with the soft keyboard – Linussjo Apr 06 '17 at 09:11
-
keyboard is an different window. If you need not track what the user is typing, you can consider adding a global textWatcher to every edittext and listen for text change which will be called every time user types. – Praga Apr 06 '17 at 09:14
-
Yes, I'm aware of this solution to the problem. I was though hoping for a solution where i could implement in the activity and not for every edittext. But if there is no such solution i will have to go back to that one. – Linussjo Apr 06 '17 at 09:18
-
Let your activity implement the textWatcher interface and set "this" as listener for each edit text, so that you need not implement for each views. Or you can write your own subclass of edit text as suggested by @suru – Praga Apr 06 '17 at 09:34
Create a class some thing like UserInteractionEditText
and extend EditText
and set the onclick lisetener in that class use that class in all the XML layouts as you use EditText
you can do some thing like this:
public class UserInteractionEditText extends EditText implements View.OnClickListener {
public UserInteractionEditText(Context context) {
super(context);
}
public UserInteractionEditText(Context context, AttributeSet attrs) {
super(context, attrs);
}
public UserInteractionEditText(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public void onClick(View v) {
//TODO:: Handle user Click Events
}
}

- 697
- 8
- 20
You can use onUserInteraction()
function of activity. You need to override this function in your activity.
This function get calls when you perform any kind of interaction with your activity.
@Override
public void onUserInteraction() {
super.onUserInteraction();
// Your code goes here
}
You can refer this example and also this answer, refer the docs here
Hope this helps.

- 1
- 1

- 3,765
- 8
- 41
- 56
-
As I said in the description, I've already override that method but it doesn't seem to respond to the soft keyboards interactions. – Linussjo Apr 06 '17 at 09:05
-
Instead of doing for all the edittext, you can extends the edittext to create customedittext and add textwatcher in it or handle onclick to check your inactivity timer – Ichigo Kurosaki Apr 06 '17 at 09:24