I'm trying to get Android to select all the text in an EditText field when it gets the focus. I'm using this attribute in the layout (on both fields):
android:selectAllOnFocus="true"
I'm not sure if this is relevant, but to get the cursor to the first editable field (there's also a disabled field before it), I'm using the following commands:
quizStartNum.setFocusable(true);
quizStartNum.requestFocus();
But, while the cursor does move to the desired field when the layout is first displayed, the text doesn't get highlighted; instead the cursor ends up to the left of the text, the default behavior. If I move to the second field by touching it, all the text is selected as desired. Then, if I move back to the first field, again by touching it, the text is also completely selected. I would like to have the behavior right from the start. Is there a way to do this?
If android:selectAllOnFocus="true" does not work, try calling setSelectAllOnFocus(true) on that particular EditText.
If that doesn't work either, this is another workaround from a previous SO post.
EditText dummy = ...
dummy.setOnFocusChangedListener(new OnFocusChangedListener(){
public void onFocusChange(View v, boolean hasFocus){
if (hasFocus) && (isDummyText())
((EditText)v).selectAll();
}
});
I had a similar issue and android:selectAllOnFocus="true" did not work for me.
The reason was that i was programatically requesting focus to the EditText before it was displayed. So if you are doing this for a EditText in an AlertDialog make sure you show it before you request focus to the EditText.
EditText should be focussed after it displayed.
Following workout worked for me,
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
// Set Input Methode
getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
}
#Override
public void onStart() {
super.onStart();
new Handler().post(new Runnable() {
#Override
public void run() {
Dialog dialog = getDialog();
if (dialog != null) {
// Set Layout
dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
// Set Cancelable False
setCancelable(false);
// Set Focus Here <------------------------
uiET_myTextView.requestFocus();
}
}
});
}
Try removing your focus commands. They aren't necessary, Android should focus on the first field automatically?
I also noticed that with android:selectAllOnFocus="true" seemed not to work, I used the mouse to select EditText in the emulator, but if I used my finger and touched it, as if on the phone, it worked. If you don't have a touch screen on your computer you may have to install your app on a physical device to test it.
Android Studio may not recognize the mouse in this case
I'll refer to this older post.
If you just want your EditText to show a hint (for "what goes in here"), you might want to use the Android's XML-attribute hint (link).
Related
I am using EditText's to accept an OTP, where user has focus on next EditText once he enters a digit to a field and so. It works fine on all devices. But on devices running android OS P i.e. API 28, requestFocus() does not work, and user is not able to enter digits to consecutive EditTexts as focus doesn't move automatically.
Here is the code - by default all EditText's are disable to prevent from opening system keyboard. I am using my own CustomKeybaord to accept numbers. However it works except Android P.
mEtCode1.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
Log.d("BEFORE_", charSequence.toString());
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
hideError(charSequence.toString());
if (!charSequence.toString().isEmpty()) {
mEtCode2.requestFocus();
mEtCode1.setBackground(getResources().getDrawable(R.drawable.verify_code_edit_text_background));
mEtCode2.setBackground(getResources().getDrawable(R.drawable.verify_code_edit_text_background));
mEtCode1.setEnabled(false);
}
}
#Override
public void afterTextChanged(Editable editable) {
}
});
Please help me with this
Thank you, in advance
I had the same issue with my OTP screen on devices with Android P sdk. The problem was that i set the height and width of the editText to 0dp, which is focus disabling in android P,
as described in Android Developer page in the Android P change log:
android-9.0-changes-28#ui-changes
Views with 0 area (either a width or a height is 0) are no longer focusable.
This is issue with Android P. And what worked for me is the following code block, So sharing here:
enterOtpTextFrame.postDelayed(Runnable {
enterOtpTextFrame.requestFocus()
}, 100
)
we require to call requestFocus with postDelayed with some small time amount. In my case it is 100 millisecond.
Here is the official documentation for requestFocus method. It states there that it only works if the desired view for which you want to have focus is enabled, has size, is visible, is focusable and is FocusableInTouchMode.
I had exact same issue. I was using databinding to set the enable state of my EditText. I realised that requestFocus was not working because databinding, due to some unknown reasons, was not enabling my textview in time.
Here is my code:
/*
setMyEditTextEnabled is my method to which my view is binded i.e.
android:enabled="#{vm.myEditTextEnabled, default=false}"
This worked for all version except Android P because it has
some timing issues with API 28 (not sure what)
*/
//binding.getVm().setMyEditTextEnabled(true);
/*
So to make it work, I am enabling my EditText directly and
it works for all versions.
*/
binding.myEditText.setEnabled(true);
binding.myEditText.requestFocus();
Also, as mentioned in following post: EditText requestFocus not working
Do set focusable and focusableInTouchMode to true as well.
In short, my point is to make sure that your edit text is fulfilling all requirements as mentioned in official doc in order to requestFocus to work.
Encountered a similar problem when popping an alert dialog. In my case, postDelayed'ing a focus request or forcing the soft keyboard to pop up didn't work. Even if I could manage to pop up the keyboard, the focus stayed on an EditText in the main activity; needless to say I have tried clearing its focus and even disabling it.
However, popping the alertDialog delayed did the trick:
final AlertDialog alertDialog = alertDialogBuilder.create(); //a builder of your own
final Runnable r = new Runnable() {
public void run() {
alertDialog.show();
}
};
editTextOnMainActivity.postDelayed(r, 100);
On one of the screens on my app I have a sliding bottom panel, and when it slides up I disable an EditText on the screen.
I disable it with
totalText.setEnabled(false);
totalText.setClickable(false);
totalText.setFocusable(false);
and reenable it with
totalText.setEnabled(true);
totalText.setClickable(true);
totalText.setFocusable(true);
But after being reenabled the edit text still does not work. When I press it, it just blinks for a second like a keyboard is going to pop up, and nothing happens. My first thought was that maybe totalText.setEnabled(false); set the input type to null, but I tried replacing the setEnabled lines to setInputType and the issue remains. Anyone run into this issue before?
Use setFocusableInTouchMode and setFocusable, both of them.
EditText.setEnabled(!EditText.isEnabled()) will enable and disable the EditText
Full cone is given below
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText etInput = findViewById(R.id.et_input);
Button btnAction = findViewById(R.id.btn_action);
btnAction.setOnClickListener(view -> {
etInput.setEnabled(!etInput.isEnabled());
});
}
Is there a way I can change the suggestions the keyboard shows for autocompletion of a word? I want to maintain a separate dictionary in the app and when the user types in the EditText he should be shown suggestions from that dictionary.
For changing the suggestions, you will have to implement your own keyboard. This is not what you want to do I believe.
The simplest option for you is to use AutoCompleteTextView for showing user the suggestions. Looks something like the following:
After reading several posts, I understood it can not be done without implementing my own keyboard. So, I ended up implementing a layout for displaying suggestions over the keyboard.(i.e. at the bottom of the activity view). When the keyboard shows up it automatically comes over the keyboard.
First, I turned off the default suggestions in the EditText using
edittext.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
Then for detecting if the keyboard is displaying or not:
myActivityView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
int heightDiff = myActivityView.getRootView().getHeight() - myActivityView.getHeight();
if (heightDiff > 180) {
// if more than 180 then its a keyboard
showSuggestions();
}else{
//keyboard gone...
// hide suggestion layout
}
}
});
I added a 100ms delay so that the keyboard hides its default suggestion layout, in case it is displaying.
private void showSuggestions(){
myActivityView.postDelayed(new Runnable() {
#Override
public void run() {
mySuggestionLayout.setVisibility(View.VISIBLE);
}
}, 100);
}
There is a tutorial on the Android Development website, which explains this. See the link here: http://developer.android.com/guide/topics/search/adding-custom-suggestions.html
in my application EditText is there which is disable and I want to implement long press option on this edittext(while disable mode) which enable it and allowing to enter the character from softkey.
Example:-
Suppose initially I am allowing user to enter some number into the EditText. After some operation, I need to disable this EditText. Again if user want to change the number which previously he enter in the editText then first he need to long press on this editText.
After doing long press on this editText, editText get enable and again user will able to change or retype the number. I need to do some operation before changing the number in the editText and during operation user not have any option to change the number in the editText.
Code:-
<EditText
android:id="#+id/eTextBillNoFrmReturn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="2dp"
android:layout_weight="7"
android:clickable="true"
android:background="#drawable/custom_edit_text"
android:inputType="number" />
#Override
public boolean onLongClick(View v) {
// do some operation
return false;
}
but this code work only EditText is enable. Long press not work when EditText is disable.
Try this one.
#Override
public boolean onLongClick(View v) {
// do some operation
btnname.setenable(true);
btnname.setfocusable(true);
return false;
}
In case anyone stumbles across this old post like I did, I thought I would post an answer since I found some settings that worked well for my situation.
The OP was asking for a way to make the EditText function as if it is disabled yet still allow setting a long-click listener that can get called.
Set the edit text to be NOT focusable and set the input type to none. The input type to none will make it so that a cursor doesn't try to show up when clicking on it, so it looks a bit nicer than the cursor that flashes for a second with just the focusable set to false.
To do this in the XML:
<EditText
android:id="#+id/txtYourEditTextID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:inputType="none" />
To do this in the code:
EditText txtYourEditText = (EditText)findViewById(R.id.txtYourEditTextID);
txtYourEditText.setFocusable(false);
txtYourEditText.setInputType(InputType.TYPE_NULL);
To do what OP is wanting, you will implement a long-click listener:
txtYourEditText.setOnLongClickListener(new View.OnLongClickListener {
#Override
public boolean onLongClick(View v) {
((EditText)v).setInputType(InputType.TYPE_CLASS_NUMBER);
v.setFocusable(true);
return true; //or return false if you do not want to consume the event
}
});
Then on whatever the action is that you want to disable it again, call
txtYourEditText.setFocusable(false);
txtYourEditText.setInputType(InputType.TYPE_NULL);
Hopefully this will help someone who is trying to do something similar.
Have you tried to replace
return false;
by
return true;
on your onLongClick(View v) method ?
Use the toggle button which will suite perfectly your case
Toggle Button
What I'm looking to do is hide only the popups that show what key you are currently pressing while using a soft keyboard. Is this possible? I am creating my own new keyboard which will have no need for them.
From what I think I understand, the picture below is the actual popup keyboard that you can choose to show using android:popupKeyboard and android:popupCharacters in the Keyboard.Key XML.
But the image below is not the same (also see this picture). Is there a way to turn the following off, using XML or even programmatically?
After reading a little bit of the actual android keyboard source code:
What I was referring to was the "key preview", which is "a popup that shows a magnified version of the depressed key." By default the preview is enabled, but to disable it, simply enough, is setPreviewEnabled(boolean previewEnabled). Which is a method from the KeyboardView class. API.
public void onPress(int primaryCode) {
mInputView.setPreviewEnabled(false);
}
public void onRelease(int primaryCode) {
mInputView.setPreviewEnabled(true); //Change to false if you want remove too for the Del key when it's pressed
}
Additionally, To get the view and universally disable the preview from within a custom class extending InputMethodService
private KeyboardView mInputView;
#Override
public KeyboardView onCreateInputView() {
mInputView = (KeyboardView)getLayoutInflater().inflate(R.layout.keyboard, null);
mInputView .setPreviewEnabled(false);
return mInputView;
}