Plenty of tutorials on the web for that:
http://www.softwarepassion.com/android-series-custom-listview-items-and-adapters/
http://www.josecgomez.com/2010/05/03/android-putting-custom-objects-in-listview/
http://androidguys.com/2008/07/14/fancy-listviews-part-one/
For that particular case, my guess is that, next to the icon, there is a RelativeLayout with three TextViews, and using a custom background drawable. That drawable will define different background colors with a selector depending on the android:state_selected and/or android:state_pressed states.
Related
So I have many LinearLayouts and RelativeLayouts that I need to apply shadows to. But all these layouts will have different background color.
For example, I have 3 LinearLayouts. I need one Blue, One Green and One Red but all will have the same shadows and style (except for the color).
I already have these layouts created so what can I do to apply shadows to them manually?
If I create an XML drawable then I have to create a different drawable for each color. Is there any other way I can do this?
I was thinking about making a common function where I can pass either a LinearLayout or RelativeLayout and apply the shadow effects with a specific solid color as background.
But I am not sure if there is a way to apply shadow programmatically. Please let me know what my choices are.
Thank you for your time.
Sounds like you need Outline. Since you mention that you want to do this programmatically Outline will do the trick. Fetch your layouts by findViewById(), use getOutlineProvider() from your layouts, get the ViewOutlineProvider then use the getoutline() method to set a custom Outline object that you will create as a shadow to any View that you desire. (also check this from the official documentation)
As for what your choices are I would have to say that generally speaking you have 2 choices.
1) Implement the shadows via XML declarations as a property of any other View that you inflate
2) Do it programmatically with Outline.
There is no such attribute in Android, to show a shadow. But possible ways to do it are:
1)Add a plain LinearLayout/RelativeLayout with colors, over which add your actual layout, with margin at bottom and right equal to 1 or 2 dp
2)Have a 9-patch image with a shadow and set it as the background to your Linear layout/RelativeLayout
I'm looking to add new states to a TextView. I chose TextViews because they're a relatively simple controls and I want use this in a widget so I really want to cut down on nested controls as much as possible. I need to define custom states for my TextViews. States like, not_started, in_progress, completed, missed, did_not_do, etc...
I want the background and font color to change with the states. How can I do this?
I can use a Level-List selector to handle my "custom states" and define my states as integers.
http://developer.android.com/reference/android/graphics/drawable/LevelListDrawable.html
I've jumped around looking for a proper solution to this problem, but can't find one. The answer might be that it's impossible, but I'm going to try one more time, even though many similar questions have been asked.
I have a view (it happens to be a RatingBar, but it could be a button or a TextView or even an ImageView) that I want a custom style for. Depending on something, I want it to (for example) use Red Stars, or Blue Stars, or Orange Stars. I want to use this view in a list, with a custom list adapter, so that based on some attribute in the underlying list data, the item list view shows either the Red-Star RatingBar, or the Blue-Star RatingBar, or the OrangeStar RatingBar.
I've attempted building a style from code (choosing the right drawables that reflect the color stars I want), and then having the custom ListAdapter create a new RatingBar with the appropriate style (since a view's style can't be changed after the view is created), but when I do that the RatingBar never shows up. I'm not sure if this is expected, or if I have something else wrong.
It's like I want a selector for my image selector, or maybe another (custom?) attribute for the selector I already have.
If I create a CustomView, that extends RatingBar, that has an extra attribute, would an xml-defined Selector be able to access that attribute and use it to select a different image?
I'd like to figure this out, because the only alternative I can come up with is to not use a ListView with a ListAdapter, which basically results in me writing more custom code.
Thoughts?
Have you tried using themes? You can define a theme for the RatingBar in styles.xml. Within this RatingBar theme you can define a android:progressDrawable, which will refer to a drawable. Create multiple themes, that each point to a different drawable. Having these different themes, you will be able to programmatically set the RatingBar Widget android:style attribute to support these themes (red, blue, green) that you create.
The problem : I've changed my app's background color to white, and now I cannot see the ExpandableListView's group indicator anymore.
ExpandableListView has methods to change text color, background color, and divider color, but nothing I can see for the group indicator color.
I do not want to provide custom drawables, I just want to change the color of the drawables that are provided by Android, programmatically.
I feel there must be some simple way to do this.
You will have to use custom drawables here. You can't change the android drawables provided by Android.
When using the "selector" to specify different images for buttons for different states, such as pressed, focused etc, do I have to write an xml file for each button? I have about 15-20 buttons in my app, so was wondering if there is a way to write just one xml and refer to parts of it?
Thanks
Chris
There is no way to refer to parts of a StateListDrawable, at least that I am aware of.
However, since StateListDrawables are usually used for button backgrounds, it is unclear why you need more than one in the first place.
StateListDrawable changes the Drawable based on the current state, as state changes at runtime. Choosing what Drawable to use initially is something you already do when specifying the Drawable in the first place.
Even classes like LevelListDrawable still require you to specify the level to the Drawable, not on the actual View the Drawable is used on and AFAIK Android automatically checks if a Drawable can handle states and if so passes them. The Drawable never gets reference to the View the Drawable is being used on.
I would just create multiple Drawable files for each button. If you want to share certain attributes of the Button like text color, padding, font sizing, etc you should use Android styles.
Android styles would let you have styles like BlueButton, RedButton, GreenButton which you can inherit styles. So you could have BlueButton that sets the text color, text size, text shadows, the drawable for blue etc, then create another style for Red that just inherits BlueButton and only changes the Drawable (although it can change any attribute it wishes) and then just use them on your Button widgets. You would still need to have multiple Drawable files for the styles to link to, but the styles can all be in one file.