Android EditText field like button - android

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?

Got an editText working like that with
android:focusable="false"
android:clickable="true"
Then an OnClickListener to override the action

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

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 .

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;
}
};

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

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?

Related

EditText weird focus behaviour

Alright,
When I have the focus on my EditText and the keyboard etc is showing. I click on my drawable at the end of the edittext (Cancel button). I recognize this event with an onTouchListener and hide the keyboard, clear focus, etc myself.
However, when I 'touch' the cancel button, it hides the keyboard but the focus stays on the edittext. Meaning the cursor is still showing, but not blinking. So when I click on the edittext again to get focus, it shows the option to copy/paste, etc like I'm long pressing the cursor and doesn't show the keyboard.
But when I 'press' the cancel button, it clears the focus, hides the keyboard and most importantly it hides the cursor resulting in completely removing the focus. Then when I click on the edittext again, it gets focus back and DOES show the keyboard.
What is this weird behavior and how do I make it that it always does the 'press' behavior.
my code:
searchView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
if (event.getRawX() >= searchView.getRight() - searchView.getCompoundDrawables()[2].getBounds().width()) {
hideKeyboard(v);
isSearching = false;
searchView.setText("");
searchView.clearFocus();
searchView.setFocusable(false);
searchView.setFocusableInTouchMode(false);
getView().requestFocus(); // parent has both focusables true
searchView.setFocusable(true);
searchView.setFocusableInTouchMode(true);
fetchArticles();
return true;
}
}
return false;
}
});
Like you're seeing, I'm trying everything I can to always get the same behavior. However no luck so far. Hope you guys can help me!
Try these in parent of that EditText or in views you want to touch:
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"
It will allow to completely transfer focus elsewhere from EditText
However, in case when your drawable is inside of your Edittext like here:
android:drawableLeft="#drawable/my_icon"
the solution won't work until you make drawable as separate view in layout.
Oke so my current 'solution' is a suggested solution by #Dmitriy Pavlukhin.
Instead of using the drawableEnd from EditText itself, I created a separate view which has the same drawable and make sure to draw this on the same position. Now when I click this view, it clears the view as desired 100% of the time. The codebase is still the same from the question, however it is now implemented in a OnClick on the separated view.

Android: Replace soft keyboard with custom layout (Wheels for date picking)

I have a EditText where user needs to enter date. For date picking I have a custom wheel view (like iOS). Now on click of date EditText, I want to open a soft keyboard like drawer from bottom & put that wheel view inside it.
So can I replace the soft keyboard with my wheel view ? I mean, the container which hold the keyboard should now hold my custom wheel view.
Is there any way to achieve that ? Or any other better option for my purpose ?
1) If you must use EditText, you can prevent keyboard coming up, by setting its input type correctly. Other answer already mentions that.
2) But why use EditText here, especially since you are not inputing any value using keyboard here. You can use a TextView, and its onClickListener you can animate a LinearLayout (containing date picker) upwards.
yourTextView.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v){
//Animate a LinearLayout containing a DatePicker
animateDatePickerView();
}
});
Depending on your requirements and how re-usable you want it to be.
For the simple way, you can create a view to host that wheel picker align it bottom of the root layout and just hide/show it with some animation.
First you would want to override the onTouchListener of your edit text, consume the event and do what you want there.
editText.setOnTouchListener(new OnTouchListener() {
public boolean onTouch (View v, MotionEvent event) {
//Show the wheely wheel.
return true; //Consume the event.
}
});
My personal idea (not tested)
prevent showing keyboard when edittext is focused.
E.g, setInputType(InputType.TYPE_NULL);
Use a customized activity which contains your wheel and show it as dialog ( set theme as dialog).

Custom Listview with EditText: Focus

I have a simple layout with EditText and a Listview. Implemented custom ArrayList adapter to fill in the listview and custom filter so user can search for items displayed in the listview.
When I run the application, the focus is initially set to EditText and the keyboard is displayed as expected. But here is what I want to do:
The focus should be initially set to ListView when the app is launched.
If the user wants to enter text by selecting edittext control, then the keyboard should appear.
( At this point as the user inputs text, the listview items will change - I already implmented this)
With the keyboard still open, if the user select an item in the listview, the keyboard should disappear and trigger the listview onItemClick function.
How can I accomplish this?
1.Add below attribute to your particular EditText in its layout-xml:
android:focusable="false"
android:focusableInTouchMode="true"
2.Then in your Activity add:
mEditText.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
v.setFocusable(true);
return false;
}
});
Hope this helps.

How to disable edittext pop up when EditText is LongClicked?

Hello All I have used onLongClickListener() to my EditText view but whenever I click on view for long time so a popup appears that doesn't let me do my task in onLongClick(View v).
The pop up contains some options like Select All,Select Text,Cut All ,Copy All etc.
I want to disable that (that should not appear on clicking long on EditText view).
How can I do that please Help Me
You can return true in your onLongClick listener onLongClick method that means you have consumed the event and it won't be passed further along.
I also got that popup when I overrode onTouchEvent() and returned false. Suppress it by returning true.

Overriding trackpad click

I have a ListActivity; and for each item in the ListView there is a checkbox.
When you touch a list item, another Activity launches.
When you use the trackpad/trackball to highlight (read: select) an item and click the trackpad, it essentially simulates touching the item. This causes my other Activity to launch.
I would like clicking the trackpad to check the checkbox of the highlighted item. Is there a handler I can override to do this?
You need to override the onTrackballEvent(MotionEvent) method and catch ACTION_DOWN. Here is an example of how to do this:
#Override
public boolean onTrackballEvent(MotionEvent event) {
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
//Do your work here
return true;
}
return super.onTrackballEvent(event);
}
Hope this works for you!
Not sure on a definite answer to this, but one thing worth researching is android:focusable.
I think your best bet is to make the List Items themselves not focusable, but the checkboxes focusable. This way when the user scrolls with the trackball/pad it will switch focus between the checkboxes instead of the list items, and behave in the way you want. This won't affect touch events.

Categories

Resources