RatingBar not showing correct amount of stars - android

When I have numStars set to '5' I only see 4 stars and when I have it set to 6 I see 5, but then when I vote for the full 5 in app the toast says "6.0" as the rating which is not what I want. Any help would be great, I'm still new to this.
I've tried changing the java code so that it took 1 integer away from the rating, but then when I tried to give 0 stars the toast said -1. I was however getting 5 stars when I had all 5 selected with this method.
<RatingBar
android:id="#+id/ratingBar"
style="#android:style/Widget.DeviceDefault.RatingBar.Small"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="6"
android:isIndicator="false"
android:padding="#dimen/padding_standard"
android:rating="3.5"
android:stepSize="0.1"/>
ratingBar.setOnRatingBarChangeListener
(new RatingBar.OnRatingBarChangeListener() {
#Override
public void onRatingChanged(RatingBar ratingBar,
float rating, boolean fromUser) {
String myRating = (getString(R.string.my_rating) +
String.valueOf(ratingBar.getRating()));
Toast.makeText(getContext(), myRating,
Toast.LENGTH_SHORT).show();
When I have 5 numStars I get a visual of 4 stars, but the rating is still out of 5. I want the visual to line up with what my toast displays.

In this style is the issue
You defined style :- #android:style/Widget.DeviceDefault.RatingBar.Small
In this style, size is predefined so that only you can't get 6 stars or more in properly. So it starts to adjust in 5 stars.
So you have to change your style or change android:numStars="6" to android:numStars="5"
Then, It will work definitely.
Note:- You can also use this style:- #android:style/Widget.DeviceDefault.RatingBar

The padding in RatingBar is the culprit, set paddingVertical and avoid padding or paddingHorizontal

Related

Adding the dots at the end of Textview (...) , dots not showing

Whenever a text (That I set programmatically from an Activity) is longer than n charachters, I want to cut off and limit the texts length and add 3 dots at the end of the textview. Text is initially set to empty (just "") in xml, and is set from activity later.
This is how I tried:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:maxLength="10"
android:ellipsize="end"
android:maxLines="1"/>
This is how it ends up looking with a random text, and I set the maxlength to 10 charachters:
I was expecting it to look like jrkfkgkglg... because the text is jrkfkgkglggfgirng, but the dots are not added at the end. Any suggestions?
In order for this to work, your width must be match_parent or have a any other size defined instead of being wrap_content.
You are telling your edit text to be maximum 10 chars in length, in your screenshot, you have a string with that length so if you will try to add anything to that string it won't get added.
You can remove the android:maxLength="10" limit and check your text from your code programmatically, something like this:
if (yourText.length() > 10) {
yourText = yourText.substring(0, 9); //get the first 10 chars
textView.setText(yourText + "..."); //display
} else {
textView.setText(original string < 10chars);
}
For this you have to remove the attribute
android:maxLines="1"
and add the new attribute
android:singleLine="true"
and in addition make your textview width match_parent.
It will work properly when you will add this then you don't need to use anything else.

android:textColor no longer works in Marshmallow

I have written an app which relies on colors defined in resources. Some are set directly in the layout XML file, others are set in code. Examples:
Color definition in res/values/styles.xml:
<color name="orvGyro">#33B5E5</color>
Layout:
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/dotSpace"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#color/orvGyro" />
Color in code:
accStatus.setTextColor(getResources().getColor(R.color.somecolor));
The app targets API 17. Up to Lollipop, this has worked flawlessly, displaying the right colors. After migrating to Marshmallow (Cyanogenmod 13), all these colors display as orange. Other colors, which are defined in Java code and not in resources, seem to display correctly.
I've tried changing the target API to 23 and adding styles for API 21+, to no avail.
What's wrong here? Is that a bug in CyanogenMod13, or am I doing something wrong?
EDIT: It seems it's not about getting the color from the resource. Hard-coding the colors as shown below also gives me orange text:
<TextView
android:id="#+id/textView9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/dotSpace"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#669900" />
EDIT 2: Just came across Android M Developer Preview - TextView android:textColor being ignored. Could this explain the behavior I'm experiencing?
EDIT 3: When I generate content dynamically instead of using layouts, colors display correctly. Example:
TextView newType = new TextView(rilLteCells.getContext());
newType.setLayoutParams(new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, 2));
newType.setTextAppearance(rilLteCells.getContext(), android.R.style.TextAppearance_Medium);
newType.setTextColor(rilLteCells.getContext().getResources().getColor(getColorFromGeneration(cell.getGeneration())));
newType.setText(rilLteCells.getContext().getResources().getString(R.string.smallDot));
row.addView(newType);
Use ContextCompat class, It is helper class for accessing features in Context introduced after API level 4 in a backwards compatible fashion.
accStatus.setTextColor(ContextCompat.getColor(context, R.color.somecolor));
public static final int getColor (Context context, int id)
Returns a color associated with a particular resource ID
Got it.
Wherever I had this issue, the text displayed in the control was a single square (U+2b1b). When I alter this text (e.g. by appending an X), only the square will display in orange and the rest of the string has the desired color.
Changing it to a small square (U+25fc) fixed things. Some other special characters would give me other colors – apparently certain characters are fixed to certain colors on Marshmallow, when on earlier versions they could be styled like any other text.
Faced the same problem on my Sony Xperia (Android 6.0 Marshmallow). The reason was that the Settings/Accessibility/High contrast text (experimental) was enabled.
When I disabled it, it worked fine again as expected.

Android RatingBar - quarter star step size

I'm working with a default rating bar and require a stepSize of .25 - according to the docs, this should be a simple matter of setting the xml attribute android:stepSize to 0.25.
Currently, that is in place, with the total xml as:
<RatingBar
android:id="#+id/ratingbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/RatingBarBig"
android:layout_below="#+id/score"
android:layout_centerHorizontal="true"
android:gravity="center_horizontal"
android:paddingBottom="5dp"
android:layout_centerVertical="true"
android:isIndicator="true"
android:stepSize="0.25"
android:numStars="5"
android:rating="0"/>
I'm filling the ratingbar programmatically with a float of 3.25 using the following code:
float rating = 3.25f;
RatingBar mRating = (RatingBar) findViewById(R.id.ratingbar);
mRating.setRating(rating);
so, the problem is that the rating bar itself is being filled to show 3.5 instead of 3.25. I've even set Log.d notices on both the rating float and the output of mRating.getRating
Anybody have any ideas?
Ah - so after 2 days of trying to figure this out, I finally ask SO, and what happens? I figured it out ten minutes later...
The issue was in my drawable.
What I forgot to mention, is that I'm using a second RatingBar, overlayed on top of the first one (with just empty transparent stars), as I'm using a colour filter on the primary one in order to change the colour of the stars based on the rating (red, yellow, green).
What was happening is that the drawables I was using for the primary RatingBar needed to use the exact same ones as the overlay - and not the ones I was using that were left over from my previous theme...
So, it all comes down to the images used, not the code. I'll accept this as the right answer once I'm allowed to.
Try setting the step size programmatically:
mRating.setStepSize((float) 0.25);
call this before mRating.setRating(rating);
See if this works

How to set progressDrawable to a RatingBar dynamically using LayerDrawable

I want to set progressDrawable to a RatingBar. I was able to do this using (layer-list) via xml. Here is my layout ....
<RatingBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:stepSize="0.5"
android:rating="3.0"
android:id="#+id/ratingbar_default"
android:progressDrawable="#drawable/star_rating_bar_full"
/>
Here is my star_rating_bar_full
where star2 and star1 are images for specifying rated star and unrated star.
The output was fine and the RatingBar looked great. I was able to rate also. Now i want to apply the same logic dynamically using LayerDrawable
I have tried to use the following answer Dynamic RatingBar. I was able to display the RatingBar but the edges were cut at the right side of Rating Bar. The functionality was working fine but this is the only problem i am facing.
Any ideas ??? Why the rating bar edges has been cut of at right side.

How to set ratingbar style from programmatically?

I don't see any method like setStyle for class RatingBar. How can I set the rating style from code?
This works for me (source):
RatingBar rating =
new RatingBar(context, null, android.R.attr.ratingBarStyleSmall);
You need to set it in xml file! If you like to change it on the run, you can have two different View-s (rb) and switch them in the program (add/remove from layout) or use rb1.setVisibility(View.VISIBLE) and rb2.setVisibility(View.GONE);
<RatingBar
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:layout_alignParentRight="true" android:id="#+id/rb1"
style="?android:attr/ratingBarStyleSmall"
android:clickable="false" android:numStars="5">
</RatingBar>
If you are programmatically creating the RatingBar as follows the
RatingBar rating = new RatingBar(context, null, android.R.attr.ratingBarStyleSmall);
then don't for get to add
rating.setIsIndicator(false);
else the touch will not work on RatingBar.
The android.R.attr.ratingBarStyleSmall gives you very small stars if you want to have a slightly bigger stars use android.R.attr.ratingBarStyleIndicator

Categories

Resources