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>