Custom Listview with EditText: Focus - android

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.

Related

Applying EditText changes when another control is clicked

I have an Android activitity with a spinner, a few buttons, and an EditText control. I want to apply text changes when the user taps anything outside the text editor. To achieve this I tried to implement onFocusChanged() listener in the following way:
textEditor.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean hasFocus) {
if(!hasFocus)
{
checkUpdatedText();
}
}
});
Unfortunately, when I tap the spinner, the dropdown window appears, but the cursor is still visible in the text editor below it, and the onFocusChange() is not triggered. So the data in the dropdown list view is obsolete. Is there a way to ensure that the changes are applied exactly when another control is tapped? I do not want to update the data on every keypress, because this is a multiline edit view and the text may be lengthy.

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 EditText field like button

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?

Moving Focus to GridView's first item view

In my activity i have two fragments 1st one contains a linearlayout and second one contains a gridview and i have to support D-pad navigation in my app.
now when user press key to movetowards grid view i want to show the 1st item of gridview as selected view
so every time when focus moves from linearlayout to gridview , gridview's first item should get selected/focused
any leads on these is highly appreciated
on keypress towards gridview you can call:
firstItem.requestFocus();
replace "firstItem" by your element name, such as button, edittext, etc
OR
you can implement OnFocusChangeListener to your activity and do this:
gridView.setOnFocusChangeListener(this);
and then
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus){
firstItem.requestFocus();
}
}

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

Categories

Resources