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>
Related
I'm trying to change the fontFamily of a CalendarView in xml, but it's not happening. Below is the code :
CalendarView :
<CalendarView
android:id="#+id/calendar_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:dateTextAppearance="#style/CalendarViewTheme"
/>
styles.xml :
<style name="CalendarViewTheme" parent="#android:style/TextAppearance.Small">
<item name="android:textSize">14.5sp</item>
<item name="android:textColor">#color/calendar_text_color</item>
<item name="android:fontFamily">#font/sf_pro_semi_bold</item>
</style>
calendar_text_color.xml :
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#color/colorWhite" android:state_selected="true" />
<item android:color="#color/colorWhite" android:state_focused="true" />
<item android:color="#color/colorWhite" android:state_pressed="true" />
<item android:color="#color/colorWhite" android:state_activated="true" />
<item android:color="#color/colorHints" android:state_enabled="false" />
<item android:color="#color/colorBlack" />
</selector>
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>
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>
I am using a background 9 patch image name button_normal.9.png. The image is not showing as a background image of button. Got stuck for 5 hours still couldn't find it. Please help. Here are the respective files.
This is button:
<Button
android:id="#+id/s_id"
android:text="#string/s_id_string"
android:layout_marginTop="20dp"
style="#style/MainScreen"/>
Style MainScreen:
<style name="MainScreen">
<item name="android:layout_width">200dp</item>
<item name="android:layout_marginTop">10dp</item>
<item name="android:textSize">15dp</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textStyle">bold</item>
<item name="android:background">#drawable/button_shape</item>
<item name="android:textColor">#drawable/button_text_color</item>
</style>
button_shape file:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="#drawable/button_shape_normal"
android:background="#drawable/button_normal"
android:state_pressed="false"/>
<item
android:drawable="#drawable/button_shape_pressed"
android:state_pressed="true"
android:background="#ed2020"
/>
button_text_color file:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false" android:color="#ed2020"/>
<item android:state_pressed="true" android:color="#ffffff"/>
button_shape_pressed & button_shape_normal file is same:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="30dp" />
try this button_shape file:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="#drawable/button_shape_pressed"
android:state_pressed="true"
android:background="#ed2020" />
<item
android:drawable="#drawable/button_shape_normal"
android:background="#drawable/button_normal" />
</selector>
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"
/>