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
Related
The android:drawablePadding attribute exists in the TextView class. Helps to fill space between text and drawing in TextView. But is there a way to create upper and lower padding without custom views?
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="4dp"
android:gravity="center_vertical"
android:textSize="16sp"
andtoid:drawableEnd="#drawable/arrow"
tools:text="Text" />
No, you can only set padding on all sides for drawable in TextView.
The android:drawablePadding attribute will give you the padding between your drawable and your text, if your drawable is on top or bottom, then you can change the upper or lower padding respectively, otherwise it is not possible to set it because there is no method or attribute related, as you can see in the TextView's documentation.
If you want a workaround you can change your drawable setting blank spaces or changing it's size, but the best solution would be making a custom view or using a third party library.
Depending on the case, you can also archive the padding you want setting the padding of the TextView itself and working with the size of the text and the drawable.
Add this attribute in TextView android:drawablePadding="10dp" to set all direction equal padding. drawablePadding The padding between the drawables and the text.
Thanks everyone for answers. I achieved the result with TextView + ImageView, because there is no ways to create upper and lower padding of endDrawable in TextView.
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
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.
Through layout.xml it is working.
<RatingBar
android:id="#+id/hotel_rating_bar"
style="#style/custom_ratingbar" />
But i want to set the style Dynamically.
Any Answer will be highly appreciated...
Dynamically changing the style of views at runtime is not yet supported in Android, except for TextViews via setTextAppearance.
You have to set the style before the view gets drawn,
either via XML or via Java in the constructor.
RatingBar ratingBar = new RatingBar(context, null, android.R.attr.ratingBarStyleSmall);
Text in the textView seems like it is grayed out.
I want so set the text color as when you click textView then It seems in black color as it is enabled.
For changing the text color of TextView you need to set its textColor property.
In XML file you can do like :
<TextView
android:text="Some text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffff00"
android:background="#ff0000"
/>
Programatically you can set TextColor and its Background in TextView using :
text_view.setBackgroundColor(Color.RED);
text_view.setTextColor(Color.BLUE);
Hope this helps. !
try this code
text.setBackgroundColor(56565);
text.setEnabled(true);
it will helpful for u
you can change just alpha value of the textview like as following
from https://gist.github.com/Skamp/1330169
final ColorStateList colors = textview.getTextColors();
...
textview.setTextColor(colors.withAlpha(255));