I've made a ratingbar in my app which uses a custom style, using my own stars.
My ratingbar is defined as followed:
<RatingBar
android:layout_width="wrap_content"
android:layout_height="15dp"
android:id="#+id/ratingBar"
android:numStars="10"
android:stepSize="1"
android:rating="10"
android:layout_marginTop="5dp"
android:layout_below="#id/feedbackTable"
android:layout_toRightOf="#id/ratingText"
android:layout_marginLeft="5dp"
style="#style/starBar" />
The style that I use in styles.xml is:
<style name="starBar" parent="#android:style/Widget.RatingBar">
<item name="android:progressDrawable">#drawable/ratingstars</item>
<item name="android:minHeight">22dip</item>
<item name="android:maxHeight">22dip</item>
</style>
With ratingstars.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/ratingstars_full_empty" />
<item android:id="#+id/secondaryProgress" android:drawable="#drawable/ratingstars_full_empty" />
<item android:id="#+id/progress" android:drawable="#drawable/ratingstars_full_filled" />
</layer-list>
When I adjust the android:rating it shows the correct amount of stars filled and being empty in the preview. However, using my emulator or real device the bar stays completely filled with stars (10/10). I've tried adjusting it programmatically by using the following piece of code:
ratingbar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
Log.d("rating", rating+"");
ratingbar.setRating(rating);
}
});
Now the log from logcat shows actually that rating IS changed, but the bar does not visually update itself with e.g. 6 stars being full, and 4 empty and I have no clue why.
The drawables are correct as the preview also shows it correct, so what can cause this?
Edit:
This is ratingstars_full_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" />
<item android:state_focused="true"
android:state_window_focused="true"
android:drawable="#drawable/star" />
<item android:state_selected="true"
android:state_window_focused="true"
android:drawable="#drawable/star" />
<item android:drawable="#drawable/star" />
</selector>
And here is ratingstars_full_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_empty" />
<item android:state_focused="true"
android:state_window_focused="true"
android:drawable="#drawable/star_empty" />
<item android:state_selected="true"
android:state_window_focused="true"
android:drawable="#drawable/star_empty" />
<item android:drawable="#drawable/star_empty" />
</selector>
You should change the id attribute in ratingstars.xml from "#+id/..." to "#android:id/...", since the resource is part of the android system, and already defined in the default android progress drawable.
<?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/ratingstars_full_empty" />
<item android:id="#android:id/secondaryProgress" android:drawable="#drawable/ratingstars_full_empty" />
<item android:id="#android:id/progress" android:drawable="#drawable/ratingstars_full_filled" />
</layer-list>
It looks like it's not a problem with the listener being called, but rather just the custom android:progressDrawable style attribute you're applying to it. My guess is that the drawable you're using is doing unexpected things. Try removing style="#style/starBar" from your RatingBar and see if things are working as expected. If so, that's your problem
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 create a custom rating bar. I set 5 custom star images, but it shows only one. Instead of a variable number of rating drawables, I see only one, regardless of the number of stars set. Any help will be appreciated.
xml.code :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical">
<RatingBar
android:id="#+id/ratingbar_default"
style="#style/RatingBar"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_marginBottom="20dp" />
<TextView
android:id="#+id/textView"
android:layout_width="276dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="show state up checkbox"
android:textColor="#CC00CC"
android:textSize="20dp" />
java code :
Activity RatingBarActivity
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rating);
final TextView text = (TextView) findViewById(R.id.textView);
final RatingBar ratingBar_default = (RatingBar) findViewById(R.id.ratingbar_default);
ratingBar_default.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener()
{
#Override
public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser)
{
// TODO Auto-generated method stub
text.setText("Rating: " + String.valueOf(rating));
}
});
<?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>
res/drawable/star_ratingbar_full_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/star2" />
<item android:state_focused="true"
android:state_window_focused="true"
android:drawable="#drawable/star2" />
<item android:state_selected="true"
android:state_window_focused="true"
android:drawable="#drawable/star2" />
<item android:drawable="#drawable/star2" />
</selector>
res/drawabstar_ratingbar_full_filled.xml:
<?xml version="1.0" encoding="utf-8"?>
<!-- This is the rating bar drawable that is used to
show a unfilled cookie. -->
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:state_window_focused="true"
android:drawable="#drawable/star3" />
<item android:state_focused="true"
android:state_window_focused="true"
android:drawable="#drawable/star1" />
<item android:state_selected="true"
android:state_window_focused="true"
android:drawable="#drawable/star1" />
<item android:drawable="#drawable/star1" />
</selector>
styles.xml file :
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"></style>
<style name="RatingBar" parent="#android:style/Widget.RatingBar">
<item name="android:progressDrawable">#drawable/star_rating_bar_full</item>
<item name="android:minHeight">48dip</item>
<item name="android:maxHeight">48dip</item>
</style>
</resources>
In my case trouble was in SVG drawables in layer-list. When I changed them to PNG it starts work correctly
Try to specify an android:numStars attribute for your RatingBar in layout file activity_rating.xml. I guess, the default value is 1 - that's why you see only one star
Just remove any-dpi drawable of rating bar filled and empty image from drawable. And it will work for you. Because it to show custom rating bar you can give both xml and png files in your layer list. But to show all the stars, you have to put only png icons into drawable folder.
Has been a while but never hurts to answer.
Here is my 3 step solution regardless of SDK version.
1- Add this as your RatingBar in your xml layout:
<RatingBar
android:id="#+id/new_ratingbar"
android:progressDrawable="#drawable/custom_drawable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
android:stepSize="0.5"/>
If needed, you can give the RatingBar minHeight and maxHeight or add other attributes as required.
2- Here is the drawable you would refer the above android:progressDrawable to:
custom_drawable.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/rating_bar_empty" /> <!--this is your .png file-->
<item
android:id="#android:id/secondaryProgress"
android:drawable="#drawable/rating_bar_fill" /> <!--this is your .png file-->
<item
android:id="#android:id/progress"
android:drawable="#drawable/rating_bar_fill" /> <!--this is your .png file-->
</layer-list>
3- The last thing is to provide the .png files in your various drawable folders.
I'm developping an Android app, which use a custom RatingBar. I've follow a tutorial (http://kozyr.zydako.net/2010/05/23/pretty-ratingbar/), but when I use my custom style, it always shows me 5 stars, even I force the RatingBar to a value (Ex 3 stars). I verified on the web with some others tutorials, but it doesn't change anything..
My files :
custom_ratingbar_full.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/custom_ratingbar_full_empty" />
<item android:id="#+id/secondaryProgress"
android:drawable="#drawable/custom_ratingbar_full_empty" />
<item android:id="#+id/progress"
android:drawable="#drawable/custom_ratingbar_full_filled" />
</layer-list>
custom_ratingbar_full_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_off" />
<item android:state_focused="true"
android:state_window_focused="true"
android:drawable="#drawable/star_off" />
<item android:state_selected="true"
android:state_window_focused="true"
android:drawable="#drawable/star_off" />
<item android:drawable="#drawable/star_off" />
</selector>
custom_ratingbar_full_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_full" />
<item android:state_focused="true"
android:state_window_focused="true"
android:drawable="#drawable/star_full" />
<item android:state_selected="true"
android:state_window_focused="true"
android:drawable="#drawable/star_full" />
<item android:drawable="#drawable/star_full" />
</selector>
styles.xml
<style name="CustomRatingBar" parent="#android:style/Widget.RatingBar">
<item name="android:progressDrawable">#drawable/custom_ratingbar_full</item>
<item name="android:minHeight">48dip</item>
<item name="android:maxHeight">48dip</item>
</style>
my_fragment.xml
...
<RatingBar
android:id="#+id/rating_bar"
style="#style/CustomRatingBar"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:isIndicator="true"
android:layout_marginBottom="10dp"
android:visibility="invisible" />
...
Any explanations ? I've search what I've done wrong, but I don't find it..
EDIT :
How I set my rating value :
I send a bundle between Fragments
myRating = bundle.getInt("rating");
I retrieve the RatingBar :
RatingBar ratingBar = (RatingBar) rootView.findViewById(R.id.rating_beer);
And finnaly I test the value to show or not the rating bar :
if (beerRating != 0) {
ratingBar.setRating(myRating);
ratingBar.setVisibility(View.VISIBLE);
Toast.makeText(getActivity(), "Rating : " + myRating, Toast.LENGTH_LONG).show();
}
I have verified my value, and it doesn't show me the correct number of stars. But if I remove my custom style from the rating bar, it works.
EDIT 2 SOLUTION :
Finally I found the solution : I have replaced tags #+android:id by #+id, and this is why it didn't work.
I was having the same problem. I downloaded the code and the items in the custom rating bar file were like this
<item
android:id="#+android:id/background"
android:drawable="#drawable/star_blank"/>
to remove the error I changed the id to
<item
android:id="#+id/background"
android:drawable="#drawable/star_blank"/>
which removed the error but it was only showing the selected items stars but the rating bar value was changing.
So I changed id in custom rating bar to
<item
android:id="#android:id/background"
android:drawable="#drawable/star_blank"/>
which worked perfectly.
Try this , Set the progress drawable as a property of rating bar,
<RatingBar
android:numStars="5"
android:progressDrawable="#drawable/ratingbar_selector"
android:stepSize="0.2" />
I had been trying to disable the highlight in a ExpandableListView. I tried setting the next drawable as background, but this didn't work.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/_GREY_Weak"/>
<item android:drawable="#color/_GREY_Weak"
android:state_pressed="true" />
<item android:drawable="#color/_GREY_Weak"
android:state_selected="true" />
<item android:drawable="#color/_GREY_Weak"
android:state_focused="true" />
<item android:drawable="#color/_GREY_Weak"
android:state_checked="true" />
<item android:drawable="#color/_GREY_Weak"
android:state_long_pressable="true" />
<item android:drawable="#color/_GREY_Weak"
android:state_hovered="true" />
<item android:drawable="#color/_GREY_Weak"
android:state_expanded="true" />
</selector>
I tried setting each state drawable as that color, #null or transparent, but still the same. Here are some screenshots of my issue. I want to get rid of that highlight in the childrens as in the parent.
I also triead setting the property drawingCacheHint in the ExpandableListView xml with no luck.
Try adding
android:listSelector="#android:color/transparent"
in ExpandableListView xml
Or override the ExpandableListView style.
I have a problem with a TableRow, which I add dynamically.
private void addRow(String body) {
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
TableRow row = (TableRow) inflater.inflate(R.layout.customrow,null);
TextView name = (TextView) row.findViewById(R.id.customName);
name.setText(body);
row.setOnLongClickListener(this);
}
I would like this row to change color upon onClick and onLongClick.
Code in the customrow.xml file is:
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tableRow1"
android:layout_gravity="center"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:gravity="center_vertical"
android:onClick="showOnClick">
<TextView android:id="#+id/customName"
android:textSize="25px"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:layout_weight="5">
</TextView>
</TableRow>
I have tried to use android:background="#drawable/clickedbackground"with the row but it is not working.
Code in the clickedbackground.xml file is:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:state_focused="false"
android:state_pressed="false" android:drawable="#android:color/transparent" />
<item android:state_selected="false" android:state_focused="false"
android:state_pressed="false" android:drawable="#android:color/transparent" />
<item android:state_pressed="true" android:drawable="#color/custom" />
<item android:state_focused="true" android:state_selected="true"
android:state_pressed="false" android:drawable="#color/custom" />
</selector>
Anyone knows what I am doing wrong (color/custom is defined in another xml which works)?
Thank you
You are creating object for tablerow named row. and you have also clickedbackground.xml file. just use below code in addRow method.
row.setBackgroundResource(R.drawable.clickedbackground);
I think it solves your problem.
In your addRow() method you're inflating the row but you're not adding it to any parent layout, and as row is a local variable I think you're not doing it anywhere else, is it a copy/paste problem?
Again, your customrow.xml might be not working because the opening TableRow tag lacks the closing >, but it might be copy/paste problem.
Using android:background="#drawable/bg" with bg being a selector is a common pattern and it should work. You might want to simplify your selector: you don't need to specify all the states for each item and all the combinations. It works with a "first match", so this will do the job:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#color/custom" />
<item android:state_selected="true" android:drawable="#android:color/transparent" />
<item android:state_focused="true" android:drawable="#color/custom" />
<item android:drawable="#android:color/transparent" />
</selector>
Also, notice that selected and focused are two different states, focused being the one you get when moving around with dpad.
If this didn't help please specify what "is not working" means: what do you expect? what's happening instead?
Adding
<resources>
<style name="row" parent="#android:style/Widget.Button">
<item name="android:background">#drawable/rows</item>
</style>
</resources>
in styles.xml and setting
style="#style/row"
in the TableRow did the job.
where the rows.xml in the drawable is
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_enabled="false"
android:drawable="#color/blue" />
<item
android:state_pressed="true"
android:state_enabled="true"
android:drawable="#color/custom" />
<item
android:state_focused="true"
android:state_enabled="true"
android:drawable="#color/white" />
<item
android:state_enabled="true"
android:drawable="#android:color/transparent" />
</selector>
do not forget to add to the style
<item name="android:focusable">true</item>
<item name="android:clickable">true</item>
Otherwise, you will not be able to use the states of a rowLayout.