0

I have the layout with a lot of nested weights, which is bad for pefrormance. How is it possible to make, for example calculator keyboard without weights, but saving the size of the buttons without giving them fixed "sp" or "dp" size, the keypad has to be scaled no matter which resolution on the screen is now

I found the solution here CommonsWare answer and here Che Jame answer

but why they saying "Use android:layout_weight="1" on both Buttons" Eclipse saying I have to get reed of the weights... Are there any other solutions? or it's impossible in android platform? Here is how I have it WITH nested weights, and the code is below

I Want to save this view BUT bithout nested weights and the buttons have to be the same size, bigger then "wrap content" parameter

Thanks for any advice.

 <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="0sp"
                        android:layout_weight="1"
                        android:orientation="vertical" >

                        <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="0sp"
                            android:layout_weight="1"
                            android:orientation="horizontal" >

                            <Button
                                android:id="@+id/cashcount_form_button1"
                                android:layout_width="0dp"
                                android:layout_height="fill_parent"
                                android:layout_weight="1"
                                android:text="@string/cashcount_key_1"
                                android:textAppearance="?android:attr/textAppearanceLarge"
                                android:textColor="@color/white"
                                android:textSize="35sp"
                                android:textStyle="bold" />

                            <Button
                                android:id="@+id/cashcount_form_button2"
                                android:layout_width="0dp"
                                android:layout_height="fill_parent"
                                android:layout_weight="1"
                                android:text="@string/cashcount_key_2"
                                android:textAppearance="?android:attr/textAppearanceLarge"
                                android:textColor="@color/white"
                                android:textSize="35sp"
                                android:textStyle="bold" />

                            <Button
                                android:id="@+id/cashcount_form_button3"
                                android:layout_width="0dp"
                                android:layout_height="fill_parent"
                                android:layout_weight="1"
                                android:text="@string/cashcount_key_3"
                                android:textAppearance="?android:attr/textAppearanceLarge"
                                android:textColor="@color/white"
                                android:textSize="35sp"
                                android:textStyle="bold" />
                        </LinearLayout>

                        <LinearLayout
                            android:layout_width="fill_parent"
                            android:layout_height="0sp"
                            android:layout_weight="1"
                            android:orientation="horizontal" >

                            <Button
                                android:id="@+id/cashcount_form_button4"
                                android:layout_width="0dp"
                                android:layout_height="fill_parent"
                                android:layout_weight="1"
                                android:text="@string/cashcount_key_4"
                                android:textAppearance="?android:attr/textAppearanceLarge"
                                android:textColor="@color/white"
                                android:textSize="35sp"
                                android:textStyle="bold" />

                            <Button
                                android:id="@+id/cashcount_form_button5"
                                android:layout_width="0dp"
                                android:layout_height="fill_parent"
                                android:layout_weight="1"
                                android:text="@string/cashcount_key_5"
                                android:textAppearance="?android:attr/textAppearanceLarge"
                                android:textColor="@color/white"
                                android:textSize="35sp"
                                android:textStyle="bold" />

                            <Button
                                android:id="@+id/cashcount_form_button6"
                                android:layout_width="0dp"
                                android:layout_height="fill_parent"
                                android:layout_weight="1"
                                android:text="@string/cashcount_key_6"
                                android:textAppearance="?android:attr/textAppearanceLarge"
                                android:textColor="@color/white"
                                android:textSize="35sp"
                                android:textStyle="bold" />
                        </LinearLayout>

                        <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="0sp"
                            android:layout_weight="1"
                            android:orientation="horizontal" >

                            <Button
                                android:id="@+id/cashcount_form_button7"
                                android:layout_width="0dp"
                                android:layout_height="fill_parent"
                                android:layout_weight="1"
                                android:text="@string/cashcount_key_7"
                                android:textAppearance="?android:attr/textAppearanceLarge"
                                android:textColor="@color/white"
                                android:textSize="35sp"
                                android:textStyle="bold" />

                            <Button
                                android:id="@+id/cashcount_form_button8"
                                android:layout_width="0dp"
                                android:layout_height="fill_parent"
                                android:layout_weight="1"
                                android:text="@string/cashcount_key_8"
                                android:textAppearance="?android:attr/textAppearanceLarge"
                                android:textColor="@color/white"
                                android:textSize="35sp"
                                android:textStyle="bold" />

                            <Button
                                android:id="@+id/cashcount_form_button9"
                                android:layout_width="0dp"
                                android:layout_height="fill_parent"
                                android:layout_weight="1"
                                android:text="@string/cashcount_key_9"
                                android:textAppearance="?android:attr/textAppearanceLarge"
                                android:textColor="@color/white"
                                android:textSize="35sp"
                                android:textStyle="bold" />
                        </LinearLayout>

                        <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="0sp"
                            android:layout_weight="1"
                            android:orientation="horizontal" >

                            <Button
                                android:id="@+id/cashcount_form_comma_button"
                                android:layout_width="0dp"
                                android:layout_height="fill_parent"
                                android:layout_weight="1"
                                android:text="@string/cashcount_comma"
                                android:textAppearance="?android:attr/textAppearanceLarge"
                                android:textColor="@color/white"
                                android:textSize="35sp"
                                android:textStyle="bold" />

                            <Button
                                android:id="@+id/cashcount_form_button0"
                                android:layout_width="0dp"
                                android:layout_height="fill_parent"
                                android:layout_weight="1"
                                android:text="@string/cashcount_key_0"
                                android:textAppearance="?android:attr/textAppearanceLarge"
                                android:textColor="@color/white"
                                android:textSize="35sp"
                                android:textStyle="bold" />
                        <Button
                            android:id="@+id/cashcount_form_delete_button"
                            android:layout_width="0dp"
                            android:layout_height="match_parent"
                            android:layout_weight="1"
                            android:drawableStart="@drawable/ic_action_back_sign_image_view"
                            android:textAppearance="?android:attr/textAppearanceLarge"
                            android:textColor="@color/white"
                            android:textSize="35sp"
                            android:textStyle="bold" />
                    </LinearLayout>

                </LinearLayout>
Community
  • 1
  • 1
android.dk
  • 19
  • 1
  • 8

1 Answers1

0

As suggested by Android LinearLayouts should be replaced by RelativeLayout or GridLayout. RelativeLayout will suffice for most situations but GridLayout can come handy in certain situations like this one. You can write something similar to the below code to achieve what you want:

<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:columnCount="4"
    android:orientation="horizontal" >

    <Button
        android:layout_column="3"
        android:text="/" />

    <Button android:text="1" />

    <Button android:text="2" />

    <Button android:text="3" />

    <Button android:text="*" />

    <Button android:text="4" />

    <Button android:text="5" />

    <Button android:text="6" />

    <Button android:text="-" />

    <Button android:text="7" />

    <Button android:text="8" />

    <Button android:text="9" />

    <Button
        android:layout_gravity="fill"
        android:layout_rowSpan="3"
        android:text="+" />

    <Button
        android:layout_columnSpan="2"
        android:layout_gravity="fill"
        android:text="0" />

    <Button android:text="00" />

    <Button
        android:layout_columnSpan="3"
        android:layout_gravity="fill"
        android:text="=" />

</GridLayout>
Varun Singh
  • 1,135
  • 13
  • 18