i'm facing wired problem in android when i try to focus for a EditText control.
In my application, i am dynamically adding the edit controls, whenever new control is added i wanted to
give auto focus and popup the softkeypad automatically.
sometimes editbox get focus, but if i type text will not appear, sometimes text will appear in the other text control.
i'm observing this behaviour in my phone.
Here is my code snippet which i used to provide focus.
if(mOldEditBox!=null)
{
mOldEditBox.clearFocus();
}
textView.requestFocus();
//textView.setInputType(InputType.TYPE_CLASS_PHONE); //to popup numpad
//textView.setOnFocusChangeListener(focuschange);
mOldEditBox = textView;
i tried setting focuschangelistener event, still it didnt worked :(
OnFocusChangeListener focuschange = new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
// TODO Auto-generated method stub
if(hasFocus){
EditText txt = (EditText)v;
//txt.setInputType(InputType.TYPE_CLASS_PHONE); //to popup numpad
((InputMethodManager)getSystemService(INPUT_METHOD_SERVICE))
.showSoftInput(txt, InputMethodManager.SHOW_FORCED);
}
}
};
Kindly help me where is the problem.. thanks in advance
You are probably not on the UI thread when requesting focus which might cause the strange behavior of the EditText. Try adding it to the message queue using the post(Runnable) method:
textView.post(new Runnable() {
public void run() {
textView.requestFocus();
}
});
Related
I'm doing a app wich have two EditText. What I wanna do is, if you click on one and write on it, the other must erease anything It has and only shows the hint string. I'm triying doing it witha a check method that does:
if(celsius.isFocused()){
faren.setText(faren.getHint().toString());
}
if(faren.isFocused()){
celsius.setText(celsius.getHint().toString());
}
Then I call this method within the onCreate() method, but of course It only checks one time, and if use that checkMethod inside a loop, the app doesn't show anthing, It freezes. An suggestions?
Use the OnFocusChangeListener.
faren.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus)
celcius.setText(""); // This will automatically show the hint text
}
});
In my application when I click an EditText, I have to perform some logic. I have the code. But it is not going into the click method.
My code:
EditText des=(EditText)findViewById(R.id.desinc);
des.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
java.lang.System.out.println("Inside click");
EditText income=(EditText)findViewById(R.id.editText1);
// TODO Auto-generated method stub
String inc=income.getText().toString();
int indexOFdec = inc.indexOf(".");
java.lang.System.out.println("index="+indexOFdec);
if(indexOFdec==0)
{
java.lang.System.out.println("inside index");
income.setText(inc+".00");
}
}
});
What am I doing wrong? Help me.
Try overriding onTouch by setting up an onTouchListener in the same way as an onClickListener. Use this code as a reference.
EditText dateEdit = (EditText) findViewById(R.id.date);
date.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
//anything you want to do if user touches/ taps on the edittext box
}
return false;
}
});
UPDATE(why this behavior):
The first click event focuses the control, while the second click event actually fires the OnClickListener. If you disable touch-mode focus with the android:focusableInTouchMode View attribute, the OnClickListener should fire as expected.
You can also try this: set android:focusableInTouchMode="false" for your EditText box in the xml. See if it works with the existing code.
You should use OnFocusChangeListener()
Try clicking EditText twice because at first instance EditText gets focus and after that EditText's click event executes. So, if you want your code to execute on first click write your code for focus change of EditText using OnFocusChangeListener().
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);
}
});
}
}
});
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("");
}
});