I have a problem that would be hard to explain. In my listview the list item's have imageviews. I would like to have an 'on click' or 'on pressed' effect that changes the background of the whole list row, and also the imageviews. I tried 2 approaches:
a) defining the xml selector state list for the row's layout
b) setting an onClickListener in java code
Option a) is no good because I can only change the items background and can't change the imageviews (defining a state list for imageviews is no good because it only works when the exact object is touched. and I want the effect whenever any location of the row is touched)
option b) works almost correct - i set the background resource for the layout of the list item and also change the image resource for imageviews. however, afterwards they stay like that. I am now wondering how can I change back to normal to achieve the effect of a short 'blink' in the java code. Just like with a button - you press it and it blinks orange for a split of a second.
PS: the permanent change of resources is important, because on press I open another activity with item's details and when I press the 'back button' the row looks like permanently selected.
I found a solution with using the OnTouchListener instead of onClickListener.
Related
I have a grid of buttons (using grid layout group). I created this by adding a UI element and duplicating it 9 times (10 buttons) and then just applying a grid layout group to the parent object while all of the buttons were still at 0,0,0. This laid out the buttons exactly as i wanted in the grid shape that i chose. When i run this on PC and click a button, it registers perfectly. When i build it down to an apk and put it on my android, the buttons don't click when you press on them, but rather when you press way off to the right in some empty field space. But the "clickable grid area" as i'll call it, is perfectly transposed into that open field space. So i have my 5x2 visible button elements on the screen. If i touch them, nothing happens. Randomly touch in empty space until one of the buttons activates, and the clickable grid is in the exact shape of the visible elements except not on top of the elements. I'm not 100% familiar with resolution settings yet but i don't think that has all too much to do with this. What am i missing and what settings can i change?
so the problem ended up being that the anchors were not set to center, they were set to top left. This was set automatically by the grid layout group component. Remove grid layout, set positions manually, all is well.
You need to define column and row for each button
I have 40 ImageViews inside a GridLayout, they have different colors and I want to know if user touched the desirable image or somewhere else(for example if user touched red image). How should I do that?
Set an OnClickListener for each view and store the views. In onClick you can check the view and know what ImageView was clicked. You could also think about changing the type to ImageButtons!
If you have further problems with your Grid not being able to be clicked, check this out: http://cyrilmottier.com/2011/11/23/listview-tips-tricks-4-add-several-clickable-areas/
TLDR: Disable focusing of the views in your layout.
If you manage your images in a collection maybe think about switching to gridview and implement an adapter with an itemClickListener. So depend on which item is clicked do you action.
How GridLayout items come from the Adapter(List/Image) in Android App
I'm having some problems to navigate on a gridView using the keyboard (with up, down, left and right keys).
I'm new in Android and maybe this question has a simple answer. I looked for a solution and anything could help me.
The problem is that I would like to change the way of navigation among items of a grid view. I would like to do it due to two reasons.
The first reason is related to the fact that some itens have clickable subitens (and consequently the entire item is not clickable). When the focus is changed from a item i to the item i+1, for example, the item i+1 must be checked if has subitens. If yes, the focus is changed to one of the subitens depending on direction (ie. if left to right, the first subitem on the left is focused).
The second reason is that in my application, the up arrow should works in the same way as the left arrow and the down arrow in the same way of the right arrow. So, I just need see if the keyCode of a onKey event is UP or DOWN and convert it to LEFT and RIGHT, respectively.
I thought that would be a simple task: override the onKeyDown method and treat these specificities, but I observed that my implementation of onKeyDown was invoked just in two cases: (1) when other keys are pressed (with exception of the aforementioned arrows and (2) when the grid view lost the focus. For example, if the item C1 is the current item selected and the user click to the left or down, the onKey is not invoked. Otherwise, if the right or up are typed, my method is invoked.
Is there any way to cancel this internal mechanism of grid view? I tried different ways, as follows:
(1) Implementation of OnKeyListener directly on the gridview and on the item (on the adapter) as well.
(2) Implementation of different callbacks to see which one would be called before this internal mechanism (like OnFocusChangeListener, OnKeyListener, OnItemSelectedListener). I tried both for the grid view and each item on the adapter.
(3) Instantiation of the gridview with the selector setted to a transparent image. When the desired item is reached on my keyEvent, I added a visible selector. It worked on the first event but when the visible selector is setted it does not back to the invisible selector.
I tried to be clear and I hope to anyone of you could help =)
Thank you so much
How are you?
You could try use onKeyUp instead of onKeyDown, see the answer below:
https://stackoverflow.com/a/20171618/1408986
And Android documentation:
http://developer.android.com/training/keyboard-input/commands.html
Hope this help.. bye!
I solved this problem by implementing both listeners: onKey and OnItemSelected. The focus was everytime running of the grid used and jusing those listeners solved my problem. Thanks a lot.
I've noticed that in most stock Android apps when you click on a View of pretty much any sort, the background of the View briefly flashes red before proceeding with whatever it's supposed to do.
I can't figure out how to do this. In some cases it seems to be built in, but not always. The Button objects I have in my Activity all flash red when clicked and I didn't have to do anything to make that happen. However, the items in my ListView do not. Each row in my ListView corresponds to a ViewGroup containing multiple other views. I want the whole row (ViewGroup) to flash red when it's clicked.
I should also point out that, functionally, everything is working properly. I click on my list item and it responds just as I designed it. It just doesn't flash red. Anyone know what I'm missing?
You could just add this to your main layout used for your items in your list :
android:addStatesFromChildren="true" android:background="#android:drawable/list_selector_background"
When one child is selected then all the parent (i.e your layout / the whole row as you said) will get selected, same for other states as pressed, etc. And the selector will apply a short transistion.
Btw, red is vendor dependent, on my Archos, it's a nice orange.
Regards,
Stéphane
I have an app whose default style is to present data in a GridView. When the user presses the Menu button they can choose to present the data as a ListView.
The problem is that once the first switch happens, it's as though the ListView is simply superimposed above the GridView. The GridView layout doesn't go away. I have to scroll with my finger a little bit before it disappears.
What I'm doing is basically setting another setContentView call to my XML file that defines the ListView. It's not just a simple matter of changing the adapters.
I am guessing that I need to completely destroy or remove my GridView--but how do I do this? Note that if I switch from ListView to GridView the problem remains.
Consider putting both in a ViewFlipper, rather than using setContentView().
for list
gridview.setNumColumns(1);
and for grid
gridview.setNumColumns(2);
What about playing with visibility?
When List is selected in menu, make gridview visibility to 'GONE',and list to 'VISIBLE'
and vice versa when Grid is selected in menu