In Android I have the following code to blank an EditText box when clicked.
This is to remove any existing information in the box and start with a blank slate.
It sort off works. When you first click in the edit box the soft keyboard appears, but the existing information in the EditBox is still there. If you click a second time in the EditBox the information dissapers.
How can I get it to clear the data on the first click? I am guessing the first click is intercepted by the soft keyboard.
Many thanks for any help.
// Blank EditText field when clicked
myEditBox.setOnClickListener( new OnClickListener(){
#Override
public void onClick(View v) {
tcA.setText("");
}
});
Im not sure, but try clearing it when the EditText gets focus.
myEditBox.setOnFocusChangeListener( new onFocusChangeListener(){
#Override
public void onFocusChange(View arg0, boolean hasFocus){
tcA.setText("");
}
});
Related
My view got 3 item, EditText, ImageView and TextView. now i'm doing some animation base on a tutorial which was working fine, now in that tutorial guy was using onClickListener to animate view and hide image, and was working fine, but my problem is, i want to show image when user is not on EditText anymore.
Right the idea is working like this
Click on EditText => Gonna animate and hide image => and when u tap or click on layout/activity/view/etc. image won't comeback.
Code :
btnInvite.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
titleInvite.animate().translationY(-350).setDuration(800).setStartDelay(100).start();
subtitleInvite.animate().translationY(-350).setDuration(800).setStartDelay(100).start();
inputInvite.animate().translationY(-350).setDuration(800).setStartDelay(200).start();
btnInvite.animate().alpha(1).translationY(-350).setDuration(800).setStartDelay(300).start();
imageView.startAnimation(disapear);
imageView.setVisibility(View.INVISIBLE);
}
});
This is what going to happen when someone click on EditText now i want to reverse it when edit text is not focused anymore, or something else clicked. I'm also using API 17, and i'm telling this bcs i looked into some of features but most of them couldn't be used in 17.
You need to add a listener for loosing focus on editText, in that listener execute the reverse animation.
EditText txtEdit = (EditText) findViewById(R.id.edittxt);
txtEdit.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
// Execute reverse animation
}
}
});
I am creating a program that write in EditText by barcode reader so I don't want to show the keyboard immediately even if I focused on it I don't wanna it to be visible , I need to press a button to show keyboard only to Edit sometimes .
and thanks
I would disable the EditText button from the beginning:
editText.setEnabled(false);
And to answer your question, yes. Even if it is disabled, you can change the text. Disabled only means user can't change it. You can programmatically edit it.
Then when the button is pressed:
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
editText.setEnabled(true);
editText.requestFocus();
}
});
This should automatically show the keyboard when the button is pressed.
Bonus:
If you want to disable the EditText once the editing is done, you can do this:
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(!hasFocus) {
editText.setEnabled(false);
}
}
});
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.
I have a question that How to check whether the widget having a focus or not. Actually I have a form in which there is a DOB edit text, a user navigates all the edit text boxes with Next action on keyboard and I want when user navigates to DOB field from any of the control then a Calendar Dialog automatically appears, currently what happens is user have to click on edit text then a Calendar Dialog appears, I want whenever DOB field gets a focus then it automatically call the Calendar Dialog.
I have searched regarding the same enough on web but failed to achieve this. Please help me out about this problem.
Thanks in advance.
Is it not
if(view.isFocussed) {}
?
1) Create a Handler, as a field in your Activity. Like this:
private Handler myHandler = new Handler();
2) When you create the ListView, add a OnFocusChangeListener, like shown below.
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
myHandler.postAtFrontOfQueue(new Runnable() {
public void run() {
myList.setSelection(0);
}
});
}
}
});
When i click on the edittextview then some times keyboard shown or some times keyboard are not shown.
In android 2.1 it show the keyboard when i click on the edittextview
but when i start same application it on android 2.2 then it not show the keyboard.
Help me how to show that problem.
OK, This might be a late response, but it worked.
I have met this problem on android 2.1 and 2.3.x(not tested on other versions of SDKs).
I noticed a strange thing that when my click on the EditText was unable to open the keyboard, I pressed the BACK button to show an alert dialog and then I canceled(closed) it, and clicked the EditText again, now the keyboard was brought to life again.
From that I can conclude that the keyboard will always show for the EditText if the EditText does not previously own focus(showing an alert dialog over the EditText view will make the EditText to lose focus).
so call the function below on your EditText when it is brought to front:
mEditText.clearFocus();
or
parentViewThatContainsEditTextView.clearFocus();
I had a similar problem on Galaxy S3 (displaying EditText controls on a PopupWindow - the keyboard was never showing). This solved my issue:
final PopupWindow popUp = new PopupWindow(vbl.getMainLayout());
[....]
popUp.setFocusable(true);
popUp.update();
I didn't want to EditText lose a focus using editText.clearFocus(). Came up to this solution.
#Override
public void onResume() {
super.onResume();
if (Build.VERSION.SDK_INT < 11) {
editText.clearFocus();
editText.requestFocus();
}
}
here's a possible solution:
editText.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(final View v, final boolean hasFocus) {
if (hasFocus && editText.isEnabled() && editText.isFocusable()) {
editText.post(new Runnable() {
#Override
public void run() {
final InputMethodManager imm = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText,InputMethodManager.SHOW_IMPLICIT);
}
});
}
}
});
code is based on the next link:
http://turbomanage.wordpress.com/2012/05/02/show-soft-keyboard-automatically-when-edittext-receives-focus/
In my case it was in a PopupWindow and I simply needed to call popupWindow.setFocusable(true)
I had this same problem when displaying an EditText in a DialogFragment. Despite the EditText getting focus (i.e., when clicked, it showed the flashing caret), the keyboard did not display.
My solution was to add a dummy EditText to the uppermost view of my DialogFragment.
<EditText
android:id="#+id/editTextFix"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/fix"
android:importantForAutofill="no"
tools:targetApi="o"
android:inputType="text"
android:visibility="gone"/>
Possible scenarios:
1) On clicking the EditText, usually the keyboard comes up. But if you press the back key button in the emulator the keyboard (not the screen keyboard) dimisses.
2) In code you can disable the keyboard on clicking the EditText by setting a flag.
InputMethodManager inputmethodmgr= (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
inputmethodmgr.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
It works like a charm, In a case if you even want to hide on click of the edittextView.
textView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
displayKeyboard();
}
});
private void displayKeyboard(){
if (textView != null) {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInputFromWindow(textView.getApplicationWindowToken(), InputMethodManager.SHOW_FORCED, 0);
}
}
In your parent view check if there is android:descendantFocusability="blocksDescendants"
remove it.