Search code examples
android-layoutandroid-activityandroid-widgetandroid-linearlayoutscreen-resolution

How can I design an activity with this apparence which work on any device with any resolution


I want to design a software which is work on any device with any screen-resolution. would you help me? I used android:layout_weight but it made me crazy

If you have any other suggestion, please tell me.

enter image description here enter image description here

This is the XML code but is tooooo long.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".TTT">

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

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="@string/Titre2"
            android:id="@+id/txtTitre2"
            android:layout_alignParentStart="false"
            android:layout_alignParentTop="false"
            android:layout_alignParentBottom="false"
            android:layout_alignParentRight="true"
            android:layout_alignParentLeft="true"
            android:gravity="center"
            android:textColor="#000000"
            android:textStyle="bold"
            android:textSize="16sp"
            android:textIsSelectable="false"
            android:layout_weight="1" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/textView"
            android:layout_below="@+id/txtTitre2"
            android:textSize="16sp"
            android:layout_weight="1" />

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="3">

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

                <LinearLayout
                    android:orientation="horizontal"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <Button
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:id="@+id/B0"
                        android:layout_row="0"
                        android:layout_column="0"
                        android:layout_weight="1"/>

                    <Button
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:id="@+id/B1"
                        android:layout_row="0"
                        android:layout_column="1"
                        android:layout_weight="1"/>

                    <Button
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:id="@+id/B2"
                        android:layout_row="0"
                        android:layout_column="2"
                        android:layout_weight="1"/>
                </LinearLayout>

                <LinearLayout
                    android:orientation="horizontal"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <Button
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:id="@+id/B3"
                        android:layout_row="1"
                        android:layout_column="0"
                        android:layout_weight="1"/>

                    <Button
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:id="@+id/B4"
                        android:layout_row="1"
                        android:layout_column="1"
                        android:layout_weight="1"/>

                    <Button
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:id="@+id/B5"
                        android:layout_row="1"
                        android:layout_column="2"
                        android:layout_weight="1"/>
                </LinearLayout>

                <LinearLayout
                    android:orientation="horizontal"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <Button
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:id="@+id/B6"
                        android:layout_row="2"
                        android:layout_column="0"
                        android:layout_weight="1"/>

                    <Button
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:id="@+id/B7"
                        android:layout_row="2"
                        android:layout_column="1"
                        android:layout_weight="1"/>

                    <Button
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:id="@+id/B8"
                        android:layout_row="2"
                        android:layout_column="2"
                        android:layout_weight="1"/>
                </LinearLayout>

            </LinearLayout>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="Small Text"
                android:id="@+id/textView5"
                android:layout_weight="2"/>

        </LinearLayout>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:id="@+id/textView7"
            android:text="12121212"
            android:layout_weight="1" />

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

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="Small Text"
                android:id="@+id/textView6" />

            <Button
                android:layout_width="200dp"
                android:layout_height="wrap_content"
                android:text="@string/New"
                android:id="@+id/button"
                android:layout_row="4"
                android:layout_column="2"
                android:onClick="newGame" />
        </LinearLayout>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:text="Small Text"
            android:id="@+id/textView8"
            android:layout_weight="3" />

    </LinearLayout>

</RelativeLayout>

Solution

  • I solved it. When we use android:layout_weight=Value we should set the value of android:layout_weight to 0dp if we use vertical linearlayout and
    if we use horizontal linearlayout we should set the value of android:layout_height to 0dp
    The value of six layout in top should be 1, 1, 3, 1, 1 ,3 respectively.
    And for having a distance of 40% from left in 5th layout, we should define it like vertical linearLayout and set the weight of first TextView which is empty to 2 and button 3 and the last Empty TextView 1

    I put the XML code which is necessairy

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <TextView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />
    
        <TextView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />
    
        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="3">
        </LinearLayout>
    
        <TextView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />
    
        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1">
    
            <TextView
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="2" />
    
            <Button
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="2" />
    
            <TextView
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1" />
        </LinearLayout>
    
        <TextView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="3" />
    
    </LinearLayout>