I have a 7x6 grid of EditText views. I want all of them disabled when the application starts, ie they should behave like normal TextViews and not to be editable. Then the user taps one cell in the grid, it changes its background and performs something visual. If the user clicks on the cell one more time it should allow editing. I'm struggling with OnClick() and OnFocusChange() listeners, but I can't accomplish such a basic interaction.
Playing with setEnabled() and setFocusable() doesn't help. I wonder why even a simple task like this has been made so difficult on Android
I finally found a solution. It's a matter of calling
setFocusableInTouchMode(boolean)
setFocusable(boolean)
when the EditText is first created, so it can intercept the clicks. Then one can set those flags back again to make the EditText editable, request the focus, and manually show/hide the soft keyboard with InputMethodManager methods
Try using this setFocusableOnTouch() instead of setFocusable() method.
Firstly write these line in your xml in EditText:
android:enabled="false"
And than use the code in java as shown below:
Boolean check = true;
yourEditText.setEnabled(check);
check=!check;
Setting input type to null is not enough, since it only suppress soft keyboard and if device has hardware keyboard, there will be input. So in order to suppress any editing you should do following:
editText.setInputType(InputType.TYPE_NULL);
editText.setFilters(new InputFilter[]
{
new InputFilter()
{
public CharSequence filter(CharSequence src, int start,
int end, Spanned dst, int dstart, int dend)
{
return src.length() < 1 ? dst.subSequence(dstart, dend) : "";
}
}
});
That will guarantee that EditText content won't be changed
Since you are using gridview to achieve your concern you can do the following.
setOnItemClicklistener on gridview
Extend Edittext to make your own edittext view
The extedned class will contain boolean property named editable using this property in onItemclicklisterner of gridviewyou can call setEditable or setFocusabel or both for a editetext.
If you share your code i can elaborate more on this issue.
According the Android guide line please use LongKeyPress for the Question you have" If the user clicks on the cell one more time it should allow editing"
You can do the follwoing:
If you want to make edittext not editable then use following method
edittext.setInputtype(Null);
If you want to make edittext editable then use the same method and set the proper inputype
visit the following link for more info
Related
I'm trying to user TextWatcher interface in order to detect which EditText was changed.
I have an activity with 10 EditTexts, and it looks weird to use 10 TextWatchers for each one of them.
There is any way to use only one TextWatcher and to use switch statement on the Editable in the functions afterTextChanged?
What I would do is to create a class that extends EditText and create a TextWatcher in that class. You can then implement those EditTexts in your XML or create them programmatically in Java with the TextWatcher listening for each EditText.
Don't know if this will work for you but you can give it a try.
I have never tried this before, but it should work if you check if the EditText is in focus. There are a few ways to go about this and the most straightforward one is to check the focus of the EditText inside your TextWatcher methods. You'll need to do something like this:
if(mEdit1.hasFocus()) {
...
} else if(mEdit2.hasFocus()) {
...
} else if(mEdit3.hasFocus()) {
...
}
A different approach would be to use an OnGlobalFocusChangeListener on your root view and set a variable indicating with EditText currently has focus. It would still require a lot of if statements to check for which EditText has the focus, but may be a more reusable solution.
I have an EditText that the user can write in, when the app starts there is already a string in the EditText. When the user clicks the EditText it becomes focused and the curser is where the user clicked the EditText text box.
I know that the code for setting the curser to the start is :
editText.setSelection(0);
But I don't know where to put this code, I tried to it in beforeTextChanged but it didn't do the job.
You can do this by setting an putting an OnFocusChangedListener. You'd do something like this:
et.setOnFocusChangeListener(new View.OnFocusChangeListener(){
public void onFocusChange(View view, boolean hasFocus){
if(hasFocus){
((EditText)view).setSelection(0);
}
}
});
Where et is the text edit you want to set the listener on.
Full-discolsure: haven't tried this code out myself.
While there is probably a way to do this, I'm not entirely sure it's the best user experience, because when the user taps a text box at a specific spot, they really expect the cursor to be there. Imagine for instance if the user sees "abcd" written there and wants to edit that to "abcde", so they figure "I'll just tap at the end and append an 'e'". Imagine the user's frustration when that doesn't work as expected.
If you expect the user to edit the textbox, I'd consider leaving it empty. If you are using the existing text as a hint ("email#example.com"), it's probably a better idea to indicate that in some other way.
I am working on a dialog at Android with a few EditTexts.
I've put this line at the onCreate() in order to disable the soft keyboard:
Keypad.this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
The problem is that it works only when the dialog appear and doing nothing.
When I move to the next EditText, the keyboard appears and not going down.
Does anybody have an idea how to solve this issue?
If you take look on onCheckIsTextEditor() method implementation (in TextView), it looks like this:
#Override
public boolean onCheckIsTextEditor() {
return mInputType != EditorInfo.TYPE_NULL;
}
This means you don't have to subclass, you can just:
((EditText) findViewById(R.id.editText1)).setInputType(InputType.TYPE_NULL);
I tried setting android:inputType="none" in layout xml but it didn't work for me, so I did it programmatically.
create your own class that extends EditText and override the onCheckIsTextEditor():
public class NoImeEditText extends EditText {
public NoImeEditText(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
public boolean onCheckIsTextEditor() {
return false;
}
}
Try this out..
edittext.setInputType(InputType.TYPE_NULL);
if (android.os.Build.VERSION.SDK_INT >= 11)
{
edittext.setRawInputType(InputType.TYPE_CLASS_TEXT);
edittext.setTextIsSelectable(true);
}
I have been looking for solutions to this all day, and I came across this approach. I'm putting it here because it seems to answer this question perfectly.
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.
Its been some time since this post, but here is a simple method which worked for me: in the xml, in the EditText properties, do: android:focusable="false". Now the keyboard will not popup even if the user clicks on it. This is useful if you are providing your own keypad.
Call TextView.setShowSoftInputOnFocus(false). This method is documented since API level 21, but it's already there since API level 16 (it's just hidden from JavaDoc). I use it with API level 16 in an AlertDialog in combination with dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN).
In API level 14, there is a hidden method setSoftInputShownOnFocus() which seems to have the same purpose, but I have not tested that.
The advantage over InputType.TYPE_NULL is, that all the normal input types can be used (e.g. for password input) and the touch event positions the cursor at the correct spot within the text.
Put this in Oncreate Method
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
Know its too late, but have you tried the following setting to EditText in your layout ?
android:inputType="none"
UPDATE
Use,
editText.setInputType(InputType.TYPE_NULL)
editText.setFocusable(false)
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(edSearch.getWindowToken(), 0);
in order to disable ANDROID SOFT INPUT KEYBOARD xml file doesn't help in my case
calling the setInputType method on EditText object in java file works great.
here is the code.
EditTextInputObj = (EditText) findViewById(R.id.EditTextInput);
EditTextInputObj.setInputType(InputType.TYPE_NULL);
If you put the textViews in the view group you can make make the view get the focus before any of its descendants by using this:
view.setDescendantFocusability(view.FOCUS_BLOCK_DESCENDANTS);
by set EditText focusable->false, keyboard will not opened when clicked
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false" />
You can do that:
textView.setOnClickListener(null);
It will disable the keyboard to the textView and the click will not work anymore.
My question is: How can I update the display of the action button of the soft keyboard on the fly?
Another post seems to be about changing the editor action BEFORE the keyboard is shown: Android: Can't figure how to use setImeActionLabel. I would like to change the soft keyboard action button WHILE it is shown.
I have partially succeeded:
- I can change the action by using: editText.setImeOptions(EditorInfo.IME_ACTION_GO);
- I can also redraw the keyboard using: InputMethodManager.restartInput(editText);
I do this using TextWatcher.afterTextChanged.
- However, I have a problem with this. If I press a key, on top of that the keyboard shows which key has been pressed. But when I call restartInput(...), this also hides the view that shows which key has been pressed. This seems normal behaviour to me, but I need a way around it.
Extending EditText and overriding onKeyUp is not a good idea, see: https://groups.google.com/forum/?fromgroups#!topic/android-developers/RIxGfx5qOjM.
The KeyboardView class has exactly what I need (invalidateKey), but I think that is only accessible when you create your own input method.
Anybody else have some better ideas?
Please try to be more specific when you describe what do you want to do ... I needed to read your post many times to be able to understand your request ..
So what you want to do is how to react to any key pressed on keyboard and show info about it in action key on keyboard.
If that is not your target plz to explain more...
If yes, so I guess you would need to use additionaL EditText ( not visible ) lets call et0. after finishing inserting a letter or any pattern you define through ontextchanged in your edit text, change the focus to et0.. so you can change the actionKey.. and then send the focus back to your editText .. so you can thread or timertask, or any way to manage this temporary changing of focus into and from et0 ..
here its not necessary to restart the input.
hope that will help you ..
Have you tried changing /system/usr/keychars/default.kcm on-the-fly before invoking your redraw code? (filename might be different depending on version of Android).
There, you are allowed to set not only key output characters, but also key display values. I haven't tried it, it might also incur a delay, but it's worth a try.
This should be very useful for you.
EditText in = new EditText(this);
in.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence cs, int s, int b, int c) {
Log.i("Key:", cs.toString());
}
public void afterTextChanged(Editable editable) {}
public void beforeTextChanged(CharSequence cs, int i, int j, int k) {}
});
I have 2 EditTexts; 01 and 02. My button will be disabled once the activity is started and when these two EditText contain text, the button has to be enabled again. However my button is always disabled and can't enable it using button.setEnabled(true);.
Can anyone help me with this?
summit.setEnabled(false);
buttonEnable();
public void buttonEnable(){
if (feedback.length()>0 && email.length()>0){
summit.setEnabled(true);
}else{
summit.setEnabled(false);
}
}
You're correct about needing a TextWatcher. The afterTextChanged(Editable) method is the one you're interested in for something like this. Call your buttonEnable() method from it, and add the TextWatcher to any applicable text fields. (Looks like feedback and email from your sample.)
One easy way can also be to set onKeyListener to your editText(), then if there is something in editText(), set button enable if nothing disable it.