I have an edittext, and when the user clicks this edittext I want to show an alertdialog.
My code is the following :
edt.setInputType(InputType.TYPE_NULL);
edt.setFocusableInTouchMode(true);
edt.requestFocus();
edt.setCursorVisible(false);
edt.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
CommentDialog.buildDialog(mContext, identifier, false, edt.getId());
}
});
I don't want the keyboard to show up when the user clicks the edittext, so I set the inputtype to TYPE_NULL.
But when the edittext doesn't have focus and I click it, the onClick event isn't executed. When I click it a second time, the alertdialog shows up correctly.
How do I fix this?
Simply try to add this to your XML file. Your keyboard pops up when widget gains focus.
So to prevent this behaviour set focusable to false. Then normal use OnClickListener.
<EditText
android:focusable="false"
...
/>
Now, it should works.
You can use onTouch instead of onClick, so it doesn't matter if the EditText has focus or not.
edt.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
CommentDialog.buildDialog(mContext, identifier, false, edt.getId());
return false;
}
});
Nothing much to do you just have to
edt.setFocusable(false);
If focusableInTouchMode is true then touch is triggered in second touch only, so unless you want that case use false for focusableInTouchMode. and if you want to enable the focusability in the view set focusable true
<EditText android:focusable="true" android:focusableInTouchMode="false" ... />
make your alert dialog box appear on
setOnFocusChangedListener()
You should add onFocusChangeListener:
edt.setKeyListener(null);
edt.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus)
{
edt.callOnClick();
}
}
});
edt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
CommentDialog.buildDialog(mContext, identifier, false, edt.getId());
}
});
Avoid using a FocusChangeListener since it will behave erratically when you don't really need it (eg. when you enter an activity). Just set an OnTouchListener along with your OnClickListener like this:
#Override
public boolean onTouch(View view, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
view.requestFocus();
break;
}
return false;
}
This will cause your EditText to receive focus before your onClick call.
Instead of setting input type use "Editable=false" and "Focus=false" if you don't require keyboard.
It maybe helpful to you.
This was a real problem for me when trying to reproduce a "click" sound from the EditText when the soft keyboard pops up; I was only getting a click every second time. What fixed it for me was the the opposite of what worked for #neaGaze. This worked for me in my_layout.xml :
<EditText android:focusable="true" android:focusableInTouchMode="true" ... />
It allows the click sound/event to happen each time when user enters the EditText, while also allowing the soft keyboard to show. You have to handle the OnClickListener of course for this to happen, even if you do nothing with it, like so :
myEditText = (EditText) findViewById(R.id.myEditText);
...
// implement the onClick listener so we get the click sound and event if needed
myEditText.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
//do something or nothing; up to you
}
});
Speaking of that pesky soft keyboard, if I finished from my Dialog style Activity with the soft keyboard up, no matter what I tried the keyboard remained up when I was returned to MainActivity. I had tried all the usual suggestions such as Close/Hide the Android soft keyboard , How to close Android soft keyboard programmatically etc. None of that worked.
In my case I did not need the soft keyboard in MainActivity. What did work was the following in my AndroidManifest.xml file, within the MainActivity section
<activity
android:name=".MainActivity"
android:windowSoftInputMode="stateAlwaysHidden">
</activity>
Related
I am an iOS developer, and I have been given task to fix this.
After digging and googling, I have no idea what to do.
We have this screen:
This screen is Activity with custom Fragment, containing ListView fed by ArrayAdapter.
The adapter produces views by getView it seems.
Every row has seven EditText controls.
Problem 1:
When screen is shown first time, and you tap on any EditText, nothing happens.
Why?
When you tap second time, and any subsequent time, focus works, keyboard appears, and you can type.
Problem 2:
This is hard to describe. On some devices, tapping any EditText causes keyboards to switch between some numeric and alphanumeric types, and this goes on and on and then stops.
Why?
After trying various stuff:
getExtractedText on inactive InputConnection warning on android
onClick event is not triggering | Android
Android EditText doesn't show the Keyboard
...
I have no idea what is the problem.
According to one of SO posts, you should not have both onClick and onTouch, yet here they are.
Why?
editText.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
view.requestFocus();
break;
}
return false;
}
});
editText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
((EditText) v).selectAll();
}
});
editText.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_ENTER) {
if (event.getAction() == KeyEvent.ACTION_UP) {
InputMethodManager imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
}
return false;
}
});
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
double amount = Double.parseDouble(((EditText)v).getText().toString());
DecimalFormat df = new DecimalFormat("0.0");
String result = df.format(amount);
((EditText)v).setText(result);
}
}
});
}
I am still digging, commenting out various code, but I am in dark, please help.
Whole code: https://gist.github.com/MartinBergerDX/473c47b008b6b0570466794f221eca31
Update 1:
I have removed all listeners, onTouch, onFocus, and text watchers.
And this is still happening:
Update 2:
It seems this is related to keyboard covering ListView. Not sure why, but when I set less than 10 items in data source, keyboard does not cover content and ping pong effect does not happen.
I am not sure if you have xml for this view or you are generating these edittext programatically if you have xml then for each edit text do android:inputType="number" if you are generating these editext programatically then do this for each edittext.setInputType(InputType.TYPE_NUMBER_FLAG_SIGNED | InputType.TYPE_CLASS_NUMBER); with this only number key willl be shown for user input next just add addTextChangedListener to each editText
editText.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
//handle your changes here
}
#Override
public void afterTextChanged(Editable s) {
}
});
Update
Android ListView with EditText loses focus when the keyboard displays The issue is when you have a ListView with input capable fields that displays the soft keyboard on focus, the EditText loses its focus for the first time but the second time it just works fine. The reason it happens is all the views are getting rendered again, so the EditText field object in the item row that used to be focused is now a completely different object. android:descendantFocusability="beforeDescendants" in your ListView and android:windowSoftInputMode="adjustPan" for your activity in the app Manifest
<ListView android:id="#+id/productList" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_alignLeft="#+id/search"
android:layout_below="#+id/search"
android:padding="5dp"
android:descendantFocusability="beforeDescendants"/>
<activity android:name=".myActivity"
android:label="#string/app_name" android:screenOrientation="sensorPortrait" android:windowSoftInputMode="adjustPan"/>
Reference https://www.mysamplecode.com/2013/02/android-edittext-listview-loses-focus.html
maybe this is useful for you:
How can I detect a click in an onTouch listener?
Try deleting onClick and using onTouch only, wish it helps.
I have an EditText with three toggle buttons beneath it.
I want to keep the focus on the EditText AND have the keyboard stay visible when I tap on any of the three toggles. i.e. I do not want the keyboard to hide when the focus is outside the EditText (I should not see the keyboard hide then reopen).
I've tried the following to no avail:
toggleButton.setOnFocusChangeListener(new View.OnFocusChangeListener()
{
#Override
public void onFocusChange(View v, boolean hasFocus)
{
editText.requestFocus();
// This doesn't fully work.
// Focus is on editText but keyboard still hides when I
// tap on the toggle button.
}
});
The EditText and ToggleButtons are in a fragment, and the parent activity has this configuration in the AndroidManifest.
<activity
android:name=".activities.MyActivity"
android:label="#string/m_activity"
android:theme="#style/AppTheme.NoActionBar"
android:windowSoftInputMode="stateHidden|adjustResize" />
What is the best way to fix this issue?
I think you should do that for your yourEditText, using OnFocusChangeListener
yourEditText.setOnFocusChangeListener(new View.OnFocusChangeListener()
{
#Override
public void onFocusChange(View v, boolean hasFocus)
{
yourEditText.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(yourEditText, InputMethodManager.SHOW_IMPLICIT);
}
});
This means that, you will request focus whenever it is changed for you yourEditText and you will also show keyboard.
You can use the LayoutParms.Example is given below
1.Hide the keyboard
this.getWindow().setSoftInputMode
(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
2.Show the Keyboard
this.getWindow().setSoftInputMode
(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
If you want to Know the other options. Refer the below link.
https://developer.android.com/reference/android/view/WindowManager.LayoutParams.
html
I've got two EditText fields and a button,
EditText topNumericInputField; // Top text field
EditText bottomNumericInputField; // Bottom text field
Button clearButton; // Regular Button
I'm trying to clear the text that's in them when I change focus, my onClick method looks like this:
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.topInputField:
clearTextBoxes();
break;
case R.id.bottomNumericInputField:
clearTextBoxes();
break;
case R.id.clearButton:
clearTextBoxes();
break;
}
}
private void clearTextBoxes() {
topNumericInputField.getText().clear();
bottomNumericInputField.getText().clear();
}
Now, it clears both fields fine with either:
a) The field I click on already has focus
or
b) I click the clear button
If I change focus from the top field to the bottom field, it gets focus, then I need to click it one more time.
I'm not sure exactly what's happening, but I'm sure it's related to something that keeps coming up in the LogCat log, every time I change focus two lines appear:
Tag: IInputConnectionWrapper Text: beginBatchEdit on inactive InputConnection
Tag: IInputConnectionWrapper Text: endBatchEdit on inactive InputConnection
I've tried searching SO and I've seen some submissions that are similar, but none seem to really be what I'm looking for.
Thanks!
I think problem is not with setting text but problem lies in capturing wrong event.
As you said you want to clear edittext when focus is changed, then you should override onFocusChange(View v, boolean hasfocus) to capture that event. You need to implement FocusChangeListener.
edittext.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean focus) {
if (focus) {
((EditText)v).setText("");
}
}
});
A simple implementation should look like above. Refer this for detailed information
You can set the text null.
topNumericInputField.setText("");
bottomNumericInputField.setText("");
You just need to clear your Text by:
topNumericInputField.setText("");
bottomNumericInputField.setText("");
I've come about as far as this which gets me halfway there, but not quite.
I have a dialer Fragment that has all the usual Buttons to enter a number including backspace, so I don't need the soft keyboard. I'd also like to give the user the ability to paste text (long click... works fine per default), as well as to edit what has been entered so I need the cursor.
The easiest way I found to make sure the soft keyboard doesn't pop up if the user clicks inside the EditText is to set the inputType to null - but that kills the cursor as well.
So, how do I declare my EditText and what kind of commands should I launch to have my EditText field never ever show the soft keyboard no matter what the user attempts, but still retain paste functionality and the cursor?
I've also tried android:windowSoftInputMode="stateAlwaysHidden" in my manifest, but to no avail.
This worked for me:
// Update the EditText so it won't popup Android's own keyboard, since I have my own.
EditText editText = (EditText)findViewById(R.id.edit_mine);
editText.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
v.onTouchEvent(event);
InputMethodManager imm = (InputMethodManager)v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) {
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
return true;
}
});
I have finally found a (for me) working solution to this.
First part (in onCreate):
// Set to TYPE_NULL on all Android API versions
mText.setInputType(InputType.TYPE_NULL);
// for later than GB only
if (android.os.Build.VERSION.SDK_INT >= 11) {
// this fakes the TextView (which actually handles cursor drawing)
// into drawing the cursor even though you've disabled soft input
// with TYPE_NULL
mText.setRawInputType(InputType.TYPE_CLASS_TEXT);
}
In addition, android:textIsSelectable needs to be set to true (or set in onCreate) and the EditText must not be focused on initialization. If your EditText is the first focusable View (which it was in my case), you can work around this by putting this just above it:
<LinearLayout
android:layout_width="0px"
android:layout_height="0px"
android:focusable="true"
android:focusableInTouchMode="true" >
<requestFocus />
</LinearLayout>
You can see the results of this in the Grapher application, free and available in Google Play.
Setting the flag textIsSelectable to true disables the soft keyboard.
You can set it in your xml layout like this:
<EditText
android:id="#+id/editText"
...
android:textIsSelectable="true"/>
Or programmatically, like this:
EditText editText = (EditText) findViewById(R.id.editText);
editText.setTextIsSelectable(true);
The cursor will still be present, you'll be able to select/copy/cut/paste but the soft keyboard will never show.
Best solution from #Lupsaa here:
Setting the flag textIsSelectable to true disables the soft keyboard.
You can set it in your xml layout like this:
<EditText
android:id="#+id/editText"
...
android:textIsSelectable="true"/>
Or programmatically, like this:
EditText editText = (EditText) findViewById(R.id.editText);
editText.setTextIsSelectable(true);
The cursor will still be present, you'll be able to select/copy/cut/paste but the soft keyboard will never show.
If your min SDK is 21, you can this method from java code:
editText.setShowSoftInputOnFocus(false);
Credits to Chen Su article.
use
android:windowSoftInputMode="stateHidden"
in your manifest file instead of android:windowSoftInputMode="stateAlwaysHidden"
This is what I did.
First, in manifest inside activity
android:windowSoftInputMode="stateAlwaysHidden|adjustNothing"
Second, in onCreate if inside activity or onActivityCreated if inside fragment
editText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
hideSoftKeyboard(v);
}
});
Do not forget to request focus to the editText
editText.requestFocus();
Then add the hideSoftKeyboard(v) method same as the other answer.
private void hideSoftKeyboard(View v){
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
The key here is to requestFocus before clicking the EditText. If without focus, first click will make the keyboard show up(my experience). However, this is applied if you have a single EditText in an activity. With this, you still can type with custom keyboard(if any), can copy and paste, and cursor is still visible.
The exact functionality that you require is provided by setting the flag textIsSelectable in EditText to true. With this, the cursor will still be present, and you'll be able to select/copy/cut/paste, but SoftKeyboard will never show. Requires API 11 and above.
You can set it in your xml layout like this:
<EditText
android:textIsSelectable="true"
...
/>
Or programmatically, like this:
EditText editText = (EditText) findViewById(R.id.editText);
editText.setTextIsSelectable(true);
For anyone using API 10 and below, hack is provided here :
https://stackoverflow.com/a/20173020/7550472
This works perfectly (for me) in 2 steps:
<activity... android:windowSoftInputMode="stateHidden"> in manifest file
Add these properties in your editText XML code
android:focusable="true"
android:focusableInTouchMode="true
You have to put both 1 and 2, only then it will work.
Cheers
EditText text = (EditText) findViewById(R.id.text);
if (Build.VERSION.SDK_INT >= 11) {
text.setRawInputType(InputType.TYPE_CLASS_TEXT);
text.setTextIsSelectable(true);
} else {
text.setRawInputType(InputType.TYPE_NULL);
text.setFocusable(true);
}
First add android:windowSoftInputMode="stateHidden" in your manifest file, under the activity. like this
<activity... android:windowSoftInputMode="stateHidden">
The on your xml add this android:textIsSelectable="true" . This will make the pointer visible.
Then on onCreate method of the activity, add this:
EditText editText = (EditText)findViewById(R.id.edit_text);
edit_text.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
v.onTouchEvent(event);
InputMethodManager inputMethod = (InputMethodManager)v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
if (inputMethod!= null) {
inputMethod.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
return true;
}
});
i found this very useful code and it work as charm, it head the Keyboard totaly, but keeping cursor and you can copy past, move the cursor...ect
using :
hideSoftKeyboard(editText);
methode :
public void hideSoftKeyboard(EditText edit) {
if (android.os.Build.VERSION.SDK_INT <= 10) {
edit.setInputType(InputType.TYPE_NULL);
} else {
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
try {
Class<EditText> cls = EditText.class;
Method setSoftInputShownOnFocus;
setSoftInputShownOnFocus = cls.getMethod("setShowSoftInputOnFocus", boolean.class);
setSoftInputShownOnFocus.setAccessible(true);
setSoftInputShownOnFocus.invoke(edit, false);
} catch (Exception e) {
e.printStackTrace();
}
}
}
EditText editText = (EditText)findViewById(R.id.edit_mine);
editText.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
v.onTouchEvent(event);
InputMethodManager imm = (InputMethodManager)v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) {
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
return true;
}
});
ha... this is the correct way of doing...this job done... this gonna work !
You can use the following line of code in the activity's onCreate method to make sure the keyboard only pops up when a user clicks or touch into an EditText Field. I tried lots of methods and codes from stackoverflow but didnt work any but this
Works Perfectly for me!! Try this.. :)`
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
You can use the following line of code in the activity's onCreate method to make sure the keyboard only pops up when a user clicks or touch into an EditText Field. I tried lots of methods and codes from stackoverflow but didnt work any but this Works Perfectly for me!! Try this.. :)`
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
How can make an EditText have a onClick event so that on single click an action is done.
private void addListenerOnButton() {
dateChanger = (EditText) findViewById(R.id.date_iWant);
dateChanger.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
showDialog(DATE_DIALOG_ID);
}
});
}
this is not working as excepted....single click gives just the onscreen keypad but not the datepicker dialog which appears only if i double click
if we just add android:focusableInTouchMode="false" in edittext on layout page it should work in a singleclick on its onclicklistener. no need to handle onFocusChangeListener.
Change your code from an onClickListener to an OnFocusChangeListener.
private void addListenerOnButton() {
dateChanger = (EditText) findViewById(R.id.date_iWant);
dateChanger.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus) {
showDialog(DATE_DIALOG_ID);
}
}
});
}
EditText is not meant for singleClick.
I mean you should not use Click Listener with it.
rather you can do like,
Use onFocusChangeListener which is also not 100% correct approach.
Best would be instead the EditText use one TextView write onClick of that and if needed give a background image to that TextView.
Rewrite
I have an EditText that launches a dialog when the user either clicks it once or navigates to it with a trackball / directional pad. I use this approach:
Use an OnFocusChangeListener for gaining focus to open the dialog.
Override dismissDialog() to clear the focus from the EditText when the user closes the dialog, preventing the user from entering text without the dialog (as far as I can tell)
.
I have also tried this (however I now remember this method did respond to trackball movement):
Use an OnClickListener for touch events.
Set setFocusable(false) to prevent user input.
Hope that helps.