2

I'm wondering would someone be able to help me please. I'm having a scaling issue when working with a custom RatingBar when displaying on different screen densities. I've set up a quick sample project to explain my issue. This is just a very basic project that is just concerned with large screen sizes for the moment, regardless of densities Project Folder Structure.

As you can see from the image, I have selectors to be used for the RatingBar and then I have a default and full image for two seperate RatingBars in each of the large density drawable folders (all images the same size for the moment). In order to show this custom RatingBar. I have tried to get these RatingBars to display correctly and have tried two seperate ways, as outlined below. The first way I have tried to specify the width and height of the RatingBars in the layout file, as shown below:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <RatingBar 
        android:id="@+id/rb_test_1"           
        android:numStars="1"
        android:rating="0.5"
        android:stepSize="0.01"
        android:isIndicator="true"
        android:layout_width="150dp"
        android:layout_height="111dp"
        android:layout_gravity="bottom"
        android:gravity="bottom"                                                          
        android:progressDrawable="@drawable/selector_rb_test_1" />

    <RatingBar 
        android:id="@+id/rb_test_2"           
        android:numStars="1"
        android:rating="0.5"
        android:stepSize="0.01"
        android:isIndicator="true"
        android:layout_width="200dp"
        android:layout_height="154dp"
        android:layout_gravity="bottom"
        android:gravity="bottom"                                                          
        android:progressDrawable="@drawable/selector_rb_test_2" />

</LinearLayout>

When looking at the GUI Graphical Layout, this produces the following output displayed on a basic 10.1 screen size vs Nexus 10. It seems to be duplicating and stretcching the image.

TestScaling-10Inch Screens-SpecifyingSize

Now when I try the second way, which is more like the way I would like to do it (by not having to specify the exact size of each of the RatingBars, by just setting the layout width and height to wrap, the following is the code:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <RatingBar 
        android:id="@+id/rb_test_1"           
        android:numStars="1"
        android:rating="0.5"
        android:stepSize="0.01"
        android:isIndicator="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:gravity="bottom"                                                          
        android:progressDrawable="@drawable/selector_rb_test_1" />

    <RatingBar 
        android:id="@+id/rb_test_2"           
        android:numStars="1"
        android:rating="0.5"
        android:stepSize="0.01"
        android:isIndicator="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:gravity="bottom"                                                          
        android:progressDrawable="@drawable/selector_rb_test_2" />

</LinearLayout>

When looking at the GUI Graphical Layout, this produces the following output displayed on a basic 10.1 screen size vs Nexus 10. This cuts off the bottom of the image for the RatingBar.

TestScaling-10Inch Screens-WrappingContent

All I'm looking to be able to do is display the multiple RatingBars without a problem regardless of the density of the screen. Can anyone help me figure out why it's displaying differently on the Nexus10 screen and how to fix it? I have been stuck on this for a while now and would be very greatful if anyone would be able to shed some light. Thank you very much in advance.

Graham Baitson
  • 580
  • 1
  • 11
  • 29
  • The first thing that caught my eye is that you set minWidth to 150dp: `android:minWidth="150dp"` but maxWidth to only 111dp: `android:maxWidth="111dp"`. Same thing with the height. Why don't you just set the width and height to a fixed size? Like this: `android:layout_width="150dp" android:layout_height="111dp"`. – Xaver Kapeller May 02 '14 at 21:56
  • Thanks so much for the comment. I'll check this first thing in the morning and let you know and also swap for setting the layout width and height as you suggested. I hope that the above min and max was just a typo and I have specified the min and max height to be 111 and min and max width to 150. I'll check first thing. Thanks very much for taking the time to comment. – Graham Baitson May 02 '14 at 22:46
  • Hi @XaverKapeller, I've updated the code and still no success, explained in the post below. Have you any other ideas? – Graham Baitson May 03 '14 at 10:58
  • Ok then I think the problem most likely is in your resources. Are you sure you scaled the image properly for all pixel densities? – Xaver Kapeller May 03 '14 at 11:03
  • Sorry @XaverKapeller I've edited the question above and remove the answer post below. When you say scaled the image for all pixel densities. I've only one folder in my application that I was to display for all large screen sizes. How would I go about ensuring this is displayed within the bounds of the width and height if I have different sized images? When working with simple images it's pretty easy because all you have to do is set the scaletype property on the image and it will scale depending on the screen density, but you cant do that with the RatingBar, you have to specify exact w and h? – Graham Baitson May 03 '14 at 11:14
  • 1
    I mean in your resources, you should have 4 different versions of your image in 4 different folders. Each of those versions has a different resolution and android will pick the right image for pixel density of your device. If the resolution of one of those 4 images is not correct than the image will have a different size on certain devices. – Xaver Kapeller May 03 '14 at 11:19
  • Sorry for all the questions, I'm slightly new to the different screen sizes and densities. So what I have is a rating bar that has 7 different images, all different widths and heights. What I originally wanted to do is not specify a size and just wrap the content width and height. But when I do this, the larger in height images get cut in half. So that's when I start using the specified width and height. So with the 4 different folders above, do I have to specify a different size image for each density for large devices (e.g. drawable-xlarge-xhdpi, drawable-xlarge-mdpi, etc...)? – Graham Baitson May 03 '14 at 11:38
  • 1
    The folders you need are `drawable-mdpi`, `drawable-hdpi`, `drawable-xhdpi` and `drawable-xxhdpi`. You can find more information [here](http://developer.android.com/guide/practices/screens_support.html). – Xaver Kapeller May 03 '14 at 11:42
  • What do you mean you have 7 different images and what do you mean that they are all different sizes? – Xaver Kapeller May 03 '14 at 11:48
  • Apologies, I may not have been clear. I meant I am using multiple RatingBars each specifying a different image. I have updated my question to display two RatingBars each with a different image. I have created the 4 drawable folders that you specified above and put the images (all of the same size) so it should pull the images depending on the density (but for the moment they will all be the same size). I still seem to be getting the same problem. – Graham Baitson May 03 '14 at 12:25
  • You can't just put them inside those folders if they are all the same size, this means that on screens with lower pixel densities the image will appear bigger. They have to be scaled properly to be the same size on all devices. It is not that difficult if you already have images you can use the [Android Asset Studio](http://android-ui-utils.googlecode.com/hg/asset-studio/dist/index.html?utm_source=twitterfeed&utm_medium=twitter) or the image resizer that comes with the Android Sdk to resize them to the correct size. – Xaver Kapeller May 03 '14 at 12:35
  • Ok I will try that soon and let you know how it goes. Just at a quick glance, I don't think I'm going to get exactly as I'm looking for because I want each of the RatingBar directly beside each other and all RatingBars baselined (all sitting on the same bottom point). I have a fear that the multiple RatingBars will look too small on some of the larger screen sizes. I will try soon and let you know anyways. Thanks so much for your help, I really appreciate it. Will get back to you soon – Graham Baitson May 03 '14 at 12:56
  • 1
    Just use the [Android Asset Studio](http://android-ui-utils.googlecode.com/hg/asset-studio/dist/index.html?utm_source=twitterfeed&utm_medium=twitter) or the image resizer to scale your images and then check if your problem is fixed, the resulting images might be too small, but if that fixes your issues than we know that this is the problem. After that I can help you to get the images to the correct size. – Xaver Kapeller May 03 '14 at 13:11
  • Hi @XaverKapeller, well I've put the images through as an Icon Set, so it has automatically scaled the images in put them into the 4 folders. So this looks fine now for the 10 inch screens but on the Nexus 7 screen, the bottom half is cut off. I also downloaded a tool (i.e.https://github.com/asystat/Final-Android-Resizer) which was recommended in another thread for scaling images. When I use this to scale the images, all screen sizes show the image half cut. This is a nightmare to work with, it's so difficult. I'm really stuck here to get any group of images working for multiple RatingBars :-/ – Graham Baitson May 03 '14 at 16:54
  • Hi @XaverKapeller, Ok so I've finally understood the concept of creating different sized images for each of the different sized screen densities. I have scaled my ratingbar images for each of the densities (i.e. ldpi 0.75, mdpi 1 -> xxxdpi 4). So this works fine for the 10inch screens now. But the problem I'm having is now because the hdpi smaller screens is displaying to rating bar too large, how would I go about ensuring it looks well on smaller screens with higher densities? I'm struggling here, so If you would like to give me a time estimate, we can work something out by sending code, etc? – Graham Baitson May 11 '14 at 16:57
  • What do you exactly mean? If you use dp and all images are properly scaled then the view should be the same height on all displays. – Xaver Kapeller May 11 '14 at 16:59
  • Yeah i'm using dp and I'm nearly sure the images are scaled correctly to the scale ratios. But say on a 10inch mdpi I have my rating bars looking perfect. But say on a large mdpi screen (e.g. 5.1inch) it will pull the same images as the 10 inch mdp, therefore the width of the screen is too small to display all the images. And I'm missing the last image. Then as you go smaller screen, I'm missing more and more of the entire 7 rating bars? Am I making sense here or am I still not fully getting it?? – Graham Baitson May 12 '14 at 13:19
  • Hi @XaverKapeller, thanks for helping me fix this problem. I have however fixed this issue, but am still not getting the desired effect. I'm going to close off this question (because you have helped me solve this issue), but I'm going to open up a new question with the new issue I'm having - displayed here: http://stackoverflow.com/q/23664132/1634369. Thanks for all your help – Graham Baitson May 14 '14 at 20:07
  • Hi @XaverKapeller, I'm going to start a bounty on the other question (link above) if you'd like to try and solve the issue for me? I've also provided a link to the code on Github if you have time to download. Thanks – Graham Baitson May 16 '14 at 13:03

1 Answers1

0

Following @Xavers comments above, this problem was due to the scaling of the images not being correct. The solution to this is to follow the scale ratios, as presented in the following answer - https://stackoverflow.com/a/11581786/1634369

ldpi | mdpi | tvdpi | hdpi | xhdpi | xxhdpi | xxxhdpi
0.75 | 1    | 1.33  | 1.5  | 2     | 3      | 4

Thanks for your help @Xaver

Community
  • 1
  • 1
Graham Baitson
  • 580
  • 1
  • 11
  • 29