Question

Disclaimer: I rewrote most of this question, including its title, because I partially figured it out.


I'd like to maximize the size of the button on all screen sizes, because it looks silly when it is to small. It should look similar to this:

(Sadly, I cannot include a picture, because my reputation is too low.)

But if I turn the orientation of the device, for example, the button matches it's parents width, becoming ugly proportioned.

(Sadly, I cannot include a picture, because my reputation is too low.)

I now have figured out how to get the dimensions of its parent (the LinearLayout) and how to set the button's size. I used the following code:

  • window is the ID of the LinearLayout containing (only) the button.
  • this code is located in the onCreate()-method of the MainActivity.

    // Adapt button's size to smaller dimension:
    final View window = findViewById(R.id.window);
    window.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            int width = window.getMeasuredWidth();
            int height = window.getMeasuredHeight();
            int smallerSize;
    
            if (width < height) {
                smallerSize = width;
            } else {
                smallerSize = height;
            }
    
            View button = findViewById(R.id.fartButton);
            button.setLayoutParams(new LinearLayout.LayoutParams(smallerSize, smallerSize));
    
            window.getViewTreeObserver().removeOnGlobalLayoutListener(this);
        }
    });
    

The problem with this approach is, that it doesn't seem to account for padding. The button get's cut off a little bit on the smaller side (in portrait mode its width, in landscape mode its height).
Interestingly, the image inside the button fits the window perfectly. If for example the height gets cut off a bit, the image still is visible in its full height (only some "extra" parts of the button get cut off, like a little border and shadow).

Is there a way to get the maximal size of the button, which would be the size of the window, but without action bar and minus padding, to prevent any part of the button to get cut off?

Was it helpful?

Solution 2

I just figured it out. Was much easier than I thought. Thanks to everyone who answered, though. It helped me a lot on the way!


The padding that is applied to the window can easily be accesed through the getPadding...() methods. I just needed to adjust the part where the width and height get saved:

int width = window.getMeasuredWidth() - window.getPaddingLeft() - window.getPaddingRight();
int height = window.getMeasuredHeight() - window.getPaddingTop() - window.getPaddingBottom();

I thought, that even by manually excluding the padding, the highlight when pressing the button would be cut off, because it is a bit bigger than the button itself. But this is not the case and it works perfectly. The button now gets displayed in its whole glory. ;)

OTHER TIPS

Your example above "should look similar to this:" does not seem to have loaded, illustration would help...

But you can manage screen proportions pretty well using android:layout_weight

I'm not sure I'm envisioning your exact needs, but you might try something like this:

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="X"
        android:text=" " 
        />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="button" 
        />
    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="X"
        android:text=" " 
        />
</LinearLayout>

where different values for X would control the horizontal aspect ratio for your button in a view.

You can overload your onMeasure method to always return a square.

Create a class that extends to Button and include this

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  int size = Math.min(getMeasuredWidth(), getMeasuredHeight());
  setMeasuredDimension(size, size);
}

Not sure how this will work if you give exact dimensions but it should work if you set width, height to match parent

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top