Android: How to record keystroke? - android

I want to make a smart keyboard that can learn and save new words from user. I already made note and keyboard separately, the problem is :
how to read all keystrokes and write it to my note in background?
how to save my note automatically?
thanks for your help

Keep a String or StringBuilder that stores all the text that the user types. All text sent through your soft keyboard will have to pass through the onKey method.
So, I'd do something like this:
1) In onKey, check to make sure primaryCode (the keycode that was pressed) is a letter/number/apostrophe using the corresponding functions. So, something like
Character.isDefined(primaryCode)
2) Concatenate primaryCode onto the end of your StringBuilder/String.
You'll also have to deal with the user moving the cursor/backspacing. In my keyboard, I only store the most recent two words (resetting this whenever the user moves the cursor). That way, the keyboard can learn what the most likely word is given the last word.
You can save your "note" using an ObjectOutputStream or (if it's fairly small) using sharedPreferences.
Send me an email if you run into an more issues: I've been writing a soft keyboard for a while so I'm pretty familiar with it.

Related

Is there any ways to know which language user is typing in EditText ? or Restrict user to type in English only

Any one have idea of getting language of user, who is typing in the EditText.
What I Have Tried ?
Please do not suggest Google's com.google.mlkit , I have already tried but not working when user types fast.
I have also tried setting up android:digits="All Alfabets", It is not working when I long press and paste from the ClipBoard, It is allowing text from the other language.
This seems like a very complex problem! And one that limiting the allowed characters won't solve - many non-English languages use the same Latin character set as English, or use it for a romanised version of their written language. nihongo o kaite imasu is Japanese, but that would pass an alphabet check!
Even where other characters are used (e.g. accented versions) it's not unusual for people to just drop them and use the "standard English" characters when typing, especially if they're being informal - e.g. Spanish uses accents on question words like ¿qué?, but people might just not bother (and skip the ¿ too, or just say k if they're being really informal)
And then there's the fact that English does use accented characters - someone can be naïve or blasé, but you don't want your app to tell people they're "not typing in English" if they write those things.
I don't know anything about mlkit but if it's capable of detecting language to some decent degree, it really might be the way to go for such a complex human problem. I'd suggest that instead of trying to interfere with the user's typing, you just trigger a check when they're done which validates what they've entered. If it looks ok, you can enable a button or whatever - if not, show an error message and make them fix it themselves.
You could do that kind of thing with a TextWatcher (or the doAfterTextChanged extension function that comes with the ktx-core AndroidX library) - you'd probably want to start a delayed task so it happens a moment after they stop typing, and that you can interrupt if they start typing again
val languageCheck = Runnable {
// do your check here, enable buttons / show errors as a result
}
// set up the checker
textView.doAfterTextChanged {
// cancel an existing delayed task
textView.removeCallbacks(languageCheck)
// schedule a new one
textView.postDelayed(languageCheck, delayMillis)
}

Programming in Android Studio to able the user to give three numbers and save them

in android stuido I would like to code an activity, where the user can input numbers. For, example he types the number to the textfield, click 'OK' button, then the textfield gets clear, he types the second number, than the third, and after they give the third number the program goes to another activity and sayst thanks for the free number. I would like to save the three numbers for further use and have them in an ascending order. How should I do it?
I agree with Andrii, this is a very vague and general question. To get you pointed in the right direction though, you would want a layout with an number based-editText widget (for the user input). I would then add the button, and then implement OnClickListener for the button so that everytime the button is pressed, it calls a method you will define that will store the value in an array or list (which can be sorted), along with some kind of tracker to keep track of how many numbers have been entered - then clearing the editText field so that another number can be input; on the third number, the method calls the activity via intent or some other way saying "thanks for the free number".
This is all general and it is going to take quite a bit of work and API Guide/DeveloperDocs searching on the Android web site.

Trouble creating a "speed dial" button on Android

I'm making a very simple launcher for Android. I'm having trouble getting some speed dial buttons to work.
The idea is that when the user presses the speed dial buttons for the first time, they'll be prompted to enter a phone number with a popup window (dialog? I think?). That number will then be assigned to that button, which will then call the assigned number when the button is next pressed. It's not great if they want to reassign a number, but this is just something rough that i'd like to get out of the way. I'm thinking of creating an int to reflect the state of the button (0 = no number assigned, 1 = number is assigned), and using if statements to either bring up the window or call the number.
I don't know how to bring up such a window (although I do know I can bring up a dial pad using (android:inputType="phone")), as well as how to pass the number that the user inputted to an int/long. I'm thinking I can assign the value to an int, although that might not be the most optimal data type. I have a rough idea on how to dial a number once it's given.
What should I do? I'm quite new to programming, so i'm having trouble with this.
may be this could help.
have a long click listener to the buttons.
on long click event, show a dialog widow which accepts the number.
store it in a persistent memory, either db, files or pref utils by android.
in on click button, have a logic to retrieve the stored content and update the views accordingly.

Android - how to create custom word dictionary for only my application?

I have successfully added a word to Android's predefined user dictionary, but i want to create a custom dictionary which can only be accessed by my application.
For example: When i type "lol", the suggestions show me "laugh out loud" but when I want another meaning of "lol" then I can manually add another meaning of "lol" (eg, "xxxxxx" - so the next time the user writes "lol" in an EditText, the suggestions will show him "xxxxxx").
No other application should have access my dictionary data.
I have worked with spell checker class but it gives me only correct word and I can't my own word meanings.
Please give me some suggestions or links.
There is an Androids inbuilt class UserDictionary so that you can add your spells programmatically into that, Here is a link you can go through with , and which may relates to your problem .(In this solution nidhi has manged programmatically to retrieve data back)
Add word to user dictionary and retrieve them back from dictionary
In addition you can go through following links to :
http://developer.android.com/reference/android/provider/UserDictionary.html
http://developer.android.com/reference/android/provider/UserDictionary.Words.html
Moreover if you want a custom personal dictionary then manually you have to store words and retrieve them back .
Hope that helps !!!

input connection-how to delete selected text?

I made a custom keyboard for android. When I press backspace button of my keyboard I use
getCurrentInputConnection().deleteSurroundingText(1, 0);
to delete one letter from the input field. But when I select some text and then press the backspace button, the selected text is not deleted. What method in input connection should I use so that selected text is also deleted from my keyboard when I press the backspace button?
When deleting you need to take into account the following situations:
There is a composing selection.
The editor/user has a cursor selection on the text.
There is no selection of any kind.
If there is a selection then it should be deleted. If there is no selection then the character in front of the cursor should be deleted.
Solution 1
At first I used this method. I like it because it only uses the input connection.
CharSequence selectedText = inputConnection.getSelectedText(0);
if (TextUtils.isEmpty(selectedText)) {
// no selection, so delete previous character
inputConnection.deleteSurroundingText(1, 0);
} else {
// delete the selection
inputConnection.commitText("", 1);
}
As long as the input connection is using the default BaseInputConnection.deleteSurroundingText method, this should be fine. However, it should be noted that the documentation warns
IME authors: please be careful not to delete only half of a surrogate
pair. Also take care not to delete more characters than are in the
editor, as that may have ill effects on the application.
If some custom view is using an input connection that doesn't correctly check for for text length or surrogate pairs, then this could cause a crash. Even though this is an unlikely senario, if you use this solution, then you should add that extra checking code here.
This may be why the sample Android keyboard first checks if there is a composing span, and if there isn't then it using the following solution.
Solution 2
You can also use the input connection to send a KeyEvent with KEYCODE_DEL. In my opinion this is not as good because it is a soft keyboard masquerading as a hard keyboard. But many keyboards do this. When I was making a custom view that accepted keyboard input, I had to handle deletes as a KeyEvent, that is, independently of the input connection (because the input connection was not deleting the text).
So just send the delete message as a KeyEvent (as if you were pressing a hard keyboard key down and then letting it up).
getCurrentInputConnection().sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN,KeyEvent.KEYCODE_DEL));
getCurrentInputConnection().sendKeyEvent(new KeyEvent(KeyEvent.ACTION_UP,KeyEvent.KEYCODE_DEL));
This works as expected. It will delete the selection if there is one, or delete one character behind the cursor if there isn't a selection. (However, you should handle any composing span separately.)
Thanks to this answer for the idea.
Call getCurrentInputConnection().commitText("",1);

Categories

Resources