TeleJim
TeleJim

Reputation: 317

What's the best way to get or calculate usable screen space

I trying to get the usable screen space for my app. This would be the space between the Action/Tool bar and the Navigation bar.
Reason I'm needing it. I have six buttons in a constraint layout chained together vertically. At run time I need to resize the buttons. The app has a fixed landscape orientation.
Ultimately I want to constrain the the bottom button of the chain to the bottom parent or nav bar (if it exists) and the top button to the action/tool bar.

I've found that there's an attribute resource for the action bar/tool bar that I'm constraining the top of the chain to.

For the bottom, my thought is to constrain the bottom button with a constraint guide. I just not sure how to find the screen position to position it at.

enter image description here

enter image description here

Upvotes: 0

Views: 245

Answers (2)

N. Matic
N. Matic

Reputation: 313

There is one more way of doing this, if the previous answer doesnt work for whatever reason. In your layout editor, put buttons on the constraint layout and constrain them one to the other. Set constraints to be fixed, for example 8dp. The first button is constrained to the top of root layout, the last to the bottom. Lastly set the height of buttons to "match constraint". Am on mobile so I cant copy any xml, but this should force the height to be ruled by the dimension of root layout and lenght of constraint

Upvotes: 1

N. Matic
N. Matic

Reputation: 313

Steps:

  1. Get window height
  2. resize images proportional to that height

add this code somewhere after setContentView or after inflation:

// root layout is the constraint layout which holds the buttons
rootLayout.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
            @Override
            public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
                int rootHeight = v.getHeight();
                int btnHeight = rootHeight/6;
                btn1.getLayoutParams().height = btnHeight;
                btn1.requestLayout();
                btn2.getLayoutParams().height = btnHeight;
                btn2.requestLayout();
                // and same for the rest of your views
            }
        });

Upvotes: 0

Related Questions