CheckBox vs CheckedTextView in a ListView - java

When I place a CheckBox in the row layout for a ListView, I am no longer able to recieve OnItemClick and OnItemLongClick events. On the other hand, using CheckedTextView allows these events to go through, but I don't know how to automate clicking on them in my JUnit/Robotium tests. Does anyone have any suggestions about what I can do to get the best of both worlds here?

You can make OnItem(Long)Click work if you set up the CheckBox views to non-focusable (i.e. setFocusable(false) and setFocusableInTouchMode(false), or their xml equivalents).
Note: The listeners will fire as long as you tap outside the checkbox itself. Tapping on the checkbox will toggle it only (but I guess this is what you would want).

Related

How to add a view to another view in a certain condition?

I'm working on an app and I want to add a view to the current view in a specific location if he checks a CheckBox but every time I try to add parent.addView(child) it crashes .. so I want to know how can I do this and how to release the view when the user clicks the CheckBox again.
That's a demo of what I want to make
https://xd.adobe.com/view/46d4808d-9888-455b-42ca-9fab587eb55d-30e5/
you can set the visibility in the xml file to gone and then set it back to visible when you click on the check box
xml:
android:visibility="gone"
jav:
view.setVisibility(View.VISIBLE)
You can create a Custom Component with the hidden content and reuse it in each checkbox. In addition, you can use a RecyclerView to add the checkbox. Or you can even create the custom component with the checkbox and hidden content and put all the logic there.

changing listview item elements

I'm new to Android development.
I need to make a listview with text and icon audio player. The icon has three States, "download", "downloading", "play" and "pause". The idea is that when populating the listview, set the "download" icon if the track is not downloaded and "play" if the track is downloaded". If a person clicks the "download" button the icon should be updated to "downloading" to start the download, then when complete, you should see the icon "play". If the user presses the "play" icon for this element should be updated to "pause".
I can't change the icon . I tried to change them in onItemClickListener and make the onClick in the getView method in adapter, but when scrolling the icons changed to the old. And if you select multiple items in the list, then change all the icons, and I need to change only one, for playing of the track.
I didn't show the code because there is nothing that can be corrected. - all wrong
if you have your own adapter try using a method notifyDataSetChanged();
That is happening due to recycling of views. If you click on an item and you will see multiple items clicked.
Use RecyclerView instead of listView, which will handle recycling by its own and its ViewHolder class takes care of the issue you are having.
And don't forget to notify the recyclerView adapter whenever you make some change in data source (i.e. arraylist)
Check out this example...
Hope it helps
http://hmkcode.com/android-simple-recyclerview-widget-example/

Appropriate way to invert/highlight a button in android

In my app I dynamically create a list of buttons. The user should be able to click on them. A short click would select the buttom as the current item, while a long click should enter the editor for this item.
Now I wonder how I can determine and set the colors that i should use. My first idea was to simply read the background and textcolor and switch them, however I'm not sure if this would be really the appropriate way to do this. So I was wondering if there is a an appropriate way of how to retrieve colors.
Of course I could hardcode some colors, but I don't know what color scheme the user has set and they might not be visible in a good way.
Please note, that, since I have to create the buttons dynamically, I can not set it in the XML.
You could simply use a ToggleButton, so android will take care of marking a "clicked" button as selected.
Since ToggleButton is a View, it has a setOnLongClickListener(Listener)-method, which can be used to make the ToggleButton long-clickable.

Disabling focus in TextView

In an Android application I have a ListActivity. After creating it I have a broadcast receiver which has to disable some items in the list. So I have a pice of code ike this one:
View child = getListView().getChildAt(i);
child.setEnabled(false);
It works in the way it changes the color of the disabled views to gray. But I need to avoid this items can be clicked. So I've tried to call
child.setFocusable(false);
or
child.setClickable(false);
but in both cases onListItemClick is called after pressing on a disabled option.
How can I avoid that onListItemClick method is called when these textviews are clicked?
Thanks.
Disabling the views does nothing with regard to the onListItemClick event.
The child must be disabled by the Adapter you are using to populate the list.
See http://developer.android.com/reference/android/widget/ListAdapter.html#isEnabled(int)
This method needs to be overriden to return false for each item you do not want to be clickable. You should also add a method to your adapter like setEnabled(int position, boolean enabled)

Android Row becomes Unclickable with Button

I have a listView, where each row has a button in the row layout. However, this seems to make the row itself unclickable. How can I make both the button and row clickable?
Thanks.
You need to set itemsCanFocus on the list like this:
mList.setItemsCanFocus(true);
To make the button clickable. Then you will need to use your own adapter and in getView return a view which is Clickable and focusable. You will also lose the default highlight states so you need to put them back in with the background resource. So do this:
view.setClickable(true);
view.setFocusable(true);
view.setBackgroundResource(android.R.drawable.menuitem_background);
to your view before returning your view.
Whenever I see posts concerning the android:focusable and android:clickable attributes, I always see them being both set to the same value at once. I figured there must be a reason if they are two separate attributes instead of being one.
It turns out that a much better way of achieving your desired behavior is to set
android:focusable="false"
or
yourButton.setFocusable(false)
on the Button in your View. Once you do that, you'll be both able to set an OnClickListener on the Button, and a click on the row will fire the onListItemClick() method in your OnItemClickListener.
Try to set your widgets to non clickable and non focusable in xml,the click on items will work normally and also the click on button will work normally.
android:clickable="false"
android:focusable="false"
Hope this helps.
Unfortunately I don't think that is possible. You ListView row can either have focusable widgets, like a button, or be clickable, not both. See link.

Categories

Resources