I have a Fragment which has an EditText. I would like to override the behavior of the delete key in the EditText, but only for certain words. The logic of whether to override the delete event is held within the Fragment, and the EditText does not have access to the Fragment.
Example:
A user is typing: "Hello, my name is Adam and I am 23." The cursor is set immediately following "Adam", and the user taps the delete key. "Adam" is one of my keywords that should not be deleted, so I want to instead move the cursor to just before the word, and cancel the delete event.
Here is what I have tried so far:
OnKeyListener. This only works for hardware keyboards, and I need a solution for both virtual and hardware keyboards.
OnEditorActionListener. This does not trigger on a delete press.
Override the onCreateInputConnection method, with one of my creation which overrides the sendKeyEvent method. I can't do this because its creation must be inside the EditText (needs super access), but the logic of what words should not be deleted resides in the Fragment (which the EditText does not have access to). I cannot pass in the logic from the Fragment to the EditText because it is constantly changing.
TextWatcher. This successfully triggers before, while, and after text has been deleted, but cannot stop the deletion.
Any ideas that I have not yet tried would be much appreciated!
Related
I have an empty activity with just the PlainText on it.
I want it so that after the user taps on the button that directs him to the empty activity it automatically selects/focuses on the PlainText bringing out the built-in android keyboard ready to get user's input.
I tried
android:focusedByDefault="true"
but that doesn't seem to do anything after going to the empty activity.
You can try to do
editText.requestFocus();
on your empty activity's onCreate() method
Set Focus on EditText
See here.
Note: Normally the general problem is "hot to remove auto focus" from EditText so it focuses automatically, so maybe you changed some attributes from your xml, please share your empty activity xml with us.
You can try to do
editText.setFocusableInTouchMstrong textode(true);
editText.setFocusable(true);
editText.requestFocus();
on your empty activity's onCreate() method
so I have recyclerview populated with data from database. Each row contains two buttons one for play and second for stop time. So what I'm trying is to save state of buttons inside recyclerview. And I used
PreferenceManager.getDefaultSharedPreferences(context).edit().
putInt(Constants.numberOfbutton, getAdapterPosition()).apply();
to save particular position. I have inserted that line of code inside button click and I checked it's saving correct position. What I'm trying to achieve is, when user enters the app after closing if he lefts any of the buttons in play mode to restore that state and continue timer. But problem is when user enters the app all buttons are running. I'm using chronometer for presentation of the time. I think here is the problem in this method:
private void startTime(int position ) {
Data data = datas.get(position);
chronometer.setBase(SystemClock.elapsedRealtime() + Long.parseLong(data.getTime()));
chronometer.start();
}
UPDATE EXPLANATION:
So I can load from database for each item of recyclerview it's time, date, name etc. But I can't figure it out how to play chronometer of just particular item when user enters the app again. NOTE AGAIN: I'm performing correct saving getAdapterAtPosition and loading data from PreferenceManager. I've checked that. Even I tried putting the number of one of rows but same issue appears.
Any help very appreciated. Thanks.
I have found the answer. I save the boolean in database actually I used 1 or 0 than parse that to true or false acordingly is button play pressed or not. Thank you all for your comments and trying to help.
Try to use handler or alarm service instead of chronometer
We are trying to build android activity that listen to any key press (either character or command) something like custom EditText.
Is there something like keyboard hook in Windows OS available on Android?
Is it possible to listen for all key pressed for all controls (EditText and other controls)?
Could this be achieved by an activity that runs in the background?
EDIT
As for security, we want to get keyboard events only for our app activity, ex: when our activity is shown and focused.
The Activity's onKeyDown / up / etc method will get called if no View consumes the event.
When Views do consume the events, you will need either:
a custom version of each relevant class that forwards the key presses up to the activity.
add a text change listener or key listener to all TextViews in the view hierarchy (could use a recursive method to step through the entire hierarchy)
You can't listen for EditText keys from background process, as it is bounded to Keyboard activity that manipulate that text. It will be security valuation as it will not only allow you to listen to keys in your own application but for all others, including sensitive data like user passwords.
Handling Keyboard Actions
http://developer.android.com/training/keyboard-input/commands.html
An event listener is an interface in the View class that contains a single callback method. These methods will be called by the Android framework when the View to which the listener has been registered is triggered by user interaction with the item in the UI.
http://developer.android.com/guide/topics/ui/ui-events.html#EventListeners
Android soft keyboard does not create any events so there is no good way of listening for keyevents ... you can use add a TextWatcher ( http://developer.android.com/reference/android/text/TextWatcher.html ) to a hidden edittext.
http://developer.android.com/reference/android/hardware/input/InputManager.html
As per documentation this has a nested class
Nested Classes
interface -- InputManager.InputDeviceListener -- Listens for changes in input devices.
Just a lead, not sure about the usage though
I have numerous edittexts inside a listview. I want to type in each of the edittexts one by one without having to scroll through the listview. How can I do that?
Use the android:nextFocusDown param to specify which field you'd like to get focus when the user hits the enter key.
I'm currently trying to develop an android application in eclipse(java) which shows some jokes downloaded from a database. The user is able to vote on each joke once, and to make sure they only do that once, I have made a table in the database that contains three columns.
E-mail
Username (Used when a user publishes a joke)
Encrypted Password
I have two "screens" right now:
Login screen
Main screen
At every single start up the application checks the SharedPreferences for a file containing some information, and if there is some information it should load the Main screen, but if there is no account information the Login screen should be loaded.
Any idea on how I can use different screens, and how should it be coded?
Two options:
In your Activity, check if there is account info. If there isn't then setContentView to the Log in screen. Otherwise, setContentView to your other content. If you go this route, you'll have to have the logic of both the login Activity and the other in the same Activity. Shouldn't be too bad if the logic is relatively uncomplicated.
Have two activities. The default Activity can be the login Activity, but in onCreate() you can check if the info already exists and if it does, simply start the other Activity right away and return from onCreate(). Otherwise, continue with setContentView, etc.
You can have a single top layout (e.g. FrameLayout) that contains two overlapping layouts (one for the login and one for the main). Use a single activity. When the activity starts do setContentView() to the top layout. Then define a method selectScreen(boolean isMain) that based on the argument sets the main layout on and the login off (or vice versa). You turn screens on/off using the setVisibility() method in class View. You can switch screen any time by calling that method. If you want to be extra fancy you can use standard animations when flipping screens.