Android : Multiple Actions on a List View - Focus Issue - android

I would like to implement a ListView, which I can do no problem w/ my cursor. Right now depending on which row you click on it takes you to a new activity based on the information pressed on that row (just like it should, and as expected). I would like to have a button however to delete the row, so a user can press any part of the row to launch the new activity, but if they press the button on that row, it deletes the row (or launches a delete activity/function).
If you can look # DroidRecord, they have a similar layout as I am looking to achive.
Thanks!
Chris.

As Mariano Kamp said, adding buttons to a row will make it "untouchable", but in my experience, this problem goes away if you set these properties on the buttons:
android:focusable="false"
android:focusableInTouchMode="false"
See also How to fire onListItemClick in Listactivity with buttons in list?

Another possible workaround - you can use an ImageView instead of the button, and set the ImageView's onClickListener (For example, when you're inflating the cell view).
ImageView is not focusable so it doesn't prevent OnListItemClick() from being dispatched, and when you click on the image only the image's listener fires.

what is your question? How to add a button to a list row?
Very simple, just as you expect it will be added to the row's layout.
Unfortunately though that will also make the whole row "untouchable". The Google developer I asked said that this is by design (as far as I remember), and that you should use TouchDelegate to cope with this. As there are no samples, not even in the android source, and only very thin documentation that didn't work for me
Anyway, it seems that not many applications use a button in the list row. I only know about mine (newsrob, see Articles List) and the Alarm clock. Maybe you could use a context menu?
Otherwise the ugly solution would be to add to call setOnClickListener() on your row view in the getView method.
Cheers

It's not the answer to your question, but the long-click/tab is generally the place to pop up a context menu and put extra actions, like delete. You can read how to do it here: How do you implement context menu in a ListActivity on Android?

I would like to thank BoD for its hint on removing the focusable state to the button(s), it saved my day.
But for information, since the button is no more focusable - state_focused on < selector > xml - , its design won't display anymore to the user.
Those buttons will still get the state pressed though, but also when clicking anywhere else on the parent view (anywhere BUT another button) !
Keep that in mind, it could not be a good solution for your own case, but it does work well.

I tried this to be able to click on the buttons but it didn't work for me
android:focusable="false"
android:focusableInTouchMode="false"
so what I did was to change the activity layout to scrollview and then add a linerLayout inside of it.
after that you can add buttons to the layout and each button will be clickable.

Related

Access Adapter items of Recyclerview

I'm using Recyclerview to show a list. I want to delete some items like IOS. In my listview template I have added a button to delete item which is invisible by default. In my activity I have another button attached at bottom (Not part of listview) and on tap of this button I want to make all delete buttons of listview visible.
My Question is how can I get reference to all delete buttons of listview in activity and is it the right way to do this?
Thanks
Assuming you have ViewHolders set up, you already have references to all the buttons in your list. All you have to do is to make them visible for every item in the list with a simple loop.
In case you haven't implemented ViewHolders I suggest you check out the documentation and take a look at some simple tutorials on how to use them.
On a side note. If I understood correctly you're making a bottom tab for your app and since you referenced iOS I gotta say this; Remember that Android and iOS are two unique operating systems with their own ways of handling things. Check out Googles pure Android documentation.
In your question title you say RecyclerView, but in your text you say ListView. The solution is similar either way, but it's best to be perfectly clear what you're doing.
In either case, there are at least two different solutions.
First, you could use a boolean flag to determine if all the the item buttons should be showing or not. You check this flag at the time the item view is inflated or created and toggle the button accordingly. If the boolean flag is ever changed, the easiest thing to do is tell the RecyclerView/ListView that the underlying data has changed and to redraw all the views. Call notifyDatasetChanged on the adapter.
The other thing you can do at the time the item buttons should change is iterate all the visible item views, find the button, and change its visibility. With RecyclerView, you can do this, and with ListView you can do this.

Android ListView custom row overlay

I am developing an activity with a ListView in which I need to change the current row by another layout by clicking on the row, and I'm not finding any way to do as much as I look (I take hours searching for possible solutions and I have not seen any reference to this problem). I do not know if this can be done in Android, but if anyone has an idea of ​​how to do this would be appreciated.
Thanks in advance.
PS: The ListView control is normal and just want to replace a layout with a different layout. I'm using the API 15.
Use a ViewSwitcher
http://developer.android.com/reference/android/widget/ViewSwitcher.html
A ViewSwitcher is -
ViewAnimator that switches between two views, and has a factory from
which these views are created. You can either use the factory to
create the views, or add them yourself. A ViewSwitcher can only have
two child views, of which only one is shown at a time.
I suggest merging the two layouts in a single one and hide the second one. In your adapter data you should have a flag or something to indicate which layout to display. When you click a row, toggle that flag for the selected item and notifyDataSetChanged() on the adapter. This will make sure the changed layout remains even if you scroll up and down and the row goes off screen.
A more optimized solution is to have different item types in the adapter.

ListItem click and checkbox click at the same time

I've been trying many things with list items and i'm facing some problems. In my previous question, thanks to #nEx.Software i was able to resolve the problem. But I'm still missing some concepts here.
Right now i'm trying to differentiate between an item's click and a checkbox within it. However, I want to do it without extending the array adapter. Is there a way to use both methods: listView.onItemClickListener() AND listView.getCheckedItemPositions(), together!
There should be a way to use an xml file [doesn't matter how complex it gets] along with extending the available Views and this thing should be done.
Putting it simple, open the gmail app, and u'll find all emails listed with checkboxes where u can click on the checkbox to mark it OR the rest of the item to open the email.
Again, I know that it is doable with extending Adapters and adding an Array for the checkboxes, but, is it possible to use the convenient methods: listView.onItemClickListener() AND listView.getCheckedItemPositions()? is CheckedTextView a part of the solution?
One more thing, rather than just answering me [where i become lazy]
Wheather it is possible or not, is there a verry reliable reference for such issues? I would really like to fully understand everything that goes into this matter... if its not possible, i must be able to tell why!
Thank you :)
add custom row into your listview. into your custom row you add one textview and checkbox into linerlayout and get linerlayout click event.

How to customize listview in android?

In my game application am using a listview to select levels. Am activating this listview item onclickable if the previous level is cleared. I need to differentiate the activated and unactivated level's in listview.How can i make it possible? If possible suggest me tutorial. Thank you
you can put your logic for activated and unactivated levels in getView method ,check if activated then inflated activated row otherwisw unactivated row.
First check this and this stackoverflow questions and try some code and post your question if you have any problems. Please try first.
You can use setClickable(false) to disable touch events on list. To make it look disabled, you can change the textView colors in the list items and get such effects.
Thanks

Focus on list item child views

Is it possible for child views of a list item to receive focus? I have a custom list
view. Each row has a linear layout with two views, a button and a textview. I want to
get the click event on the button and also have the background change color with focus.
Wow, that was hard to read. You should edit the question and try to explain yourself better.
To enable a click listener on those buttons you must add the listener inside the getView() method of the adapter. If you are using the View Holder Pattern
holder.button.setOnClickListener(...)
and then access the parent or whatever to change the background.
If you don't know what I'm talking about with that holder thing, watch this video, it's at the beginning.
Good luck!
From what I could decipher from your question this might be what you need.
Don't quote your own sentences. Also reading what you have typed, before clicking Post would save a lot of effort spent in understanding the question.

Categories

Resources