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());
});
}
Related
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
Hi I'm making custom dialer so I create my own input pad.
The problem is how do I disable the EditText but still allow cut/copy/paste? The stock dialer can do this.
I have tried android:focusable="false" but it disables cut/copy (can still paste though).
I also tried to disable the inputType programatically which disables all three commands:
myEditText.setInputType(InputType.TYPE_NULL); //Can't cut/copy/paste
Disabling it from manifest also doesn't work:
android:configChanges="orientation|keyboardHidden" //Keyboard still popped up
Any solution? Thanks
After hours and hours of research, I finally found a solution that works for all API versions. Hope this saves someone's time.
If you are developing for API >= 11, the solution is simple, either:
1) Add the two properties below in the xml file of EditText
android:inputType="none"
android:textIsSelectable="true"
or
2) Programatically do the below
myEditText.setInputType(InputType.TYPE_NULL);
myEditText.setTextIsSelectable(true);
And you're done.
If you want to cater for API < 11 as well, I found that there is no way to disable to keyboard from popping out if you wanted to select the text for copy paste purpose. Setting focusable to false will disable the keyboard but it doesn't help because it disables your ability to select text too. Any other solutions I found in stackoverflow all either doesn't work or disables text selection at the same time too.
One ugly way to solve this is as such..
First, add this property in the xml file of EditText
android:editable="false"
Yes this is deprecated, but necessary for making the EditText not editable in API version < 11.
Next, we will need to hide the keyboard as soon as it shows up, so that we can continue selecting text without the keyboard blocking the way.
Use this code below to detect keyboard showing up (solution obtained from https://stackoverflow.com/a/9108219/1241783), and hide it immediately.
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.HONEYCOMB)
{
final View activityRootView = findViewById(R.id.activityRoot);
activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
Rect r = new Rect();
//r will be populated with the coordinates of your view that area still visible.
activityRootView.getWindowVisibleDisplayFrame(r);
int heightDiff = activityRootView.getRootView().getHeight() - (r.bottom - r.top);
if (heightDiff > 100) { // if more than 100 pixels, its probably a keyboard...
//Hide the keyboard instantly!
if (getCurrentFocus() != null)
{
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
}
}
}
});
}
It works for my case. Though you can see the keyboard showing up in a split second (which is the ugly part) but I can't think of any other way to get this to work at the time of writing. If you have a better solution, please leave a comment!
Let me know too if this saves someone's time :)
To disable the soft keyboard showing, keeping the copy/paste and cursor functionality, just add this line in your activity:
getWindow().setFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM,
WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
Since the current top answer uses a deprecated method and didn't have the paste method for me, here's another way that doesn't use old methods. But, it does try to use a hidden method via reflection with a fallback. =)
I've subclassed EditText into a new widget called KeyboardlessEditText that still retains all the cool editing features without the keyboard showing. Just drop the file in and go.
The full code is a little long for this post, but as long as GitHub doesn't go down, then this will work: https://github.com/danialgoodwin/android-widget-keyboardless-edittext/blob/master/KeyboardlessEditText2.java
To disable system keyboard automatic pop up for EditText or TextView do the following:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
editTextView.setShowSoftInputOnFocus(false);
} else {
editTextView.setTextIsSelectable(true);
//N.B. Accepting the case when non editable text will be selectable
}
I had the same problem but later I also wanted allow typing after double tap.. after hours and hours of searching I found working solution (at least for me). Use this in your onCreate method:
editText.setCursorVisible(false);
editText.setTextIsSelectable(true);
editText.setShowSoftInputOnFocus(false);
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN); // This just hide keyboard when activity starts
These lines should definitely do the trick.. and if you want to revert that use this:
editText.setCursorVisible(true);
editText.setShowSoftInputOnFocus(true);
To show keyboard again use:
private void showSoftKeyboard(View view) {
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
view.requestFocus();
inputMethodManager.showSoftInput(view, 0);
}
To allow copy/paste next time just use these three lines:
editText.setCursorVisible(false);
editText.setTextIsSelectable(true);
editText.setShowSoftInputOnFocus(false);
For further keyboard hide use:
private void hideSoftKeyboard() {
if(getCurrentFocus() != null) {
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
}
}
This code is working on API >= 21
try this
EditText et = ... // your EditText
et.setKeyListener(null) //makes the EditText non-editable so, it acts like a TextView.
No need to subclass. The main difference between this and making your EditText non-focusable, is that the EditText still has its own cursor - you can select text, etc. All it does is suppress the IME from popping up its own soft keyboard.
Had a similar need due to my custom inline "fake" input which was still visible as the os soft keypad was appearing after focus moved to an edit text.
Solution was to make the edit text hide soft input until the previous custom input widget had finished its edit lifecycle.
Used #Bruce's answer for inspiration, also saw a few related posts which I'll attach at end.
Solution I found worked was:
fun setInputType(inputType: Int) {
getEditText().setRawInputType(inputType)
if (inputType == InputType.TYPE_NULL) {
getEditText().setTextIsSelectable(true)
getEditText().isCursorVisible = true
}
}
had to use setRawInputType() instead as multiline text input was not respected when setting from InputType.TYPE_NULL back to InputType.TYPE_TEXT_FLAG_MULTI_LINE.
Seems there are users reporting issues relating to calling setInputType(InputType.TYPE_NULL). see:
https://issuetracker.google.com/issues/36907992
other useful related posts:
How to make EditText not editable through XML in Android?
EditText non editable
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).
i wanted to create an EditText with an android:inputType="textPassword. However i also do not want to use the SoftKeyboard for input.
So i tried setting InputMethod to null, but this would also disable the textPassword features of replacing password rendering to "*[lastchar]".
Is there any other way to disable the softKeyboard from showing up?
Uhm just figured it out myself:
Simply use:
pinInput.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
InputMethodManager mgr = (InputMethodManager) Pin.this.getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
});
This works well for me. I feared it might show the SoftKeyboard and then hide it again instantly, or flicker the screen, but does nothing like it.
In my application I want to display a list of stuff and provide the user with the ability to filter the list by using the soft keyboard. To that end I added a button that should trigger (hide/show) the soft keyboard for filtering. I don't want to have a visible edit text control, cause it would take up unnecessary space. Rather than that, I would like to display a toast showing the filter query as the user types, much as the 'android:textFilterEnabled' attribute for ListView does. To my understanding there is no obvious way of doing this with available Android components. So I tried the following approach:
1) creating a layout containing invisible edit text and the list view:
<ListView android:id="#+id/main_list"
android:drawSelectorOnTop="false"
android:layout_height="0px"
android:layout_width="fill_parent"
android:layout_weight="5"
/>
2) adding button as a popup and invoking InputMethodManager on click to toggle the soft input (called in onCreate):
private void initButton() {
Button buttonView = (Button) getLayoutInflater().inflate(R.layout.keyboard_button, null);
buttonView.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
final View target = findViewById(R.id.filterbox);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
// this does not work...
// imm.toggleSoftInputFromWindow(target.getWindowToken(), InputMethodManager.SHOW_FORCED, 0);
// ... so need to track this in an instance variable - which sucks
if (imeShowing) {
imm.hideSoftInputFromWindow(target.getWindowToken(), 0);
imeShowing = false;
} else {
// check that the filterbox got focus
Preconditions.checkState(target.requestFocusFromTouch());
Preconditions.checkState(target.hasWindowFocus());
Preconditions.checkState(target.hasFocus());
imm.showSoftInput(target, 0);
imeShowing = true;
}
}
});
buttonPopup = new PopupWindow(buttonView);
// ... code to display the button as a small popup
}
As mentioned in the code sample, the 'obvious' approach (calling toggleSoftInput) does not work, so I had to revert to this ugly if-else. This is however, a secondary problem. The primary problem is that when I run this in the emulator, the soft keyboard is displayed correctly, but as soon as I start typing in it, the systems starts an intent to the google search activity! And the typed characters appear in the Google search box displayed as a result. What is even more weird, this only happens the first time I type after deploying and running the app. I.e. if I go back to my app from the Google search box, everything works as expected (no redirects to the search box). Before showing the display I make sure that the invisible edittext gets focus, so it should be the target of the soft keyboard, right??
Does anyone have any idea what is happening here?
Sorry this is me (the author of this question), I can't access my old account due to SO new 'awesome' OpenID login:/
So I figured out that what I really need to do is just turn on the 'android:textFilterEnabled' attribute of the list, and focus on it on the button click. ListView supports input from soft keyboard (although I figured it out by looking at the actual code of AbsListView rather than getting hints in any documentation:). Also, I managed to get PopupWindow working for the button (instead of Dialog that I tried previously) so the focus problem is gone. The working code for this (in case anyone has similar problem) below:
View popupView = getLayoutInflater().inflate(R.layout.keyboard_button, null);
Button buttonView = (Button) popupView.findViewById(R.id.keyboardButton);
buttonView.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
final View list = findViewById(R.id.the_list);
list.requestFocus();
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
}
});
buttonPopup = new PopupWindow(this);
The list will handle input from soft keyboard just as it does from hard one. The trick is just to trigger the keyboard and focus on the list. Also you may control how the filtering is applied to the list by setting a custom QueryFilterProvider on its associated adapter.