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
Related
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
I am developing an Android app. There's this issue with the EditTexts, because in some devices the "background" is white (which is the way I want it), but on others is transparent, and makes the app less professional. Here's an image with the "white background":
On other devices the edit texts that say "Yo" and "Casa" are transparent, so the words mix with the map and its pretty awful. I have tried changing the background color in the android layout xml. The closest one was I believe #drawable/white, but slightly changes the position and size of the edit text, making them look like they're one and its even worst.
Here is an example:
Is there a nice way to approach this problem? I read somewhere that an option was to add a white background image to the edit texts, but seemed like a lot of trouble.
UPDATE
I tried adding a background image, but its the same as changing the background color to any color using the android:background, the edit texts get like smaller or something. I added a blue "delimiter" to the image and this is the result:
But I want them like in the first picture, not so close one to another.
This would be the code of the edittext layout XML, the other one looks very similar. Only by adding the android:background tag changes from picture 1, to picture 3
SOLUTION
To solve this what I did was set a background image and set its height in dp as follows:
<EditText
android:id="#+id/markerTitle"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_alignParentLeft="true"
android:layout_below="#+id/buttonModifymarker"
android:ems="10"
android:inputType="textPersonName"
android:text="TÃtulo"
android:background="#drawable/editwhite"
>
Why don't you try setting the background of EditText programmatically?
yourEditText.setBackgroundColor(Color.WHITE);
or...
yourEditText.setBackgroundColor(Color.parseColor("#ffffff"));
simply you can add android:background="#drawable/white" attribute to your element in layout xml. you can use a lot of color resource between the double quotes (like from android color resources).
put the following image which has a solid white background in the drawable folder (it may dont appear because it's white and this page's background is white also) right click under this paragraph and select "Save Image as".
and refer to it in your layout and you will get a result like the following image
if the issue continue after trying this, please show me your layout code or email me at : mohamed_hamed4158#yahoo.com , thanks for voting my answer :)
I want to create metro UI, so I did like in the picture But the problem is the position of the icons, they are in the top of the button, and I don't think it's aesthetic interface. I inserted the icons in the drawable Top in the button. Well how can make the a little bit in the middle.
You have to set the android:paddingTop and android:paddingBottom eqaully. Just don't copy and paste 10dp as the value of the padding. Value depends on the size of your button. Refer to below:
<Button
android:id="#+id/find_teacher"
style="#style/page_button"
android:drawableTop="#drawable/ic_search_light"
android:text="#string/find_teacher"
android:paddingTop="10dp"
android:paddingBottom="10dp"/>
This is the output:
I can remember 2 options:
top padding
gravity
You might wan't to check the layout documentation
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.
I want to customize Buttons for my application. The application has a color picker where the user will select color and I have to set that particular start/end color to the buttons. These colro values will be stored in an object "Utility".
Basically from start only, I want to use "Utility" object to set colors for background, text color, font, etc. And again when the color is changed by the user I got to change it to the buttons and refresh them. And also to save colors in a file, so next time user starts app, it comes up with the last color selected.
I couldn't find <selector> to be the best option, as I wont be able to change the color in xml. What can be the best option for such requirement ?
UPDATIONS :
#jitendra, from your answer I got somethign helpful. I use GradientDrawable to set colors of my buttons. In my onCreate() of the Activity, I call a method RefreshComponents() that sets the background of root, text color/size of buttons and gradient colors of the buttons. It works properly, but the only problem I see is the on applying GradientDrawable to the button the gap between 2 buttons is lost.
This is the image WITHOUT applying GradientDrawable :
On applying GradientDrawable the output is :
You see the size of button is increased a bit from all the sides. If I apply to next button also, they both touch eachother. My xml for the above is :
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mainroot" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:orientation="vertical"
android:paddingTop="35dip" android:paddingBottom="35dip"
android:paddingLeft="35dip" android:paddingRight="35dip"android:gravity="center" >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mainrow1" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:orientation="horizontal"
android:layout_marginBottom="15dip" android:gravity="center_horizontal" >
<Button android:text="Accounting" android:id="#+id/accBtn" android:layout_width="80dip" style="#style/TileButtonStyle" />
<Button android:text="Data" android:id="#+id/dataBtn" android:layout_width="80dip" android:layout_height="fill_parent"></Button>
<Button android:text="Information" android:id="#+id/infoBtn" android:layout_width="80dip" android:layout_height="fill_parent" android:ellipsize="end"></Button>
</LinearLayout>
..... Other lineasr layout with same parameters as above child
And the GradientDrawable that I create is :
public static GradientDrawable getButtonDrawable(Button btn) {
int colors[] = {getStartColor(), getEndColor()};
GradientDrawable grad = new GradientDrawable(GradientDrawable.Orientation.LEFT_RIGHT, colors);
grad.setCornerRadius(5f);
return grad;
}
And finally in my onCreate(), I add :
GradientDrawable btnGradient = Utility.getButtonDrawable(btn1);
btn1.setBackgroundDrawable(btnGradient);
What is going wrong here ? Is the margin around the button becoming 0 ? Do I have to set bounds for the grad, or again set LayoutParams for the button ?
Any help is appreciative to help me achieve my goal.
Thanks
You can Create StateListDrawable Object dynamically in java file and set as background and sources of applcation components.
Android has Themes and Styles, but they are a development-time feature and can't be manipulated at runtime. However, different predefined themes can be applied at runtime.
So you can have a set of predefined themes, with fixed view properties (colors, fonts, etc..) and give user an option to choose a theme at runtime.
But, if you need to change every particular view property, then you will need to roll your own "theme" system. Which means you will need to have properties stored somewhere and applied each time a view is built.
Tvd! I incline to agree with Peter Knego and Jitender Sharma. Furthermore, I think/believe you can setOnClickListener on those buttons of yours and perform your color changing stuff with the code that rests inside the setOnClickListenermethod assigned to every button. In addition to this, you'll have to configure your color.xml file and custom themes. There's a lot of work to be done. I'm not really sure which is the best way though. I strongly suggest you to go through the android learning stuff I'd provided to you in my previous answer. Only they can give you a detailed insight and a solid idea to go ahead. All the best!
Oh I got the solution :
I added layout_marginRight attribute to buttons and that did the work.
Though I am still concerned, without GradientDrawable the buttons had margin betweenthem then after applying GradientDrawable why is the default margin lost ? Why is the need of additional layout_marginRight to be added ?
If anyone yet has answer for this, please let me know.
Thanks