Android: Disable soft keyboard at all EditTexts - android

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.

Related

Making a calculator app w/ buttons and TextView with blinking cursor and NO keyboard focusability. Is it possible without using Canvas to draw cursor?

There is the proper method which is to draw the line myself via canvas. I want to avoid the complexity and I am asking here to double check if there is a simpler method I can use.
I am currently using a temporary method, which is to use SpannableString to highlight the nearest text 'character' that the cursor is on. But as you know, this "selects" the current character. I prefer the cursor to be between two characters instead of on top of one character.
I also don't want keyboard focus because I already laid out some nice buttons for the user to interact with. I don't want the app to use the Android keyboard and input methods. Only my buttons should be enough.
I tried accessing TextView methods:
Remove keyboard focus and input via the manifest
setFocusable(false)
setFocusableInTouchMode(false)
setCursorVisible(true) or cursorVisible="true"
This didn't work for me. I know that if I make the TextView focusable, then the cursor will be visible. But if it's not focusable, then the cursor does not show, even if it it is: cursorVisible="true".
What should I do?
Solution:
Disable input method of EditText but keep cursor blinking
Maybe try leaving out the xml attribute android:editable entirely
and then try the following in combination to
keep the cursor blinking and prevent touch events from popping up
a native IME(keyboard)..
/*customized edittext class
* for being typed in by private-to-your-app custom keyboard.
* borrowed from poster at http://stackoverflow.com/questions/4131448/android-how-to-turn-off-ime-for-an-edittext
*/
public class EditTextEx extends EditText {
public EditTextEx(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
public boolean onCheckIsTextEditor() {
return false; //for some reason False leads to cursor never blinking or being visible even if setCursorVisible(true) was
called in code.
}
}
Step 2 change the above method to say return true;
Step 3 Add another method to above class.
#Override public boolean isTextSelectable(){ return true; }
Step 4 In the other location where the instance of this class has been
instantiated and called viewB I added a new touch event handler
viewB.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent event) {
viewB.setCursorVisible(true);
return false;
} });
Step 5 Check to make sure XML and or EditText instantiation code
declares IME/keyboard type to be 'none'. I didnt confirm relevance,
but Im also using the focusable attributes below.
<questionably.maybe.too.longofa.packagename.EditTextEx
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:focusable="true"
android:focusableInTouchMode="true"
android:inputType="none">
Sorry for so many xml attributes. My code uses them all, testing in
4.2.1, and has results. Hope this helps.

EditText methods in java android

Hello I'm new to android developing.
Is there a method in java that equals to #.gotFocus?
Is there in java an events list that I can watch and select like in c# visual studio?
I tried to do #.Focus or something similar but had no success.
I want to reproduce the following scheme:
1- EditText has a certain hint => "Enter a value"
2- The user clicks the edit text and the hint disappears => ""
3- The user fills a certain value => "certain value"
Thank's for helpers :)
Ron Yamin, If I understand your doubt correctly what you want is:
1- Have a field of text for the user to type words/numbers etc --> It is called EditText in android
2- Have an hint so the user knows what to type --> Eg. "Type your name"
3- And react to focus in some way.
The first one you will achieve either through XML or by code. If you have a main.xml in your layouts folder (assuming you are using eclipse/android studio to develop), you can use the interface to drag an edit text to the android screen.
The second one you will achieve still through the XML. If you right click on it, right side of the screen there will be a little window called Proprieties that you can change things like height and width and a hint. Type there your hint.
Finally the last one you need to go to your code in .java and get a reference of your edit text (findViewById).
Either through setOnClickListener or setOnFocusChangeListener.
More info you can checkout here:
http://developer.android.com/guide/topics/ui/controls/text.html
I have googled a tutorial you can check with more detailed information and step by step guide.
Hope it helps:
http://examples.javacodegeeks.com/android/core/widget/edittext/android-edittext-example/
It seems that you changed your question quite a bit, and my C# ignorance got the best of me.
It seems that what you really want is an EditText, the example text you are looking for is the hint.
You can set the hint in the xml file or by code with .setHint(string) method.
Here's where to start:http://developer.android.com/guide/topics/ui/controls/text.html
edit 3 - events in android are dealt with by using listeners. You can use an onClickListener to achieve what you want.
textView.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(){
//dostuff
}
}
Assuming your textfield is an instance of EditText (which it probably should be), you can do the following:
textfield.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus){
// this is where you would put your equivalent #.gotFocus logic
}
}
});
It's worth noting that the behavior you've described can be achieved by using textfield.setHint. The hint is text that is cleared automatically when the user selects the EditText. It's designed specifically for the case you describe, e.g. textfield.setHint("Enter a Value")
I'm not familiar with c# but I'm guessing you want event fired when edittext get focus. Try this
EditText txtEdit= (EditText) findViewById(R.id.edittxt);
txtEdit.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus){
// do the job here when edittext get focus
}
}
});

some EditTexts with only one TextWatcher

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.

EditText: how to enable/disable input?

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

Android: How to enable my button back if EditText is not empty?

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.

Categories

Resources