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.
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 know I can use the android.R.attr. for default values (like colors, styles, etc.)
Does anyone know which android.R.atrr. I need to use for the default ListView Item background color?
When I look at the available android.R.atrr. I see there is colorFocusedHighlight, colorLongPressedHighlight, colorMultiSelectHighlight and colorPressedHighlight, but not one for just the item that isn't pressed/focussed/etc.
It's actually a drawable, not a color like I mentioned in my comment.
http://developer.android.com/reference/android/R.drawable.html#list_selector_background
I am new to android and try to create a simple color picker.
The idea is to have a few filled circles showing a color and the selected one should have an circle around it.
That didn't sound too hard. I draw the circles with <shape>, create two resources, one with only a filled circle, one with the filled circle and an transparent circle with a solid stroke. Then I created a <selector> and set this as the background of my <RadioButton>.
This worked fine for one color, however now I'd like to have the same styles, but with a different color for each <RadioButton>.
The only solotution I could come up with is to create this triplet of xml-files for each color. This would certainly work, but it strikes me as extremely inelegant.
I tried to access the background of the buttons, but it seems I can only access the <selector>, not it's children.
I also tried to create a FrameLayout that would parent the RadioButton and an ImageView so the RadioButton only needs to display the border. This didn't fully work. I could select a RadioButton, but it wouldn't get deselected upon selecting another one.
I guess the best solution is to have a background that can change itself according to the state of the button, just like <selector> does, but with a more comprehensive way of determining which drawable to use, at least with access to the tag-Property of the RadioButton, but I cannot see how to do this.
check this github repo
https://github.com/VishalJogiya/CustomRadioAndShapes
xml layout code
<customradio.vj.com.library.CustomRadio
android:id="#+id/radio9"
android:layout_width="#dimen/thirty_two_dp"
android:layout_height="#dimen/thirty_two_dp"
android:layout_marginBottom="#dimen/eight_dp"
android:layout_marginLeft="#dimen/sixteen_dp"
android:layout_marginRight="#dimen/sixteen_dp"
android:layout_marginTop="#dimen/eight_dp"
custom:radioColor="#AA00FF"
custom:radioShape="simple_circle2" />
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.
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.