I am trying to write code based on which I should update the rating in steps of 1 as I am getting integer values for Rating. Here is the code for the same
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center_vertical|center_horizontal"
android:layout_marginRight="3dp" >
<RatingBar
android:id="#+id/PriceRating"
style="#style/foodRatingBarSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:isIndicator="true"
android:numStars="1"
android:stepSize="0.25"
android:max="5" />
</LinearLayout>
Here is the code in the style file:
<style name="foodRatingBarSmall" parent="#android:style/Widget.RatingBar">
<item name="android:progressDrawable">#drawable/foodratingbar_full</item>
<item name="android:minHeight">20dip</item>
<item name="android:maxHeight">20dip</item>
</style>
Here are the foodratingbar file
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/background" android:drawable="#drawable/foodratingbar_fill"
/>
<item android:id="#+id/secondaryProgress"
android:drawable="#drawable/foodratingbar_fill" />
<item android:id="#+id/progress" android:drawable="#drawable/foodratingbar_empty" />
</layer-list>
And here is the code for foodratingbar_fill:
<?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/dollar" />
<item android:state_focused="true"
android:state_window_focused="true"
android:drawable="#drawable/dollar" />
<item android:state_selected="true"
android:state_window_focused="true"
android:drawable="#drawable/dollar" />
<item android:drawable="#drawable/dollar" />
</selector>
where dollar is a png image
The problem with my code is that I cannot get it to update to the required number of stars and I see vague values of the number of stars being displayed on android. I would like to know how I can display one dollar image for 1 rating with a maximum of 5
Related
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"
I want to have an onClick that the chatlist-item changes it's color when clicked/focused. I don't want to use any Java code if possible.
I tried this:
chatlist_layout.xml
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="false"
android:layout_alignParentRight="false"
android:background="#drawable/round_corners"
android:listSelector="#drawable/selector">
</ListView>
selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:drawable="#color/ripple_material_light" /> <!-- focused -->
<item android:state_focused="true" android:state_pressed="true" android:drawable="#color/green_hsrt_1_default_CMYK_100_10_55_0" /> <!-- focused and pressed-->
<item android:state_pressed="true" android:drawable="#color/grey" /> <!-- pressed -->
<item android:drawable="#color/black" /> <!-- default -->
</selector>
what am I doing wrong?
Any kind of help will be greatly appreciated
The problem may be in ListView background. So try to add android:drawSelectorOnTop="true"
Hi I am using custom rating bar with custom images. I need to provide space between stars. When I increase minimum height and maximum height, star image remains same.
<RelativeLayout
android:id="#+id/RelativeLayout01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:padding="5dp">
<TextView
android:id="#+id/allreviews"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="Rating"
android:layout_marginLeft="8dp"
android:textSize="14sp" />
<RatingBar
android:id="#+id/reviewallrating"
style="#style/customRatingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="3dp"
android:layout_toRightOf="#+id/allreviews"
android:isIndicator="false"
android:numStars="5"
android:stepSize="0.1" />
</RelativeLayout>
styles:
#drawable/star_rating_bar_full
12dip
12dip
star_rating_bar_full.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/star_ratingbar_full_empty" />
<!-- <item android:id="#+android:id/secondaryProgress"
android:drawable="#drawable/star_ratingbar_full_empty" /> -->
<item android:id="#+android:id/progress"
android:drawable="#drawable/star_ratingbar_full_filled" />
</layer-list>
star_ratingbar_full_empty:
<?xml version="1.0" encoding="utf-8"?>
<!-- This is the rating bar drawable that is used to
show a filled cookie. -->
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:state_window_focused="true"
android:drawable="#drawable/star_gray" />
<item android:state_focused="true"
android:state_window_focused="true"
android:drawable="#drawable/star_gray" />
<item android:state_selected="true"
android:state_window_focused="true"
android:drawable="#drawable/star_gray"/>
<item android:drawable="#drawable/star_gray" />
</selector>
star_ratingbar_fullfilled.xml:
<item android:state_pressed="true"
android:state_window_focused="true"
android:drawable="#drawable/yellow_star" />
<item android:state_focused="true"
android:state_window_focused="true"
android:drawable="#drawable/yellow_star" />
<item android:state_selected="true"
android:state_window_focused="true"
android:drawable="#drawable/yellow_star" />
<item android:drawable="#drawable/yellow_star" />
I used style="?android:attr/ratingBarStyle" in styles but still output remains same.
You could simply add some empty space to the left and right sides of your PNGs.
That is, increase the width of the grey and yellow PNGs in GIMP (or whatever graphic editor program you use).
Just the canvas, without stretching the star icon.
If you are using Drawable for the stars. Make sure your drawable have some left padding according to your requirement in the png image.
Otherwise through code its not working. Some days back I stuck with the same problem. I gone through by using the left padded image. Hope it will work.
I need to design a this type button which should have a background with another small image in the center and the text at the bottom.
While the current I am using is a textview as below
<TextView
android:id="#+id/homeButton1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="#ffffff"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:gravity="center"
android:text="Button"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#drawable/button_color"
android:textStyle="bold" />
But i cannot put an image in its center.
If I use android:drawableTop
this is the image I get
How can this be feasible?
try compound drawables here is an example
<TextView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="#drawable/image"
android:gravity="center"
android:text="#string/text" />
output:
Put a bellow line to your TextView xml and change "img_src" to your image.
android:drawableTop="#drawable/img_src"
UPDATE 1 :
drawableTop only support to draw above the text.
If you want to draw image on the center, here is a sample code.
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/background_image" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="#drawable/center_image" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/imageView1"
android:text="TextView" />
</RelativeLayout>
Check this one, works exactly as you want
<Button
android:id="#+id/btn"
style="#style/MyButton"
android:drawableTop="#drawable/btn_my"
android:text="SomeText" />
btn_my.xml // add this inside drawable folder
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/ic_launcher"
android:state_focused="true"
android:state_pressed="true" />
<item android:drawable="#drawable/ic_launcher"
android:state_focused="false"
android:state_pressed="true" />
<item android:drawable="#drawable/ic_launcher" android:state_focused="true" />
<item android:drawable="#drawable/ic_launcher"
android:state_focused="false"
android:state_pressed="false" />
</selector>
copy the below into styles.xml
<style name="MyButton">
<item name="android:layout_gravity">center_vertical</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:gravity">center_horizontal</item>
<item name="android:drawablePadding">2dp</item>
<item name="android:textSize">16dp</item>
<item name="android:textStyle">bold</item>
<item name="android:textColor">#ff29549f</item>
<item name="android:background">#null</item>
</style>
Here is the OUTPUT
you can refer to this code.
Border color of Edit Text is set to some color.
working fine in portratait mode.
Why i can't see the same color/image in landsacpe mode??In landscape mode when soft key is open then edit text color become red(default)..where can i locate that red border scalable image??
ny idea??
below is my layout file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<EditText
android:id="#+id/edit1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/edit1_text"
android:inputType="text"
android:background="#drawable/edit_text"
/>
<EditText
android:id="#+id/edit2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/edit2_text"
android:inputType="text"
android:background="#drawable/edit_text"
/>
</LinearLayout>
and here is #drawable/edit_text file code
<?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/textfield_default" />
<item
android:state_window_focused="false"
android:state_enabled="false"
android:drawable="#drawable/textfield_disabled" />
<item
android:state_pressed="true"
android:drawable="#drawable/textfield_pressed" />
<item
android:state_enabled="true"
android:state_focused="true"
android:drawable="#drawable/textfield_selected" />
<item
android:state_enabled="true"
android:drawable="#drawable/textfield_default" />
<item
android:state_focused="true"
android:drawable="#drawable/textfield_disabled_selected" />
<item
android:drawable="#drawable/textfield_disabled" />
</selector>
#Mr.Singh - When I executed your code in my device, I am getting edit text border even in landscape mode.Please check....