Is there a way to programmatically deselect/wipe whatever the user has selected with the trackball/trackpad?
When I hit the back button on an Activity, the Activity it falls back to has a button that is selected as if the user had used the trackball/pad. I'm not sure what is selected on the previous Activity, but obviously something is. I'd like to programmatically wipe any selection just before the Activity finishes.
Looking through the JavaDoc for View I see a number of focus-related functions.
void clearFocus(); // drop focus from this view.
View findFocus(); // finds a view that is a child of this view that has focus, if any
View focusSearch(int dir); // finds the next view that can take focus in the given direction
void requestFocus
Sounds like findFocus().clearFocus() should do the trick (unless findFocus happens to return null)... you just need a handle to the other activity's View... which shouldn't be too hard if it's your code, or Non Trivial if it isn't.
If it IS your code, it seems like you could just add a clearFocus() to the button's onClickHandler.
Related
I have an EditText pageTitle and its behavior is dependent on its onFocusListener and onClickListener and another method setListenerToRootView() which detects if the soft keyboard is shown or hidden. The issue is whenever I click pageTitle, the cursor is automatically placed at the beginning. I am suspecting setListenerToRootView() is responsible for this bug since issue fixes itself when I remove this method.
However, I do need this method since another view's visibility (saveCancelBar) depends on the soft keyboard's visibility. Is there any way that I can keep this method but make sure that when the user clicks pageTitle, the cursor will be placed on the exact position that he clicked (not the beginning)?
use setSelection() for selecting the last position programatically.
in onClick and onFocus use setSelection
editTextName.setSelection(editTextName.getText().length());
I have ListView with extendable items. Everything is working great until I trigger the dialog popup with EditText. Right after it animation just stop working. If I tap other button to animate the ListView container, everything coming back to normal.
In my ListViewAdapter I got item view in extends FrameLayout class and Animation is trigger when is called override onMeasure method with is not trigger after Soft Keyboard appear.
I think this is causing because there is no focus on particular item on the list?
Off course regular click on the list item is working fine.
Strange is also this on that when I hide soft keyboard by pressing back button everything also working as it should be.
Thank you for any suggestions.
When you trigger the dialog popup with EditText, save the focus of the list view:
int focusedIndex = lstView.SelectedItems[0].Index;
You can then restore it by using:
lstView.Items[focusedIndex].Selected = true;
Dialogs are modal, which means that they take away all the focus and get the user to only focus on their own UI. You need to not use a dialog, instead you need to use a Fragment or a Activity with wrap_content and a RelativeLayout to keep it small and in the center.
I wanted to develop an on screen keyboard similar to the square app (image below)
I would appreciate it if anyone could give me tips on which classes to focus on/ override for this. In particular, how do I associate the button clicks to the number input in the EditText field?
Every Button gets a Tag with the associate value (for example 1). Then, you use the android:onClick-attribute to set all buttons to the same method (input() for example) in the activity.
This method will be called with the clicked view, which you can then use the getTag()-method on to get the corresponding value:
// Will be called for every Button that is clicked
public void input(View v){
Log.v("APP", "Pressed: "+v.getTag());
}
Organize the Buttons in a GridLayout (GridView might also be possible).
I added a button in a listview item, and after the button is clicked, I want the button is disabled. I used the below setOnClickListener for my button in the custom adapter, but the problem is when i clicked on a button, the button of another list item will also be disabled. For example, when I clicked the button of item 1, the button of item 1 then is disabled, but the item 4's button will also be disabled at the same time although i didn't click on it.
And also, when I scroll up and down, all item's button just enable and disable randomly.
Anyone know why is this happening?
holder.viewBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
holder.viewBtn.setEnabled(false);
showInfo();
} });
I was so frustrated when I first faced this problem!
The problem here is that the listview just doesn't remember the state of the button. Dunno if it is a bug but anyways I needed a way out and this is what I did.
I believe you are using a custom adapter with a viewholder. Means you are on the right path. You need to keep an array of booleans whose size is equal to the number of items in your list. in your btnClick() set the state of the item in the array.
Now everytime you scroll, or do soemthing that makes the list redraw, getView() is called. Put a check in your getView() for the items state and enable/disable it. One more thing, Make sure you implement both if{} and else{} for the check.
if(checked){
holder.viewBtn.setEnabled(false);
}else{
holder.viewBtn.setEnabled(true);
}
if you don't do this you'll see weird behaviours. One more thing if you are using the
if(convertview == null){
//create the holder
}else{
convertview = getTag();
}
method, make sure you populate the state after the above step.
I have not seen your implementation but I had to pop up a button in the item and then delete the item from the list using it. So I had to take extra care for maintaining the state.
So be careful about the states once the underlying data has changed.
Sorry about the long post but the problem is such :(
I found a link that has the solution in a basic format
This is happening because ListView reuses Views in an erroneous manner. Either implement your own ListAdapter without View reuse or file a bug report with Google
Activity1 has a listView. Clicking one item (let's say item 3) will start Activity2. Activity2 have a back button once clicked will bring user back to Activity1. What I want to achieve is to highlight the item 3 when user back to Activity1 so that the user have a sense where to continue. (May be i need to set the focus to item 3 as well.)
EDIT: Following code works.
public void onResume()
{
super.onResume();
//lastSelectedPosition saved in OnItemClickListener
lv.setSelection(lastSelectedPosition);
lv.requestFocusFromTouch();
}
well its pretty simple. just save the clicked item position of the list to a field when the list is clicked to launch your new activity.
Afterwards in the onResume() method just use myList.setSelection(savedPosition);
as for the highlighting, well focus works kinda bad especially if you have a bit more complex rows(buttons,checkboxes etc) and other ui elements beside the list that can take away the focus. i believe the best way to achieve this is just set the background of that particullar item onResume to the highlighted one and override onScroll listener to just change the background to your default when the list is scrolled. indeed its a workaround but it will work in 100% of the cases opposite to just focusing an item. plus maybe you can add animations on the view so you can make it look really nice and smooth.