Set ImageView fo ListView Active State - android

Most apps that maintain active state in a menu listView also provide a small imageView visual in addition to color change.
How would you do this?
Would it be a part of the state_activated=true selector somehow, or should it be done programatcally in Java and perhaps in the ListAdapter?.
In the image below, you see on the Youtube app, the acte state has BOTH Background image change AND the visual indicator. (Red Line)

You can do that by make 9 patch image and use it as background when the row activate.

Related

In Android 4, how to do "toggle button" like behavior for other widgets?

I am new to Android, coming from a web app dev background. I have a HorizontalScrollView with a list of dynamically added ImageViews. When an image is clicked, it should be marked as selected, the image then should have an UI effect to reflect the state(a green border or a tick on bottom right corner), when it is clicked again, it should be deselected.
In web development, we could use css classes to reflect an item's ui state by checking whether it has a certain class.
How is it usually done in Android?
Thanks
Have you looked into ListView? There's no reason to write that kind of functionality all over again.

Button invisble but enabled

i designed my whole activity page in photoshop and opened it in eclipse.I want to put buttons on my fake image buttons. Doesnt it make any problem? I use the code below to make the button invisible but it disables button too. How can i fix this problem?
android:visibility="invisible"
You should not do like that (having a fake ImageButton on a View you want to make clickable), as many error can occur with doing it. Assuming you are trying to put this button on a whole ImageView, or a whole Layout, or at least a whole view, you should consider adding a click listener to this view. Try to find more information about how to use the View.setOnClickListener(View.OnClickListener) method. You will have less error to debug with it.
But if you still want to do, try instead using android:alpha property:
android:alpha="0"
It will make the button become invisible, but still present, and still active
This android:alpha attribute allows you to specify an opacity value for a color.
To be honest you shouldn't be doing that unless you are testing something or prototyping. Instead, you should cut your design in photoshop and export the images for your buttons.
Your approch will most likely cause the buttons to be positioned incorrectly above the image when run on different devices with different screen sizes.
The correct way to do this would be to use the ImageButton view with a StateListDrawable set as the source image.
You can read more about State Lists here:
http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList
It's very easy to declare it in XML and have different images for various button states (pressed, disabled, focused, ...).
To sum it all up. I would do the following things:
Export images for my buttons in all the different states that you prepared (i find neutral and pressed as an absolute minimum).
Import them as drawables in your project (be sure to generate different versions for every screen density you want to support).
Create a State List Drawable XML Resource as described here.
Set the resource created in step 3 as the android:src property of your ImageButton (setImageResource(int)).
Try android:background="#null" for your button. It will make that default gray background disappear, making your button invisible but still allowing you to add text etc if you want to.
That being said: I would not recomment a button here. I would prefer Pauls answer in your particular case.

Is it possible to have a selected and unselected state of a single image placed in an imagebutton?

I have an image for a button. Here it is:
My task is to create a selected and unselected state of this image. This image will be my unselected state.
Using the same image, my task is to create a selected button with darker background. Is it possible to darken the image in Java?
NOTE:
I have knowledge that I can do this my using an external software and have another image for selected button. However, should my idea be real and was already implemented in Android, it would actually benefit more people. So, upon research, I have not found possible ways to do this. If this is possible, surely I have missed something great.
Ideally you can have a darker image and when clicked in onClick() of onClickListener you can set the alpha setAlpha of this image to 0.5 or you need to implement selectors with two different images/states.

Photoshop created button won't fill entire background of Android button widget

I have created a shiny button in photoshop and now wish to use it for the background of a button on an android layout. Every time I change the buttons buttons background property it inserts the button but it does not fill the entire background but instead seems to push the boundaries of the button away from it as if there were huge amounts of padding! I have not altered the buttons padding properties either! I have previously been successful in filling the buttons entire background with buttons already created that I downloaded from the internet but I dont know if the were created in photoshop......however I did resize these buttons in photoshop and then successfully use them. Please see the image attached and notice the blue outline of the button whose background I am trying to fill! Any help is greatly appreciated!!!!
Try this: http://developer.android.com/guide/developing/tools/draw9patch.html

Easier alternative for showing Button as pressed or not than Selector?

I have an application with a large number of buttons across the various screens. Each of the buttons has a background image as well as text (so ImageButton is no good). I am aware that it's possible to have multiple versions of an image for the different states a button can be in (e.g. pressed and not pressed), that it's possible to put the different versions of the image in a selector, and then to set the background of the button to be the selector. However, this means I need to create two versions of each image (times four for each of the different densities I'm considering) and thus bloating the size of my apk as well as adding considerable time to produce the duplicate images.
Is this the only way to change the look of a button for when it's pressed or not (i.e. to set its background to be a selector which links to two different images), or is there some other easier way to change the look of a button, like specifying some built-in property of the button (its 'darkness' for example)?
Found one way of showing a View as pressed or not by wrapping the View in a FrameLayout, giving the FrameLayout a foreground drawable which is a selector consisting of a black-ish transparent colour (pressed state) and fully transparent colour (default state), and then setting the click listener on the FrameLayout instead of the View. Wrote it out in greater detail on my Blog here:
http://adilatwork.blogspot.co.uk/2012/12/android-show-view-as-pressed-without.html

Categories

Resources