[Android Q&A, Help & Troubleshooting] Srapur:
Hi dear developers,

I spend much time with this issue.

When I create LinearLayout with four ToggleButtons with self images, button image will not keep aspect ratio, images will be deformed.

Button images pressed in vertical, not in horizontal.

This is layout code with deformed images:

<LinearLayout

android:id="@+id/linearLayout4"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_marginLeft="0px"

android:orientation="horizontal">

<ToggleButton

android:id="@+id/bt1"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:layout_weight="1"

android:background="@drawable/led"

android:text="Led"

android:textOff="BT1"

android:textOn="BT1" />

<ToggleButton

android:id="@+id/bt2"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:layout_weight="1"

android:background="@drawable/led"

android:text="Led"

android:textOff="BT2"

android:textOn="BT2" />

<ToggleButton

android:id="@+id/bt3"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:layout_weight="1"

android:background="@drawable/led"

android:text="Led"

android:textOff="BT3"

android:textOn="BT3" />

<ToggleButton

android:id="@+id/bt"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:layout_weight="1"

android:background="@drawable/led"

android:text="Led"

android:textOff="BT4"

android:textOn="BT4" />

</LinearLayout>

When I create similar LinearLayout with four ImageViews instead of ToggleButtons with same images, images

not will be

deformed. Same images

keep aspect ratio

. Button images pressed in vertical and horizontal with same ratio.

<LinearLayout

android:id="@+id/linearLayout3"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_marginLeft="0px"

android:orientation="horizontal">

<ImageView

android:id="@+id/img1"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:layout_weight="1"

android:src="@drawable/led" />

<ImageView

android:id="@+id/img2"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:layout_weight="1"

android:src="@drawable/led" />

<ImageView

android:id="@+id/img3"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:layout_weight="1"

android:src="@drawable/led" />

<ImageView

android:id="@+id/img4"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:layout_weight="1"

android:src="@drawable/led" />

</LinearLayout>

I don't know, why it's behavior different. Size of image is 128 x 128 pixels in png file.

How I can reach that ToggleButton's image will keep aspect ratio for all relative sizes?

Application is compiled in Android Studio 3.6.1 with build gradle:

...

android {

compileSdkVersion 25

buildToolsVersion '28.0.3'

defaultConfig {

applicationId "com.nabto.androidaudiodemo"

minSdkVersion 11

targetSdkVersion 25

versionCode 1

versionName "1.0"

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

}

...

Thank you for your help

Srapur