4

Consider this layout (pulled from here):

enter image description here

I'd like to understand the principles behind making this layout scale with screen size. For the square, a custom onMeasure function works nicely:

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, widthMeasureSpec);
}

The width and height of the custom Togglebuttons and Imagebuttons below should scale to fill the remainder of the screen, minus layout_margins, and the text and images within should scale to fill the buttons, minus padding. The outer margin should also scale.

My first thought was to use a relative layout to position the buttons, and layout_margin/padding attributes to create margins. However, relative layouts and layout_margin/padding require fixed pixel values, so they aren't scalable.

I then thought of using nested linear layouts with layout_weights to position the buttons, and placeholder views to create margins. Although these techniques are scalable, they don't work with buttons, because buttons have many attributes (text size, image size, corner radius, etc.) that require fixed pixel values. This limitation means, for example, that the following xml:

<ToggleButton 
    android:layout_weight="1"
    style="@style/myButton"
    [...] />
<View 
    android:layout_weight="1"/>
<ImageButton 
    android:layout_weight="1"
    style="@style/myButton"
    [...] />

won't necessarily make the two buttons, and the space between them, all the same width. It all depends on the text size, image size, etc. etc. of the buttons.

I've taken a look at this question but I feel there should be a simpler solution for such a simple problem, that shouldn't require resorting to too much non-XML.

Community
  • 1
  • 1
1''
  • 26,823
  • 32
  • 143
  • 200

3 Answers3

7

If you build your views using "dp" it would, basically, be the same size for eack screen size.
In most cases you will prefer that your view will resize itself proportional to the screen size.
Of course, in most cases you will need to build separate layouts for tablets.
But, besides I can recommend you to do the next steps:

1. Add this library to your project.

2. Now in your layout you can write views like that:

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="I'm scalable!" 
    android:textSize="@dimen/_12sdp"/>

In this example your TextView will scale on each screen size.

3. Preview all screen sizes to see the result.

Elhanan Mishraky
  • 2,736
  • 24
  • 26
4

It depends a lot on the number of custom View and ViewGroup classes you want to create. An implementation with the least number of custom classes that I could think of would be something like this (very similar to what you've described):

  • Customized FrameLayout for the largest square, with a custom onMeasure() implementation to match the height to the available width (you mentioned this one already).
  • Nested LinearLayout instances using weight to get all the grid buttons to be the same size.

The big drawback to this approach is efficiency. You would need roughly 36 LinearLayout instances to create the small 9x9 grids inside of a larger 9x9 grid...that's 36 views of pure layout overhead.


As far as text sizing, there are a couple ways I could think of to handle this. One would be to use Paint.measureTextBounds() (you can get the Paint object of any TextView to do the measurements) to determine what size you need to make the text in each button after they have been measured. Unfortunately this would be a somewhat iterative process because the Paint measures a given text based on its current settings, so you would need to:

  1. Set the text size
  2. Measure bounds
  3. Check height
  4. Repeat until the size just fits

The good news is you would only need to do this once and just apply it to all the grid buttons, but you would need to wait until the grid buttons are measured.

Another option here would be to display an image instead of text inside of something like ImageView, which can scale the content for you to its size. You could use something like the TextDrawable that I wrote to set text content as an image that is scalable without quality loss.


Now back to the layout. You could gain back a ton of efficiency by creating a custom ViewGroup to measure and lay out the grid (the name GridLayout is already taken...and it doesn't quite serve this purpose, so let's call it BlockLayout). Creating a custom BlockLayout will allow you to measure the size of each block and lay out 9 subviews in a grid with a single parent instead of 4 LinearLayout instances. This is basically the same way that you measure the overall square, just divided evenly.

You could then build the entire layout with only 10 instances of layout overhead...and even less if you can code the entire thing into a single ViewGroup.

Basically the more code you can write to flatten the view hierarchy, the better your application will run overall.

HTH

devunwired
  • 62,780
  • 12
  • 127
  • 139
  • Thanks for the thorough answer! I think I will use your TextDrawable, since it looks like by far the most convenient way to resize text. How can I use it with a button, though? Ordinarily I'd make a custom style for the button with an element `@drawable/myButton`, where myButton is a selector with shapes for each state. But it seems like you're using the TextDrawable as a background for an ImageView in your [blog post](http://wiresareobsolete.com/wordpress/2012/12/textdrawable-draw-some-text/), and I don't think Views can have two backgrounds. – 1'' Dec 30 '12 at 03:17
  • Edit: on further reading, I noticed that there's a difference between `setImageDrawable()` and `setBackgroundDrawable()`. Am I right in assuming that I should position the button and set its *backgroundDrawable* to be the selector via XML, then in Java get the button via `findViewByID` and set its *imageDrawable* to be the TextDrawable? – 1'' Dec 30 '12 at 03:33
  • Yes, I would recommend using `ImageView` or `ImageButton` and setting a `TextDrawable` as the content, not the background. This is because you can use a `ScaleType` like `FIT_CENTER` to get the text to fill the view space. With `Button` (whose content is pure text) you can't set the content as a Drawable, only the background. Also note that one disadvantage of this is that you cannot define custom Drawable types in XML, so adding it to the style isn't really possible. – devunwired Dec 30 '12 at 22:28
  • It works great! By the way, if you happen to have some advice about using a webcam with the emulator, there's [a question with a bounty](http://stackoverflow.com/questions/14012924/android-how-to-use-webcam-in-emulator) crying out for some attention. – 1'' Dec 31 '12 at 05:24
0

To complement Devnuwired's answer, you can write a method similar to this to add his TextDrawable text to an ImageButton:

private void addText(String text, ImageButton button, int colour) {
    TextDrawable d = new TextDrawable(this);
    d.setText(text);
    d.setTextColor(colour);
    d.setTextAlign(Layout.Alignment.ALIGN_CENTER);
    button.setImageDrawable(d);
}

or alternately

private void addText(String text, int buttonID, int colour) {
    TextDrawable d = new TextDrawable(this);
    d.setText(text);
    d.setTextColor(colour);
    d.setTextAlign(Layout.Alignment.ALIGN_CENTER);
    ((ImageButton) findViewById(buttonID)).setImageDrawable(d);
}
1''
  • 26,823
  • 32
  • 143
  • 200