Android RatingBar can't set rating - android

I'm trying to implement a RatingBar in my Android app but I'm running into some issues when trying to set the rating. My bar is always displayed as having the maximum available rating. Below are my files:
styles.xml
<style name="ratingBarStyling" parent="#android:style/Widget.RatingBar">
<item name="android:progressDrawable">#drawable/star_rating_bar</item>
<item name="android:indeterminateDrawable">#drawable/star_rating_bar</item>
<item name="android:minHeight">25dip</item>
<item name="android:maxHeight">27dip</item>
</style>
(same for v21)
star_rating_bar.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/background"
android:drawable="#drawable/star_rating_bar_full_empty" />
<item android:id="#+id/secondaryProgress"
android:drawable="#drawable/star_rating_bar_full_empty" />
<item android:id="#+id/progress"
android:drawable="#drawable/star_rating_bar_full_filled" />
</layer-list>
star_rating_bar_full_empty
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:state_window_focused="true"
android:drawable="#drawable/ic_star_empty" />
<item android:state_focused="true"
android:state_window_focused="true"
android:drawable="#drawable/ic_star_empty" />
<item android:state_selected="true"
android:state_window_focused="true"
android:drawable="#drawable/ic_star_empty" />
<item android:drawable="#drawable/ic_star_empty" />
</selector>
star_rating_bar_full_filled
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:state_window_focused="true"
android:drawable="#drawable/ic_star" />
<item android:state_focused="true"
android:state_window_focused="true"
android:drawable="#drawable/ic_star" />
<item android:state_selected="true"
android:state_window_focused="true"
android:drawable="#drawable/ic_star" />
<item android:drawable="#drawable/ic_star" />
</selector>
And finally the implementation of the RatingBar on the Fragment layout:
<RatingBar
style="#style/ratingBarStyling"
android:id="#+id/venue_card_rating"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:numStars="5"
android:stepSize="1.0"
android:rating="2.0"/>
Now for some reason I can see the filled star icons but non of the empty ones. I have also tried setting it programmatically but the results were the same. Any help would be greatly appreciated.
This is what I'm always getting: stars.png

You have made small mistake/typo
You will have to use android:id instead of providing a new id.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#android:id/background"
android:drawable="#drawable/star_rating_bar_full_empty" />
<item android:id="#android:id/secondaryProgress"
android:drawable="#drawable/star_rating_bar_full_empty" />
<item android:id="#android:id/progress"
android:drawable="#drawable/star_rating_bar_full_filled" />
</layer-list>

Related

Rating bar with custom drawable not showing half star only full stars

Hello I have a rating bar with custom drawable but it only displays full stars only and not showing half
here is my code, how can I solve this?
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#android:id/background"
android:drawable="#drawable/empty_star" />
<item
android:id="#+id/secondaryProgress"
android:drawable="#drawable/empty_star" />
<item
android:id="#android:id/progress"
android:drawable="#drawable/filled_star" />
<style name="doctor_rating" parent="#style/Widget.AppCompat.RatingBar">
<item name="android:minHeight">13dp</item>
<item name="android:minWidth">65dp</item>
<item name="android:numStars">5</item>
<item name="android:progressDrawable">#drawable/custom_rating_stars</item>
<item name="android:stepSize">.1</item>
</style>
<RatingBar
android:id="#+id/doctor_rating_bar"
style="#style/doctor_rating"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:layout_gravity="end"
android:rating="3.5"
/>
See below code this will help you.
custom_rating_stars.xml:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#android:id/background"
android:drawable="#drawable/ratingbar_empty" />
<item
android:id="#android:id/progress"
android:drawable="#drawable/ratingbar_filled" />
</layer-list>
ratingbar_empty.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:state_window_focused="true"
android:drawable="#drawable/star_grey_small" />
<item android:state_focused="true"
android:state_window_focused="true"
android:drawable="#drawable/star_grey_small" />
<item android:state_selected="true"
android:state_window_focused="true"
android:drawable="#drawable/star_grey_small" />
<item android:drawable="#drawable/star_grey_small" />
</selector>
ratingbar_filled.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:state_window_focused="true"
android:drawable="#drawable/star_yellow" />
<item android:state_focused="true"
android:state_window_focused="true"
android:drawable="#drawable/star_yellow" />
<item android:state_selected="true"
android:state_window_focused="true"
android:drawable="#drawable/star_yellow" />
<item android:drawable="#drawable/star_yellow" />
</selector>
Try this:
<style name="doctor_rating" parent="android.widget.RatingBar">
<item name="android:minHeight">13dp</item>
<item name="android:minWidth">65dp</item>
<item name="android:numStars">5</item>
<item name="android:progressDrawable">#drawable/custom_rating_stars</item>
<item name="android:stepSize">0.1</item>

How to change TabLayout.Tab's icon when the tab is selected?

How to change TabLayout.Tab's icon when the tab is selected?
try this
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/menu_live_stream_selected" android:state_checked="true" />
<item android:drawable="#drawable/menu_live_stream_selected" android:state_pressed="true" />
<item android:drawable="#drawable/menu_live_stream_selected" android:state_selected="true" />
<item android:drawable="#drawable/menu_live_stream_default" />
</selector>

Android: how can change the text color in custom view?

I have this view called ButtonMultiline:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:gravity="center">
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/ButtonMultilineCatalogTitle"
android:text="title"/>
<TextView
android:id="#+id/subtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/ButtonMultilineCatalogSubtitle"
android:text="subtitle"/>
I apply a background on this layout to change color on press:
<selector android:exitFadeDuration="200"
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:drawable="#drawable/btn_large_normal" />
<item android:state_pressed="true">
<color android:color="#color/background_black_top" />
</item>
<item android:state_focused="true" android:drawable="#drawable/btn_large_pressed" />
<item android:drawable="#drawable/btn_large_pressed" android:state_hovered="true" />
<item android:state_checked="true" android:drawable="#drawable/btn_large_normal" />
<item android:drawable="#drawable/btn_large_normal" />
All works fine until here.
I want to change the both TextView color on press too, but when I add this line in both TextView, there is a crash on inflate problem on this button_text_catalog.xml:
<android:background="#drawable/button_text_catalog">
My button_text_catalog.xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#color/white" android:state_pressed="true" />
<item android:color="#color/white" android:state_focused="true" />
<item android:color="#color/theme_primary"/>
Thanks guys!
Use this code:
button_text_catalog.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- pressed -->
<item android:state_pressed="true" android:color="#color/white"/>
<!-- focused -->
<item android:state_focused="true" android:color="#color/white"/>
<!-- activated -->
<item android:state_activated="true" android:color="#color/white"/>
<!-- checked -->
<item android:state_checked="true" android:color="#color/white"/>
<!-- default -->
<item android:color="#color/theme_primary"/>
</selector>
in Style.xml
add <item name="android:textColor">#drawable/button_text_catalog</item>

Android TextView change background image when pressed

I wanto to change the background-image of a textview when I click it. I have red about the selectors and I did like this:
selector.xml
<?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/img1" /> <!-- pressed -->
<item android:drawable="#drawable/img2" /> <!-- default -->
</selector>
and then in the layout file:
<TextView
android:id="#+id/textUp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:background="#drawable/selector"
android:clickable="true" />
but it doesn't work. I tried to change the bg color and in this case it works. What am I doing wrong?
You should create a new drawable xml like this:
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!--item android:drawable="#drawable/iv_circle_checked" android:state_pressed="true" />
<item android:drawable="#drawable/iv_circle_unchecked" /-->
<item android:drawable="#drawable/iv_circle_checked" android:state_enabled="true" android:state_pressed="true"/>
<item android:drawable="#drawable/iv_circle_checked" android:state_enabled="true" android:state_focused="true"/>
<item android:drawable="#drawable/iv_circle_checked" android:state_enabled="true" android:state_selected="true"/>
<item android:drawable="#drawable/iv_circle_unchecked" android:state_enabled="true" android:state_selected="false"/>
<item android:drawable="#drawable/iv_circle_unchecked" android:state_enabled="true"></item>
</selector>
When you create this, you should add in your background attr this xml.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/addgrnbtnhov"
android:state_pressed="true"
/>
<item android:drawable="#drawable/addgrnbtn" />
</selector>
please use different images.
// try this way
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/img1" android:state_checked="true" android:state_pressed="false"/>
<item android:drawable="#drawable/img2" android:state_checked="false" android:state_pressed="false"/>
<item android:drawable="#drawable/img1" android:state_checked="true" android:state_pressed="true"/>
<item android:drawable="#drawable/img2" android:state_checked="false" android:state_pressed="true"/>
</selector>
<CheckBox
android:id="#+id/chk"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello"
style="#android:style/Widget.TextView"
android:background="#drawable/selector"
android:gravity="center"
android:checked="true"
android:button="#null"/>
What you can do:
android:onClick="change"
In java:-
public void onCLick(View view){
TextView text=(TextView) findViewById(R.id.textUp);
text.setBackgroungResource(R.drawable.image);
}
Try using scaleType="fitXY"
<TextView
android:id="#+id/textUp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:scaleType="fitXY"
android:background="#drawable/selector"
android:clickable="true" />

Styled my buttons. Now what? I need to add icon

I was able to style button to my liking with pure XML (no image). Here is how it looks:
shape_button_normal_blue.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<corners android:radius="7dip"/>
<gradient android:startColor="#color/blue_start" android:endColor="#color/blue_end" android:angle="270" />
<stroke android:width="1dip" android:color="#80036990" />
<padding android:left="5dip" android:right="5dip" android:top="7dip" android:bottom="7dip"/>
</shape>
Similar to that I described other shapes for pressed/disabled/etc
Here is my selector_button_blue:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false" android:state_enabled="true"
android:drawable="#drawable/shape_button_normal_blue" />
<item android:state_window_focused="false" android:state_enabled="false"
android:drawable="#drawable/shape_button_disabled" />
<item android:state_pressed="true"
android:drawable="#drawable/shape_button_selected" />
<item android:state_focused="true" android:state_enabled="true"
android:drawable="#drawable/shape_button_focused_blue" />
<item android:state_enabled="true"
android:drawable="#drawable/shape_button_normal_blue" />
<item android:state_focused="true"
android:drawable="#drawable/shape_button_normal_blue" />
<item android:drawable="#drawable/shape_button_disabled" />
</selector>
And finally my style:
<style name="MyBlueButton" parent="#android:style/Widget.Button">
<item name="android:background">#drawable/selector_button_blue</item>
<item name="android:layout_marginLeft">1dp</item>
<item name="android:layout_marginRight">1dp</item>
<item name="android:layout_marginTop">5dp</item>
<item name="android:layout_marginBottom">5dp</item>
<item name="android:textColor">#color/white</item>
<item name="android:textSize">18dp</item>
<item name="android:textStyle">bold</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_width">fill_parent</item>
</style>
I'm using those buttons just fine with text. Happy and proud :)
Now I need to put icon on them. I see there is drawingTop/Bottom/Left/Right but it doesn't do what I need. What do I miss here? How do I make it nice and centered?
<Button style="#style/MyBlueButton" android:layout_marginTop="0dp" android:text="" android:layout_width="0dp" android:layout_weight="0.15"
android:drawableTop="#drawable/ic_accept"/>
use imagebutton, something like Edited
>
<ImageButton
android:id="#+id/call"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_accept"
android:background="#drawable/selector_button_blue"
/>

Categories

Resources