Problem in implementing Android rating bar style - android

I want to make style for rating bar programmatically. I've read this tutorial
Pretty Rating Bar
I can do layerList and selector and I can link them programmatically but I can't link the layerList I made with The style attribute programmatically so I can set the style attribute in the rating bar constructor with my own style
RatingBar rb=new RatingBar(getBaseContext(), null, defStyle);.
to make custom rating bar I need to do the following :-
1-make 2 xmls one for the full_empty state and another one for full empty they both include selectors
I made this for example for the fullFilled state:
StateListDrawable state_fullFilled = new StateListDrawable();
state_fullFilled.addState(new int[] {android.R.attr.state_pressed},
getResources().getDrawable(R.drawable.star_rated));
state_fullFilled.addState(new int[] {android.R.attr.state_focused},
getResources().getDrawable(R.drawable.star_rated));
state_fullFilled.addState(new int[] {android.R.attr.state_selected },
getResources().getDrawable(R.drawable.star_rated));
this for fullEmpty:-
StateListDrawable state_fullEmpty = new StateListDrawable();
state_fullEmpty.addState(new int[] {android.R.attr.state_pressed},
getResources().getDrawable(R.drawable.rate));
state_fullEmpty.addState(new int[] {android.R.attr.state_focused},
getResources().getDrawable(R.drawable.rate));
state_fullEmpty.addState(new int[] { android.R.attr.state_selected},
getResources().getDrawable(R.drawable.rate));
2-then I should make layerList of those drawables I've made like this:-
Drawable[] d=new LayerDrawable[2];
d[0]=state_fullEmpty;
d[1]=state_fullFilled;
LayerDrawable layer=new LayerDrawable(d);
3-I should make this step programmatically But I can't:-
<style name="foodRatingBar" parent="#android:style/Widget.RatingBar">
<item name="android:progressDrawable">#drawable/food_ratingbar_full</item>

Related

BottomNavigation view ripple color effect

I've seen this ripple color effect on Material Calculator app, on the Google Play and now on the BottomNavigation view.
How can I make this color effect starting from touch?
Gif: https://d13yacurqjgara.cloudfront.net/users/72535/screenshots/2673294/bottom_navigation_material_design_by_jardson_almeida.gif
I think it will be easier if you use style:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorControlHighlight">#color/ripple_material_dark</item>
</style>
If you know how to make simple ripple, then here is code to change color of it:
RippleDrawable rippleDrawable = (RippleDrawable)view.getBackground(); // assumes bg is a RippleDrawable
int[][] states = new int[][] { new int[] { android.R.attr.state_enabled} };
int[] colors = new int[] { Color.BLUE }; // sets the ripple color to blue
ColorStateList colorStateList = new ColorStateList(states, colors);
rippleDrawable.setColor(colorStateList);
Both answers do the work but I found a library which simplifies the work when using BottomNavigation:
https://github.com/roughike/BottomBar

Setting StateDrawableList to ImageButton in code have different results from setting via xml?

I have next result:
result
Right buttons:
android:adjustViewBounds="true"
android:background="#android:color/transparent"
android:scaleType="centerInside"
android:src="#drawable/xml_menu_button"
xml_menu_button:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="false" android:drawable="#drawable/bottom_button_menu"></item>
<item android:state_pressed="true" android:drawable="#drawable/bottom_button_menu_pressed"></item>
</selector>
Second button have similar xml as config.
Left buttons added to the panel dynamically. So I set its resources via code:
ImageButton button = new ImageButton(getActivity());
button.setBackgroundColor(Color.TRANSPARENT);
button.setAdjustViewBounds(true);
button.setScaleType(ScaleType.CENTER_INSIDE);
StateListDrawable states = new StateListDrawable();
states.addState(new int[] {android.R.attr.state_pressed},
new BitmapDrawable(getResources(), PATH_TO_BUTTON_PRESSED_IMAGE));
states.addState(new int[] { },
new BitmapDrawable(getResources(), UPATH_TO_BUTTON_IMAGE));
button.setImageDrawable(states);
Panel is custom ViewGroup. With layout bounds turned on, I can see, that measuring and layout stage passed as I want. But there is a difference between image sizes, as you can see.
All buttons have the same size 70x58 px. In layout buttons have size 77x77.
What am doing wrong?

Change parent's background color when its child is clicked

I have a TableRow which contains a LinearLayout, and then this LinearLayout contains a TextView. What I want is, once the TextView is clicked, the entire TableRow would change its background color.
I've tried to use getParent() and performClick() to pass the click event from TextView to TableRow. The onClick() method of TableRow does get called, but its background color does not change.
Of course I've set the selector by using either
row.setBackgroundResource(R.drawable.menu_item_bgcolor);
or
row.setBackgroundDrawable(activity.getResources().getDrawable(R.drawable.menu_item_bgcolor));
Doesn't work. Can anyone provide any insight into this? Thanks,
Below is the selector xml file:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#drawable/menu_item_pressed" />
<item android:state_focused="true" android:drawable="#drawable/menu_item_pressed" />
<item android:drawable="#drawable/menu_item_normal" />
</selector>
try this
StateListDrawable states = new StateListDrawable();
states.addState(new int[] {android.R.attr.state_pressed},
getResources().getDrawable(R.drawable.pressed));
states.addState(new int[] {android.R.attr.state_focused},
getResources().getDrawable(R.drawable.focused));
states.addState(new int[] { },
getResources().getDrawable(R.drawable.normal));
row.setImageDrawable(states);

Android button states programmatically in Java (not XML)

How does one define Android button image for the "state_pressed"
"android:state_focused" in Java?
For example, how would one accomplish the equivalent in Java for the XML from
http://developer.android.com/reference/android/widget/ImageButton.html
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="#drawable/button_pressed" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="#drawable/button_focused" /> <!-- focused -->
<item android:drawable="#drawable/button_normal" /> <!-- default -->
</selector>
Just use addState method of StateListDrawable
StateListDrawable stateListDrawable = new StateListDrawable();
stateListDrawable.addState(new int[] {android.R.attr.state_pressed},
getResources().getDrawable(R.drawable.phone));
You can use constants below for the first parameter of this method
android.R.attr.state_accelerated
android.R.attr.state_activated
android.R.attr.state_active
android.R.attr.state_drag_can_accept
android.R.attr.state_drag_hovered
android.R.attr.state_enabled
android.R.attr.state_first
android.R.attr.state_focused
android.R.attr.state_hovered
android.R.attr.state_last
android.R.attr.state_middle
android.R.attr.state_pressed
android.R.attr.state_selected
android.R.attr.state_single
android.R.attr.state_window_focused
Create an instance of StateListDrawable and then assign it with imagebutton.setImageDrawable(stateDrawable).
10x to Tang Ke, I`m using this for different list item color whit selection color.
selected state
stateListDrawable.addState(new int[] {android.R.attr.state_pressed},
new ColorDrawable(getResources().getColor(R.color.alpha_blue500)));
default state
stateListDrawable.addState(new int[] {},
new ColorDrawable(getResources().getColor(R.color.red)));
Here you can change color for different state of the row item (ex. paid vs free)
set state to custom layout row item in list adapter
holder.relativeLayout.setBackgroundDrawable(stateListDrawable);

Android : How to update the selector(StateListDrawable) programmatically

I want to update the selector for a button programmatically.
I can do this with the xml file which is given below
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false"
android:drawable="#drawable/btn_off" />
<item android:state_pressed="true"
android:state_enabled="true"
android:drawable="#drawable/btn_off" />
<item android:state_focused="true"
android:state_enabled="true"
android:drawable="#drawable/btn_on" />
<item android:state_enabled="true"
android:drawable="#drawable/btn_on" />
</selector>
I want to do the same thing programmatically. I have tried something like given below
private StateListDrawable setImageButtonState(int index)
{
StateListDrawable states = new StateListDrawable();
states.addState(new int[] {android.R.attr.stateNotNeeded},R.drawable.btn_off);
states.addState(new int[] {android.R.attr.state_pressed, android.R.attr.state_enabled},R.drawable.btn_off);
states.addState(new int[] {android.R.attr.state_focused, android.R.attr.state_enabled},R.drawable.btn_on);
states.addState(new int[] {android.R.attr.state_enabled},R.drawable.btn_on);
return states;
}
but it didnt work.
And how to set android:state_enabled="false" or android:state_enabled="true" programatically.
You need to use the negative value of the needed state.
E.g.:
states.addState(new int[] {-android.R.attr.state_enabled},R.drawable.btn_disabled);
Notice the "-" sign before android.R.attr.state_enabled.
Very late response here but in case anyone else is having problems setting a StateListDrawable programmatically. Then as with the XML files the order in which you set the states into the StateListDrawable is important.
For example this will work as expected:
StateListDrawable sld = new StateListDrawable();
sld.addState(new int[] { android.R.attr.state_pressed }, new ColorDrawable(Color.GRAY));
sld.addState(new int[] {}, new ColorDrawable(Color.GREEN));
This won't:
StateListDrawable sld = new StateListDrawable();
sld.addState(new int[] {}, new ColorDrawable(Color.GREEN));
sld.addState(new int[] { android.R.attr.state_pressed }, new ColorDrawable(Color.GRAY));
I am going to answer your question "How to update the selector for a BUTTON programmatically?" by proposing to switch a button for a LinearLayout with embedded ImageView and TextView. There are a number of benefits to doing this, especially if you will later decide to customize your views. There is no loss of functionality resulting from this switch. You will still be able to attach same event listeners you can attach to a button, but will be able to avoid the buttons/tabs styling nightmares. Here is a relevant code from the layout.xml
<LinearLayout
android:id="#+id/button"
style="#style/ButtonStyle">
<ImageView
android:id="#+id/background"
android:src="#drawable/custom_image"/>
<TextView
style="#style/TextStyle"
android:text="Custom Button"
android:id="#+id/text"/>
</LinearLayout>
Next, I have a selector file called custom_image.xml located in the drawable folder. Here is the content of the selector file
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/disabled_img" android:state_enabled="false" />
<item android:drawable="#drawable/unselected_img" android:state_selected="false" />
<item android:drawable="#drawable/selected_img" android:state_selected="true" />
</selector>
The three source image files (disabled_img.png, unselected_img.png, selected_img.png) are also located in the drawable folder.
Now back to your Java code. There is no need for the funky StateListDrawable garbage for many reasons. First, it just looks ugly, and is hard to maintain. But most importantly it goes against the principles of keeping your logic separate from your presentation. If you are managing your drawable resources in Java code, you know you are doing something fundametally wrong.
Here is what I am proposing instead. Whenever you want your button to be selected, you just pop this one-liner in there:
((LinearLayout)findViewById(R.id.button)).setSelected(true);
Or whenever you want the button to be in the disabled state, here is another one-liner:
((ImageView)findViewById(R.id.background)).setEnabled(false);
Please notice that in this last example I am specifying the disabled state on the ImageView inside the LinearLayout. For some reason whenever you change the enabled / disabled state of the LinearLayout, the selector is not being triggered. It works fine when you do it on the ImageView instead.
Not enough rep to comment but the accepted answer did not work for me.
My goal was for designers to set enabled, disabled, and pressed background colors on some button. I intended for them to use it to test colors on different displays.
I noticed some other people mentioned it did not work for them either.
The states that need to be defined are
not pressed and enabled, this is what you see when the button is enabled and not pressed.
pressed and enabled, this is what you see when the button is pressed and enabled
not enabled. This is what you see when the button is disabled
Here is some code that will give you an idea of the states. I am using Color.Parse() to generate ints for the colors then I pass them to this method to get the StateListDrawable.
private StateListDrawable createDrawable(int enabled, int pressed, int disabled) {
StateListDrawable stateListDrawable = new StateListDrawable();
stateListDrawable.addState(new int[] { -android.R.attr.state_pressed, android.R.attr.state_enabled }, new ColorDrawable(enabled));
stateListDrawable.addState(new int[] { android.R.attr.state_pressed, android.R.attr.state_enabled }, new ColorDrawable(pressed));
stateListDrawable.addState(new int[] { -android.R.attr.state_enabled }, new ColorDrawable(disabled));
return stateListDrawable;
}
I don't know how you are adding the StateListDrawable, since the code is not here. But be sure to be check the documentation and the adding the setState().
You can set the properties from the View, such as yourView.setEnabled(true)
I hope that helps

Categories

Resources