Show soft keyboard (numeric) using AlertDialog.Builder - android

I have an EditText in an AlertDialog, but when it pops up, I have to click on the text box before the keyboard pops up. This EditText is declared in the XML layout as "number", so when the EditText is clicked, a numeric keypad pops up. I want to eliminate this extra tap and have the numeric keypad pop up when the AlertDialog is loaded.
All of the other solutions I found involve using
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
This is NOT an acceptable solution, as this results in the standard keyboard rather than the numeric keyboard popping up. Does anyone have a way to make the numeric keyboard pop up in an AlertDialog, preferably while keeping my layout defined in XML?
AlertDialog dialog = new AlertDialog.Builder(this)
.setTitle("Mark Runs")
.setView(markRunsView)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
EditText runs = (EditText)markRunsView.findViewById(R.id.runs_marked);
int numRuns = Integer.parseInt(runs.getText().toString());
// ...
})
.setNegativeButton("Cancel", null)
.show();
Edit: I want to be perfectly clear that my layout already has:
android:inputType="number"
android:numeric="integer"
I also tried this:
//...
.setNegativeButton("Cancel", null)
.create();
EditText runs = (EditText)markRunsView.findViewById(R.id.runs_marked);
runs.setInputType(InputType.TYPE_CLASS_NUMBER);
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
dialog.show();
but that also did not work. With the setSoftInputMode line, I still get the full keyboard when the AlertDialog loads; without it, I still get nothing. In either case, tapping on the text box will bring up the numeric keypad.
Edit Again:
This is the XML for the EditText
<EditText
android:id="#+id/runs_marked"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="4dip"
android:inputType="number"
android:numeric="integer">
<requestFocus/>
</EditText>

Coming a bit late, but today I had this same problem. This is how I solved it:
Call the keyboard when the Dialog opens just like you did:
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
Now, into the Dialog I'm assuming you have a EditText field that accepts only numbers. Just request focus on that field and the standard keybord will automatically transform into the numeric keyboard:
final EditText valueView = (EditText) dialogView.findViewById(R.id.editText);
valueView.requestFocus();
Now you just have to remember to dismiss the keyboard once you're done with the Dialog. Just put this in the positive/negative/neutral button click listener:
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(valueView.getWindowToken(), 0);

Just try to set the InputType by using setInputType().
EditText runs = (EditText)markRunsView.findViewById(R.id.runs_marked);
runs.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL);

I think that you should keep on using the setSoftInputMethod hack but also provide hints to android that you want a numeric input.
Using xml layout attributes
For this, you can use several xml attributes to your EditText xml definition (see android:inputType for available options)
Examples:
<EditText android:inputType="phone" ...
<EditText android:inputType="number" ...
<EditText android:inputType="numberSigned" ...
<EditText android:inputType="numberDecimal" ...
You can also both hint android to show digital keyboard and restrict input to acceptable characters with android:numeric
Examples:
<EditText android:numeric="integer" ...
<EditText android:numeric="signed" ...
<EditText android:numeric="decimal" ...
Programatically
Use EditText.setRawInputType(int) with constants such as TYPE_CLASS_NUMBER you will find in android:inputType
or TextView.setKeyListener(new NumberKeyListener())
EDIT
AlertDialog focus by default on the positive button. It seems that it is what is causing trouble here. You may look at this similar question and its answer.

Try to specify in layout xml for your edit box:
<EditText ...>
<requestFocus/>
</EditText>

Related

Show/Hidden Soft Keyboard event in Android

I need to get the Show/Hidden event from the SoftKeyboard in Android.
I did a research, but nothing worked.
I wanted that 'cause we're working with a tablet 5.0'' with low resolution, so when you edit a EditText, the keyboard rise in full screen, and then or you press the "enter or next" key, or you press the back button to hide the keyboard... I need to update some fields with the new value, but I can't use the TextWatcher 'cause have some business logics on what's the right fields to update, and 'cause I just want to update when the user really finish the input.
And the onFocusChanged isn't a option, 'cause we don't want our customers needing to cliking in the next field, and hiding the keyboard to see the new values.
I don't want to override the onTouchEvent too, see if where the user cliked isn't the same field that he is editing.
Sorry for the specific problem and more specific solution that I'm asking for.
And sorry for my bad English :D
Use tree view observer to detect any change in view height and that time you can check whether keyboard state
final View activityRootView = findViewById(R.id.chat_pane_root_view);
activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener()
{
#Override
public void onGlobalLayout()
{
InputMethodManager mgr = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
if(mgr.isAcceptingText())
{
//keyboard is up
}
}
});
First of all in your declairation of your activity in manifest file declaire like this
<activity android:name="Map" android:windowSoftInputMode="stateHidden">
from this property your keyboard is being hidden,
after this you are declaire the imeOption property in your main.xml file edittext like this
<EditText
android:id="#+id/edttexttitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:hint="#string/tasktitle"
android:imeOptions="actionNext"
android:inputType="text"
android:textSize="#dimen/font_size_medium"
android:textColor="#color/darkgray1" />
this code is for your first edittext if more then one and if only one edittext then your need to change the imeOption of edittext like
android:imeOptions="actionGo"

Android Display Numeric Keypad on Button Click

In my application, I am trying to display the numeric keypad when the user clicks on a button.
When the button is clicked, I shift the focus to the EditText in my layout using requestFocus() and next I need to display the numeric keypad so that the user can type in the values..
The values will always be numeric and hence I need to show only the numeric keypad.
I tired using this inside my button's onClick() method but it does not work.
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
Please provide me with any solution to this.
Also, my application is for an Android tablet supporting 4.0.3.
This one in your EditText property
android:inputType="phone" (This will displayed phone numeric keypad)
or
android:inputType="number" (This will displayed numeric keypad)
Now, you have to just set Focus on your EditText on Button's click..
something like,
edtNumber = (EditText) findViewById(R.id.number);
// Button's onClick....
#Override
public void onClick(DialogInterface dialog, int which)
{
edtNumber.requestFocus();
}
in EditText put below line.
android:inputType="number"

Android show input keyboard only when user press a button

I have a PopupWindow in one Activity.
When user press on the list item in the activity, the window will popup for getting input from users.
There are some EditText in the window. And also I provided some buttons that preset some text on it, so when user press on it then it will enter to the edittext.
I can disable the softkeyboard when the window first popup. But when I change the focus on edittext (Move from one edittext to another edittext), the keyboard shown up.
I want the softkeyboard show only when user press on the "show keyboard" button in the popup window
How can I do it?
Updated:
public void onFocusChange(View view, boolean hasFocus) {
if (hasFocus) {
selectedEditText = (EditText)view;
String text = selectedEditText.getText().toString();
selectedEditText.setSelection(text.length());
InputMethodManager inputManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(selectedEditText.getWindowToken(), 0);
}
}
I tried the code above but it still showing.
set onFocusChangedListener() on every editText you have and disable Keyboard Pop inside it if it has focus..
Show it only when the user Presses the button..
There is very simple solution too; create fake focus above your EditText and you good to go.
Like this:
<LinearLayout
android:layout_width="0dp"
android:layout_height="0dp"
android:focusable="true"
android:focusableInTouchMode="true">
</LinearLayout>
<!--Your EditText-->
<EditText
...
</EditText>
also you can add these two line above your mentioned EditText in any VIEW, like TextView and so on.
android:focusable="true"
android:focusableInTouchMode="true"
Go to the xml and find the activity in which you want to hide keyboard
add given attribute to the Activity..
android:windowSoftInputMode="stateAlwaysHidden"

How can I get a "done" button in softkeyboard?

how can I have a "done" button in my softkeyboard (Samsung Galaxy 10.1, Android 3.1) when writing in an EditText?
Using
<EditText
android:id="#+id/comment"
android:layout_width="772dp"
android:layout_height="200dp"/>
I get
If possible, I'd also like to remove this "attachment" button.
Anybody can help?
EDIT
I managed to get a "Done" button using
android:inputType="textImeMultiLine",
but the "return" button disappeared...
How can I have both? (I asked this new question here).
add this to your EditText xml:
android:imeOptions="actionDone"
or, to set it from code:
yourEditText.setImeOptions(EditorInfo.IME_ACTION_DONE);
for more, read this
Using my Galaxy S2 phone
For the code below, each EditText will have a Return button that adds a new line:
EditText editText = new EditText(this);
For the code below, each EditText will have a Next button that navigates to the next field and the last one will have Done button that will dismiss the keyboard:
EditText editText = new EditText(this);
editText.setInputType(InputType.TYPE_CLASS_TEXT);
For the code below, no change, each EditText has a Return button:
EditText editText = new EditText(this);
editText.setImeOptions(EditorInfo.IME_ACTION_DONE);
For the code below, all EditText will have a Done button and all will dismiss the keyboard.
EditText editText = new EditText(this);
editText.setInputType(InputType.TYPE_CLASS_TEXT);
editText.setImeOptions(EditorInfo.IME_ACTION_DONE);
For layouts use code below:
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="text"
android:imeOptions="actionDone"/>
In my Intel x86 Emulator at least, the "Done" key appears only if you specify the input type: "phone", "number", "text", "textPassword", ... with android:inputType. If you don't specify any or you set "textMultiLine", "Done" does not appear.
android:imeOptions="actionDone"
and
editText.setImeOptions(EditorInfo.IME_ACTION_DONE);
seem useless, since they don't change anything either in the first case (where "Done" appears anyway) or in the in the second case (since "Done" keeps not appearing) !
Add the next code to your EditText in xml
android:imeOptions="actionDone"
android:imeActionLabel="#string/done"
android:singleLine="true"
The android:inputType="text" field is optional
Use TextView.setImeOptions and pass it actionDone.

Disabling of EditText in Android

In my application, I have an EditText that the user only has Read access not Write access.
In code I set android:enabled="false".
Although the background of EditText changed to dark, when I click on it the keyboard pops up and I can change the text.
What should I set to disable EditText?
I believe the correct would be to set android:editable="false".
And if you wonder why my link point to the attributes of TextView, you the answer is because EditText inherits from TextView:
EditText is a thin veneer over
TextView that configures itself to be
editable.
Update:
As mentioned in the comments below, editable is deprecated (since API level 3). You should instead be using inputType (with the value none).
use EditText.setFocusable(false) to disable editing
EditText.setFocusableInTouchMode(true) to enable editing;
You can try the following method :
private void disableEditText(EditText editText) {
editText.setFocusable(false);
editText.setEnabled(false);
editText.setCursorVisible(false);
editText.setKeyListener(null);
editText.setBackgroundColor(Color.TRANSPARENT);
}
Enabled EditText :
Disabled EditText :
It works for me and hope it helps you.
Use this to disable user input
android:focusable="false"
android:editable="false" This method is deprecated one.
For disable edit EditText, I think we can use focusable OR enable but
Using android:enabled=... or editText.setEnabled(...)
It also changes the text color in EditText to gray.
When clicked it have no effect
Using android:focusable=... or editText.setFocusable(false) - editText.setFocusableInTouchMode(true)
It doesn't change text color of EditText
When clicked it highlights the EditText bottom line for about few millisecond
Output
As android:editable="false" deprecated
In xml
Use android:enabled="false" it's simple. Why use more code?
If you want in java class you can also use this programmatically
editText.setEnabled(false);
As android:editable="false" is depricated.You can use InputType TYPE_NULL on EditText
use like this :
editText.setInputType(InputType.TYPE_NULL);
Simply:
editText.setEnabled(false);
To disable the functionality of an EditText, just use:
EditText.setInputType(InputType.TYPE_NULL);
in case you want to enable it some way, then you can use:
EditText.setInputType(InputType.TYPE_CLASS_TEXT);
I use google newly released Material Design Library. In my case, it works when I use
android:focusable="false" and
android:cursorVisible="false"
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/to_time_input_layout"
app:endIconMode="custom"
app:endIconDrawable="#drawable/ic_clock"
app:endIconContentDescription="ToTime"
app:endIconTint="#color/colorAccent"
style="#style/OutlinedEditTextStyle"
android:hint="To Time">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/to_time_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="false"
android:cursorVisible="false" />
</com.google.android.material.textfield.TextInputLayout>
Set below properties in class:
editText.setFocusable(false);
editText.setEnabled(false);
It will work smoothly as you required.
This will make your edittext disabled.
editText.setEnabled(false);
And by using this
editText.setInputType(InputType.TYPE_NULL);
Will just make your Edittext not show your softkeyboard, but if it is connected to a physical keyboard, it will let you type.
Disable = FOCUS+CLICK+CURSOR
Disabling focus, click, and cursor visibility does the trick for me.
Here is the code in XML
<EditText
android:id="#+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="false"
android:cursorVisible="false"
android:clickable="false"
/>
if you use android:editable="false", eclipse will remind you this message "android:editable is deprecated: Use inputType instead".
So, I use android:focusable="false" instead, it worked well for me.
android:editable="false"
is now deprecated and use
YourEditText.setInputType(InputType.TYPE_NULL);
Using android:editable="false" is Depracted. Instead you'll need to Use android:focusable="false"
Use TextView instead.
In my case I needed my EditText to scroll text if no. of lines exceed maxLines when its disabled. This implementation worked perfectly for me.
private void setIsChatEditTextEditable(boolean value)
{
if(value)
{
mEdittext.setCursorVisible(true);
mEdittext.setSelection(chat_edittext.length());
// use new EditText(getApplicationContext()).getKeyListener()) if required below
mEdittext.setKeyListener(new AppCompatEditText(getApplicationContext()).getKeyListener());
}
else
{
mEdittext.setCursorVisible(false);
mEdittext.setKeyListener(null);
}
}
Try this one, works fine for me:
public class CustomEdittext extends EditText {
Boolean mIsTextEditor=true;
public CustomEdittext(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
#Override
public boolean onCheckIsTextEditor() {
// TODO Auto-generated method stub
return mIsTextEditor;
}
#Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
mIsTextEditor=false;
Boolean mOnTouchEvent=super.onTouchEvent(event);
mIsTextEditor=true;
return mOnTouchEvent;
} }
Note: You need to add this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
on your activity or else keyboard will popup at first time.
As some answer mention it, if you disable the editText he become gray and if you set focusable false the cursor is displaying.
If you would like to do it only with xml this did the trick
<YourFloatLabel
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/view_ads_search_select"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:clickable="true"/>
</YourFloatLabel>
I simply add a FrameLayout appear above the editText and set it focusable and clickable so the editText can't be click.
This works for me:
android:focusable="false"
From #Asymptote's comment on the accepted answer, use:
myEditText.setEnabled(false);
myEditText.setInputType(InputType.TYPE_NULL);
...and Bob's your uncle.
Today I still use editable="false", but also with focusable="false".
I think the case we need to make an EditText un-editable, is because we want to keep its EditText style (with that underline, with hint, etc), but it accepts other inputs instead of text. For example a dropdown list.
In such use case, we need to have the EditText clickable (thus enabled="false" is not suitable). Setting focusable="false" do this trick, however, I can still long hold on the EditText and paste my own text onto it from clipboard. Depending on your code and handling this can even crash your app.
So I also used editable="false" and now everything is great, except the warning.
you can use android:focusable="false" but also need to disable cursor otherwise
copy/paste function would still work.
so, use
android:focusable="false"
android:cursorVisible="false"
There are multiple was how to achieve multiple levels of disabled.
editText.setShowSoftInputOnFocus(false); and editText.setFocusable
prevents the EditText from showing keyboard - writing in some text. But cursor is still visible and user can paste in some text.
editText.setCursorVisible(false)
hides the cursor. Not sure why would you want to do that tho. User can input text & paste.
editText.setKeyListener(null)
I find this way most convenient. There is no way how user can input text, but widget still works with OnClickListener if you want to trigger action when user touches it
editText.setEnabled(false);
completely disables EditText. It is literally 'read-only', user cannot input any text in it and (for example) OnClickListener doesn't work with it.
TextEdit documentation
Set this in your XML code, It works.
android:focusableInTouchMode="false"

Categories

Resources