0

I've read and tried a few answers that I found in stackoverflow but the ones I found didn't work.

I've the following dimensions (1+2 for portrait button height\width and 3+4 for landscape).

I just want the button size to be smaller in landscape than portrait

<dimen name="button_height_portrait">180dp</dimen>
<dimen name="button_width_portrait">180dp</dimen>
<dimen name="button_height_landscape">60dp</dimen>
<dimen name="button_width_landscape">60dp</dimen>

in my code I override onConfigurationChanged:

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);

        if (mScreenOrientation == Configuration.ORIENTATION_PORTRAIT) {
           if (mButton != null) {
               Integer ButtonWidthPortrait = getResources().getDimensionPixelSize(R.dimen.button_width_portrait);
               Integer ButtonHeightPortrait = getResources().getDimensionPixelSize(R.dimen.button_height_portrait);
               mButton.setWidth(ButtonWidthPortrait);
               mButton.setHeight(ButtonHeightPortrait);
           }
        }
        else if (mScreenOrientation == Configuration.ORIENTATION_LANDSCAPE) {
            if (mButton!= null) {
                Integer ButtonWidthLandscape = getResources().getDimensionPixelSize(R.dimen.button_width_landscape);
                Integer ButtonHeightLandscape = getResources().getDimensionPixelSize(R.dimen.button_height_landscape);
                mButton.setWidth(ButtonWidthLandscape);
                mButton.setHeight(ButtonHeightLandscape);
            }
        }
    }

the result is that the buttons looks with the same size exactly. I've also tried to get the dimension:

int dimenHeightPortrait = (int) (getResources().getDimension(R.dimen.start_button_height_portrait) / getResources().getDisplayMetrics().density)

and so on.. but the same result.

I update mScreenOrientation in the base method onConfigurationChanged:

if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
    mScreenOrientation = Configuration.ORIENTATION_PORTRAIT;
}
else if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
    mScreenOrientation = Configuration.ORIENTATION_LANDSCAPE;
}

Here's my full dimens file:

<resources>
    <dimen name="activity_horizontal_margin">16dp</dimen>
    <dimen name="activity_vertical_margin">16dp</dimen>
    <dimen name="nav_header_vertical_spacing">16dp</dimen>
    <dimen name="nav_header_height">160dp</dimen>
    <dimen name="fab_margin">16dp</dimen>
    <dimen name="star_text_size">26sp</dimen>
    <dimen name="toast_save_text_size">34sp</dimen>
    <dimen name="toast_image_height">50dp</dimen>
    <dimen name="toast_image_width">50dp</dimen>
    <dimen name="space_between_touch_views">10dp</dimen>
    <dimen name="button_moretext_size">20sp</dimen>
    <dimen name="button_get_text_size">20sp</dimen>
    <dimen name="key_corner_radius">3dp</dimen>
    <dimen name="key_top_inset">3dp</dimen>
    <dimen name="key_bottom_inset">3dp</dimen>
    <dimen name="key_right_inset">2.5dp</dimen>
    <dimen name="key_left_inset">2.5dp</dimen>
    <dimen name="title_text_size">11sp</dimen>


    <dimen name="button_height_portrait">180dp</dimen>
    <dimen name="button_width_portrait">180dp</dimen>
    <dimen name="button_height_landscape">60dp</dimen>
    <dimen name="button_width_landscape">60dp</dimen>
</resources>
Maor Cohen
  • 936
  • 2
  • 18
  • 33
  • 1
    sorry, forgot to mention it but I update it in the base. I'll edit the question with this info.. if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) { mScreenOrientation = Configuration.ORIENTATION_PORTRAIT; } else if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) { mScreenOrientation = Configuration.ORIENTATION_LANDSCAPE; } – Maor Cohen Mar 25 '17 at 12:10
  • Why not make this an answer? – Peter Chaula Mar 25 '17 at 12:12
  • because it's not the answer. I've already updated mScreenOrientation before I posted my question. I just didn't write here that I updated mScreenOrientation. – Maor Cohen Mar 25 '17 at 12:14
  • posted (at the end of my question) – Maor Cohen Mar 25 '17 at 12:50

1 Answers1

0

Succeeded!

I don't know why setWidth and setHeight didn't work but this works (so if someone got this problem too, then just replace the two lines of the setWidth and setHeight with these next lines and do the same for the portrait) :

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.width = ButtonWidthLandscape;
params.height = ButtonHeightLandscape;
mButton.setLayoutParams(params);
Maor Cohen
  • 936
  • 2
  • 18
  • 33