prakash .k
prakash .k

Reputation: 635

EditText focus in android

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.

Upvotes: 0

Views: 5076

Answers (3)

Steelight
Steelight

Reputation: 3367

You should use OnFocusChangeListener()

Upvotes: 2

pixelscreen
pixelscreen

Reputation: 1975

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.

Upvotes: 3

Lalit Poptani
Lalit Poptani

Reputation: 67296

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().

Upvotes: 0

Related Questions