1

I am trying to cope with one (seemed to be) smalll thing. In my app I've got one activity with two EditText fields.

I want one of them to be normall field (etNormal), and the other (etButton) behave more like button, so when you touch it, the keybord is not shown but instead the sliding drawer is opened. If sliding drawer is opened and you will press the normall edittext sliding drawer will hide.

I've tried adding OnClickListener and OnTouchListener (not in same tries) to both with condition if etButton was clicked/touched open sliding drawer, if not then close.

The outcome was strange. When it was OnTouchListener test it was more like toggle, so when I pressed one drawer opens and on another close. When it came to OnClickListener I needed to press each edtitext twice to get action done.

And to hide keybord in etButton I am using setInputType(InputType.TYPE_NULL);. I've tried also setEnabled(false); but then I was even unable to click/touch it. The one defect of currently used method is when I am changing click from etNormal to etButton, the keyboard is still shown and it doesn't hide.

So, can anyone tell me what I can do to achive my goal?

EDIT:

I've erad your current suggestions and modified a little my code, but still it is not working.

This is a part of it where I am assigning OnTouchListener:

OnTouchListener touchListener = new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent ev) {
                if(v==etButton && ev.getAction()==MotionEvent.ACTION_DOWN) {
                    slidingDrawer.animateOpen();
                }else {
                    slidingDrawer.animateClose();
                }
                return false;
            }
        };

        etNormal1.setOnTouchListener(touchListener);
        etNormal2.setOnTouchListener(touchListener);
        etButton.setOnTouchListener(touchListener);

Also in etButton declaration in XML layout file I have:

android:focusable="false"

But now, on etButton touch nothing hapens (only sliding drawer hides if was opened), and when etNormal1 or 2 is touched sliding drawer shows up or hides depending what was first (in other words toggel).

Any idea, what is wrong here?

sebap123
  • 2,189
  • 3
  • 34
  • 75

6 Answers6

6

Got an editText working like that with

android:focusable="false"
android:clickable="true"

Then an OnClickListener to override the action

jfv
  • 135
  • 1
  • 9
4

In addition to above answers, I hide cursor using cursorVisibility!

android:focusable="false"
android:clickable="true"
android:cursorVisible="false"
android:focusableInTouchMode="false"

cursorVisible To show/hide the cursor on clicking the EditText

focusable To gain focus when user is touching the view, like EditText

focusableInTouchMode To keep a view selected. (select and click are different)

For detailed understanding, refer here

reverie_ss
  • 1,376
  • 15
  • 22
2

In your layout, add the following attribute to the EditText

android:focusable="false"
android:focusableInTouchMode="false"

Next, write method to handle the click on the EditText and add your application logic .

jagmohan
  • 2,012
  • 1
  • 26
  • 38
1

If you are using onTouch event, when you click the edittext, you will get two events with action as MotionEvent.Action_down and action Up. so basically it will give the effect of clicking the edit text twice. can you please provide the code so that we can have a deep look.

ReWrite your code as :

OnTouchListener touchListener = new OnTouchListener() {

        boolean isOpen=false;

        @Override
        public boolean onTouch(View v, MotionEvent ev) {
            if(v==etButton && ev.getAction()==MotionEvent.ACTION_UP) {
               if(!isOpen){
                slidingDrawer.animateOpen();
                }else{
                slidingDrawer.animateClose();
                }
                isOpen=!isOpen;
            }
            return false;
        }
    };
Eldhose M Babu
  • 13,432
  • 7
  • 35
  • 44
0

If etButton needs to be an EditText (why not a button, if it should behave like one?), maybe you could set an onFocusChangeListener. Once it gets the focus, you can show the drawer...?

Not sure about not showing the keyboard...

jpm
  • 2,686
  • 1
  • 15
  • 20
  • That does not work. One of the EditText will automatically get focus without user interaction when the layout is inflated. – Xaver Kapeller Nov 04 '13 at 10:19
  • mh ok (a hacky workaround would be to add an invisible view that has the focus in the beginning, but I'm sure there's a better solution). – jpm Nov 04 '13 at 10:25
  • Yes, but a better workaround in my opinion is to manually assign the focus to the layout containing the EditTexts. That way you have full control over which View actually has focus and you do not need to add an extra View. And aside from that there is no better workaround. At least that I know of. This is just a sometimes problematic behavior of EditTexts. – Xaver Kapeller Nov 04 '13 at 10:28
0

EditTexts are tricky. The reason why you have to press twice when you have use an OnClickListener is that the first time around the EditText gets focus and this consumes the touch event, in that case the OnFocusListener is triggered. When you touch the second time, the EditText already has Focus so now a click event is triggered.

I would suggest you try to do this without EditTexts. That would in any case yield a cleaner and simpler solution. Why exactly do you want to use EditTexts instead of Buttons?

Xaver Kapeller
  • 46,604
  • 11
  • 87
  • 82