Separate stars in a ratingbar - android

I need to separate the stars in a RatingBar, it's possible? Or have I to create my own RatingBar?
<RatingBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="7"
android:id="#+id/estrellas2"
android:scaleX="1.2"
android:scaleY="1.2"
android:layout_below="#+id/textView2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="30dp"/>

Set
android:progressDrawable="#drawable/custom_ratingbar_selector"
property to customize rating bar .
and
Create custom_ratingbar_selector.xml in drawable and paste this code
<?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/unslected_star_icon" />
<item
android:id="#android:id/secondaryProgress"
android:drawable="#drawable/unslected_star_icon" />
<item
android:id="#android:id/progress"
android:drawable="#drawable/slected_star_icon" />
</layer-list>

Try this code,
android:progressDrawable = "#drawable/rat_star"
android:indeterminateDrawable = "#drawable/rat_star"
#drawable/rat_star :
<?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/starempty" />
<item android:id="#+android:id/secondaryProgress"
android:drawable="#drawable/starempty" />
<item android:id="#+android:id/progress"
android:drawable="#drawable/star" />
</layer-list>

Related

Changing color of drawable in selector

Is it possible change a color of drawable from selector?
My selector:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="#drawable/ic_plus_circle_grey600_48dp" android:state_pressed="true"/>
<item android:drawable="#drawable/ic_plus_circle_black_48dp"/>
</selector>
I try use that way, (in this case I'm using a selector on tint) but I'm getting some error:
<ImageView
android:id="#+id/btAdd"
android:layout_width="wrap_content"
android:src="#drawable/ic_plus_circle_black_48dp"
android:tint="#color/blue_android_focused"
android:layout_height="wrap_content"/>
This is the selector of tint:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#color/blue_android_pressed" android:state_pressed="true" />
<item android:color="#color/green" />
</selector>
Using tint on bitmap (** API 21+**):
Selector: drawable/selector_add
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<bitmap android:src="#drawable/ic_plus_circle_grey600_48dp"
android:tint="#color/blue_android_pressed" />
</item>
<item>
<bitmap android:src="#drawable/ic_plus_circle_grey600_48dp"
android:tint="#color/colorAccent" />
</item>
</selector>
In the ImageView
<ImageView
android:id="#+id/btAdd"
android:layout_width="wrap_content"
android:src="#drawable/selector_add"
android:layout_height="wrap_content"
/>
You can have the same drawables with different colors, that´s work for me
Your should use tint like this :
<ImageView
android:id="#+id/activity_register_et_fullname"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#drawable/bg_page"
app:backgroundTint="#color/blue_android_focused"/>
Use ImageView in AppCompatActivity, it will be inflated with AppCompatImageView (see also android.support.v7.app.AppCompatViewInflater). You should look at the code in it.

rating bar becomes to large and is cut-off when adding custom images in android

I followed an answer on stack overflow to adding a custom rating bar to an android project, here it is: https://stackoverflow.com/a/32828726/5909396
Just in case I am posting my code.
First I created ratingBar.xml in drawable folder:
<?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/secondaryProgress"
android:drawable="#drawable/ratingbar_empty" />
<item android:id="#android:id/progress"
android:drawable="#drawable/ratingbar_filled" />
Then I created ratingbar_empty.xml and ratingbar_filled.xml also in drawable folder:
<?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_emptyStar" />
<item android:state_focused="true"
android:state_window_focused="true"
android:drawable="#drawable/ic_emptyStar" />
<item android:state_selected="true"
android:state_window_focused="true"
android:drawable="#drawable/ic_emptyStar" />
<item android:drawable="#drawable/ic_emptyStar" />
<?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_fullStar" />
<item android:state_focused="true"
android:state_window_focused="true"
android:drawable="#drawable/ic_fullStar" />
<item android:state_selected="true"
android:state_window_focused="true"
android:drawable="#drawable/ic_fullStar" />
<item android:drawable="#drawable/ic_fullStar" />
And then I created the custom style in styles.xml
<style name="CustomRatingBar" parent="#android:style/Widget.RatingBar">
<item name="android:progressDrawable">#drawable/ratingbar</item>
<item name="android:minHeight">50dp</item>
<item name="android:maxHeight">50dp</item>
</style>
And finally I added a ratingBar to layout. The way I structured it is the rating bar is inside a relative layout and there is a textview inside the relative layout as well that is on top of the rating bar (The text view shows if there is no rating);
<RelativeLayout
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="0.4"
android:orientation="vertical"
android:layout_gravity="center">
<RatingBar
android:id="#+id/ratingRestaurant"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/CustomRatingBar"
android:numStars="5"
android:stepSize="0.1"
android:isIndicator="true"
android:max="5"
android:progress="3"
android:layout_centerVertical="true"
android:progressTint="#ffe4b331" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="No rating available"
android:textColor="#android:color/white"
android:textSize="11dp"
android:id="#+id/textViewNoRatingAvailable" />
</RelativeLayout>
When I run the application on the simulator, the stars are really large and are cut-off like below:
There are supposed to be five stars. How can i fix this problem?
I made a library with a customizable rating bar. You can set full and empty icon and their size. I hope this helps you.
This is an example of the usage, is very simple:
<com.kabuki5.logicalratingbar.CustomRatingBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:clickable="true"
app:iconSize="58dp"
app:imageEmpty="#drawable/ic_heart_empty"
app:imageFull="#drawable/ic_heart_full"
app:initRating="5"
app:maxItems="5" />
Also you can set a listener to get on change value callback.
Logical Rating Bar
set
android:layout_width="wrap_content"
android:layout_height="0dp"

My styled seekBar in android studio is not working as it should

I have a seek bar with the following xml file
<SeekBar
android:id="#+id/seekBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="100"
android:progress="50"
android:layout_margin="10dp"
android:progressDrawable="#drawable/styled_progress"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:thumb="#drawable/thumbler_small"
android:indeterminate="false" />
and in styled_progress I have
<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/progress"
android:drawable="#drawable/progress_cyan"/>
<item
android:id="#+id/secondaryProgress"
android:drawable="#drawable/progress_red"/>
</layer-list>
but I can see only the red part everywhere, the cyan part is not appearing.
Update your styled_progress.xml as, Because you are using wrong ids, you have to use
For Background
android:id="#android:id/background"
For Progress
android:id="#android:id/progress"
For secondaryProgress
android:id="#android:id/secondaryProgress"
and check yours.
<?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/progress_red"/>
<item
android:id="#android:id/progress"
android:drawable="#drawable/progress_cyan"/>
<item
android:id="#android:id/secondaryProgress"
android:drawable="#drawable/progress_red"/>
</layer-list>

customize toggle button on Android

I customized the Toggle button by using a drawable defined by using a selector. I use this drawable as background for the Toggle button.
<ToggleButton
android:id="#+id/mailbox:toggle_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_weight="1"
android:background="#drawable/toggle_background"
android:gravity="center_horizontal|center_vertical" />
The toggle_background is defined here:
<?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" />
<item
android:drawable="#drawable/img2"
android:state_checked="false" />
</selector>
The problem is that the image is always stretched. Is there a way to define an image for the two states that is not stretched?
What I need is a background that will be stretched and in the center of the button an icon that must not be stretched.
Is it possible?
This is my final solution.
In the layout:
<ToggleButton
android:id="#+id/mailbox:toggle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/toggle_drawable_layers"/>
in the toggle_drawable_layers.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="#drawable/toggle_drawable_bkg"></item>
<item android:left="10dp" android:drawable="#drawable/toggle_drawable_left"
android:gravity="center_vertical|center_horizontal" />
</layer-list>
in the toggle_drawable_left.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true">
<bitmap android:src="#drawable/bitmap_checked"
android:gravity="center_vertical|center_horizontal" />
</item>
<item android:state_checked="false">
<bitmap android:src="#drawable/bitmap_unchecked"
android:gravity="center_vertical|center_horizontal" />
</item>
</selector>
I did the same task this way, the button:
<ToggleButton android:id="#+id/mlAbout"
android:textOn="#string/about"
android:textOff="#string/about"
android:background="#drawable/ml_about" />
#drawable/ml_about:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="#drawable/list_bg_top"></item>
<item android:left="10dp">
<bitmap android:src="#drawable/waiting_status_btn"
android:gravity="center_vertical|left" />
</item>
</layer-list>
#drawable/list_bg_top background image that will be stretched and #drawable/waiting_status_btn is the icon the will not be stretched with the widget
Just use bitmaps instead of drawables in your selector like so (no need to use layers):
<?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" />
<item
android:drawable="#drawable/img2"
android:state_checked="false" />
</selector>
Also, don't forget to declare
android:textOff=""
android:textOn=""
in your ToggleButton declaration
There is another way to overcome the issue of stretching:
Just convert the image into a Nine-Patch image, where the stretchable areas are just on all sides of the image.
Then Android will stretch the image only in invisible areas, and keep the visible image unchanged.

I can't set imageLevel

I have ICS app with layout which use a ImageView with drawable list. I try to set imageLevel to it but pic doesn't change.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:maxLevel="0"
android:drawable="#drawable/main_activity_idle" />
<item
android:maxLevel="1"
android:drawable="#drawable/main_activity_upload" />
<item
android:maxLevel="2"
android:drawable="#drawable/main_activity_download" />
</layer-list>
<ImageView
android:id="#+id/main_panel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="6dp"
android:layout_below="#+id/main_panel_logo"
android:layout_alignParentRight="true"
android:src="#drawable/main__dr_list" />
statusIndicator = (ImageView) findViewById(R.id.main_panel_activity);
statusIndicator.setImageLevel(0);
Do you know why it happens?
You should use level-list instead of layer-list.

Categories

Resources