Android ratingbar "ratingBarStyleSmall" loads random amount of stars - java

I find myself a little baffled here. I'm fairly new to android/java development so please help out here to identify what I'm doing wrong. I have checked aggressively throughout google, android dev and stack but couldn't find much info on this.
I have a string variable with a number as theRating. e.g. 2.5 (out of 10)
I am trying to display this using ratingbarstylesmall... It's for showing only and not rating.
RatingBar rb = new RatingBar(this, null, android.R.attr.ratingBarStyleSmall);
rb.setIsIndicator(true);
rb.setNumStars(5);
rb.setStepSize((float) 0.5);
rb.setMax(10);
rb.setRating(Integer.parseInt(theRating));
llTextEtc.addView(rb);
The stars load fine inside LienarLayout (llTextEtc), show up in the right place, it's the right style stars that i want (small) but...
it's completely random. Some show 8 stars, some show 15 then back to 7 and so on. Totally random. What am I doing wrong?
Thanks guys
Update:
With help from the accepted answer:
A LinearLayout was added to hold the ratingbar with wrap_content layout. Otherwise the parent (scrollview) became wrap_content.
LinearLayout llRating = new LinearLayout(this);
RatingBar rb = new RatingBar(this, null, android.R.attr.ratingBarStyleSmall);
rb.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
rb.setIsIndicator(true);
rb.setNumStars(5);
rb.setRating(Float.parseFloat(theRating)/2);
llRating.addView(rb);
llTextEtc.addView(llRating);
setStepSize and setMax was also removed, Rating was set to float instead of integer. The stars are calculated and then set. stepSize and Max doesn't help when isIndicator is true.

I noticed a couple of problems here. First and most important you need to set the layout width to WRAP_CONTENT for it to work properly. The following is from the dev guide:
The number of stars set (via setNumStars(int) or in an XML layout) will be shown when the layout width is set to wrap content (if another layout width is set, the results may be unpredictable).
Secondly I noticed that you have set the stepSize to a float and you are seting the rating using an Interger. I would first convert the value from the URL feed to a Float and then validate it before passing it to the rating bar.

Related

Staggered Grid View (Both Directions) - different cells width - vertical scroll [duplicate]

This question already has an answer here:
RecyclerView with StaggeredGridLayoutManager : variable number of columns and vertically scrollable
(1 answer)
Closed 10 months ago.
I need to make panel of some sort with a Staggered Grid behavior such in the photo attached (cells width are not the same in each cell):
I tried normal GridView, I tried StaggeredGridLayoutManager with and without a recycler view,
I've searched the internet for so long, and still hasn't found anything that gave me the suitable solution in java.
I also tried with the following answer: LayoutManager for RecyclerView Grid with different cell width and Android Grid view with different width of column (There are many more answers similar to this 2), but I need it to be "dynamic" and what I mean by that is that I need the cell width to be determine by it's content width and not just by it's index like in the answers attached.
Anyone knows a way to accomplish such a thing? or knows a library or a similar project with that feature?
p.s: if there is anything else you need me to add to this question in order to clarify, comment it please :)
It looks like https://github.com/google/flexbox-layout may help you.
You can use FlexboxLayoutManager as adapter and play with properties, all the views will wrap automatically depending on their width.

scrolle to a table raw in a scrolle view at run time

In my Android project I'm using a table layout contained in a scroll view. When a product is selected from a spinner the table view must automatically scroll to the table raw showing the relevant product's details. Unfortunately I can't post any of my code work. I have a big problem in scrolling the view. So can someone help me with some helpful code.
Thank you.
I think one solution is to know the index in your list of the selected product. Then you'll have to measure the childs in your ScrollView. When you know the sizes of your children and the index of the child you want to scroll to you can use ScrollView.scrollTo(x, y)
the y will be something like this: index * heightOfChild.
This is ofc if your childs are of the same size. Otherwise youll have to measure each child separatly.
Edit:
scrollTo apparently "clamps the scrolling to the bounds of our child." Not sure what it means... But you could try scrollBy(x, y) the documentation doesnt write anything about clamps for that.
if you are using a listView you could try setSelection() as described here:
Android scroll to the bottom of a listview

ActionBar: Center text on navigation bar buttons

Simple aim:
To have default buttons in the navigation bar of my ActionBar with the text centre aligned. I simply want to use the default, simple way of:
ActionBar.Tab myTab = Actionbar.newTab()
myTab.setText("my tab's text");
What's the problem
I cannot find any simple way to do this whatsoever. You can't access the views (as ActionBar.Tab.getCustomView() always returns null due to it being the default view). There is no method that I can see in ActionBar, ActionBar.Tab, etc to get the current view, get or modify LayoutParams (especially Gravity). A visualisation of the problem is below (one line seems to work right, but double lined navigation buttoms definitely seem left aligned):
Unideal solution
Am I right in negatively thinking that the only way to accomplish this is to use custom views for all of my tabs, somehow guess / attempt to copy the default formatting for the tabs (as I can't find anyway to access these views) and assume / hope that the default formatting / font / style of ActionBar.Tab's text does not change any time soon?
Surely there has to be a better way?
AFAIK, there's no way you can access the Views in ActionBar. Using custom layout seems the only solution. However, doesn't Android align the text to center automatically? It apparently does so in all my applications.
please download the follwing example. it will help you. i am sure i take help from this demo.
https://github.com/JakeWharton/ActionBarSherlock

How to set textview position constant in runtime?

I am trying to display some textviews visible in runtime.
Here's my code:
for (int i=0; i<arrBool.length; i++) {
arrBool[i] = r.nextBoolean();
if(arrBool[i]==true) {
textView[i].setVisibility(View.VISIBLE);
}
}
When I run the application, textviews should randomly be visible. It is working, but my problem is I have set the layout of those textviews. When I run Android Application, the visible textviews go to top left corner and loses the layout position.
How to deal with this?
Change start visibility parameter of views to View.INVISIBLE
It will hold their own places on the layout and prevent from taking this places by other views, which is normal behavior in case of View.GONE
Adding more to teoREtik solution.
In your layout do not specify the android:visible property.
for (int i=0; i<arrBool.length; i++) {
arrBool[i] = r.nextBoolean();
if(arrBool[i]==true) {
textView[i].setVisibility(View.VISIBLE);
else
textView[i].setVisibility(View.INVISIBLE);
}
}
I don't think using the FOR loop will help you out making the random possibility of the visible text view everytime you open. Why don't you just use the integer (0 for false and 1 for true) and a Random like this:
Random rnd = new Random(1); // --> This will randomize numbers up to one;
int enable = rnd.nextInt(); // --> Get the random value from 0 to 1
if(enable == 1) // --> If visibility of the text field is enabled everytime you opened the app...
{
textView.setVisibility(View.VISIBLE);
} else {
textView[i].setVisibility(View.INVISIBLE);
}
You can modify more and experiment on the randomizer for the integer's value by visiting this example at " How can I generate random number in specific range in Android? " topic. Check all the possible answers. Don't mind the green check mark and focus these answered codes by investigate its comments. I'm sure all of these answers about "random" topic, the link I gave you, is guaranteed effective.
About the layout, I recommend not to use the relative layout and instead use the linear layout because using the linear layout stays on the original place proportionally since relative layout is screen resolution dependent on the coordinates. Also try practicing manipulate the string value dimensions under res folder to maintain the size of the text proportionally in different screen resolution (from HVGA to WVGA). Check at " Different font sizes for different screen sizes " for more details about text size proportion.

Why does adding this empty padding View mess up my layout?

So I'm using layout weights to scale parts of my layout to a certain fraction of the screen, and when I try to add an empty view for padding between elements, it causes a 0-size error on the views.
The original layout, which works before adding anything is:
http://pastiebin.com/?page=p&id=4e9777a2639f4 (Sorry for the pastebins instead of pasting it here, it wouldn't format correctly because of the xml tags)
And adding this between, or before or after any of the DWIButtons cause the error:
http://pastiebin.com/?page=p&id=4e9777caefc2d
I have no idea what to do here ):
EDIT 1:
Here it is without the padding view added
EDIT 2:
Also, adding any other type of view doesn't mess it up. I added a TextView instead of a blank View, and it resized correctly
adding images might help us understand the situation. but i suspect the layout weight is what is messing up your layout. setting layout weight to 1 means you want the blank layout to fill the available space(most of the time the entire screen).

Categories