Set a square aspect ratio for a toggle button?



I have a toggle buttons set up within LinearLayouts with "weight" so that its dimensions will adjust based on the screen dimensions. However, I'm finding it difficult to keep it squared as one cannot predict the physic ratios of the height and width of the screen.



<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:orientation="vertical">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="4"></LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3"
android:gravity="center_horizontal">

<ToggleButton
android:id="@+id/tehButton"
android:adjustViewBounds="true"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@drawable/custom_button"
android:textOff="Off"
android:textOn="On"
android:textSize="36dp"
android:checked="false"
android:clickable="true" />
</LinearLayout>


<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="4"></LinearLayout>
</LinearLayout>


as it is right now, the button is very stretched out. ~2x as wide as it is high. In my activity, I tried using setWidth, but it had zero effect....


I tried using just an ImageView instead of a ToggleButton. It worked for the layout, but I couldn't get the text "on/off" to show up, nor could I figure out how to handle having a toggle state.


Any help/suggestions is appreciated! Thanks.


No comments:

Post a Comment