0

I try to use a combination of RelativeLayout and LinearLayout full of TextViews and a button on the bottom. I write a sequence in MainActivity that change the orientation from horizontal to vertical and vice versa. So basically I had to turn lines in columns.

Here is the xml file:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="10dp"
android:paddingRight="10dp" >

<LinearLayout
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="0dp"
    android:paddingTop="40dp"
    android:paddingRight="16dp"
    android:orientation="horizontal"
    android:gravity="top"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textv1"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_gravity="left"
        android:text="@string/text1"

        />

    <TextView
        android:id="@+id/textv2"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:text="@string/text2"/>

    <TextView
        android:id="@+id/textv3"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:text="@string/text3"/>

    <TextView
        android:id="@+id/textv4"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:text="@string/text4"/>

</LinearLayout>

<LinearLayout
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:gravity="center"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textv5"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_gravity="center"
        android:text="@string/text5"/>

    <TextView
        android:id="@+id/textv6"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:text="@string/text6"/>

    <TextView
        android:id="@+id/textv7"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:text="@string/text7"/>

    <TextView
        android:id="@+id/textv8"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:text="@string/text8"/>


</LinearLayout>

<LinearLayout
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout3"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:gravity="bottom"
    android:foregroundGravity="right"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textv9"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_gravity="bottom"
        android:text="@string/text9"/>

    <TextView
        android:id="@+id/textv10"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:text="@string/text10" />

    <TextView
        android:id="@+id/textv11"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:text="@string/text11"/>

    <TextView
        android:id="@+id/textv12"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:text="@string/text12"/>


</LinearLayout>

<Button
    android:id="@+id/button2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Button"
    android:layout_alignParentBottom="true"/>

</RelativeLayout>

It should appear like this:

how should appear

But it turn like this:

horizontal

vertical

Only the center line stay in the right place the others stack on top of each other.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
maximus383
  • 584
  • 8
  • 25
  • You may have better luck with a TableLayout: https://developer.android.com/reference/android/widget/TableLayout – Benjamin Cassidy Oct 13 '20 at 17:37
  • I just have to use: Linear and Relative. – maximus383 Oct 13 '20 at 17:38
  • @Soni, Android provides an option to use the configuration of the phone to select application's resources. It means you can create two layout files with the same name in different `layout` directories one for portrait and one for landscape orientation. [Read more here](https://developer.android.com/guide/topics/resources/runtime-changes) about handling configuration changes. I assure you - it will be the easiest way. SO answer related with example solution: https://stackoverflow.com/a/4858052/7210237 – Jenea Vranceanu Oct 13 '20 at 17:42
  • @JeneaVranceanu, I know it but I don't have to use the orientation of screen, just change the orientation of LinearLayout inside the Relative – maximus383 Oct 13 '20 at 17:45

4 Answers4

0

How does this look? Linear Margin

code:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:layout_marginTop="40dp">


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


    <LinearLayout
        android:id="@+id/LinearLayout1"
        android:layout_width="match_parent"
        android:layout_height="147dp"
        android:gravity="top"
        android:orientation="horizontal"
        android:paddingLeft="0dp"
        android:paddingTop="40dp"
        android:paddingRight="16dp"
        tools:context=".MainActivity">

        <TextView
            android:id="@+id/textv1"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_gravity="left"
            android:text="@string/text1"

            />

        <TextView
            android:id="@+id/textv2"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:text="@string/text2"


            />

        <TextView
            android:id="@+id/textv3"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:text="@string/text3"

            />

        <TextView
            android:id="@+id/textv4"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:text="@string/text4"

            />


    </LinearLayout>

    <LinearLayout
        android:id="@+id/LinearLayout2"
        android:layout_width="393dp"
        android:layout_height="141dp"
        android:gravity="center"
        android:orientation="horizontal"

        tools:context=".MainActivity">

        <TextView
            android:id="@+id/textv5"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_gravity="center"
            android:text="@string/text5"

            />

        <TextView
            android:id="@+id/textv6"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:text="@string/text6"


            />

        <TextView
            android:id="@+id/textv7"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:text="@string/text7"

            />

        <TextView
            android:id="@+id/textv8"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:text="@string/text8"

            />


    </LinearLayout>

    <LinearLayout
        android:id="@+id/LinearLayout3"
        android:layout_width="397dp"
        android:layout_height="104dp"
        android:foregroundGravity="right"
        android:gravity="bottom"
        android:orientation="horizontal"
        tools:context=".MainActivity">

        <TextView
            android:id="@+id/textv9"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_gravity="bottom"
            android:text="@string/text9"

            />

        <TextView
            android:id="@+id/textv10"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:text="@string/text10"


            />

        <TextView
            android:id="@+id/textv11"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:text="@string/text11"

            />

        <TextView
            android:id="@+id/textv12"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:text="@string/text12"

            />


    </LinearLayout>
</LinearLayout>

<Button
    android:id="@+id/button2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Button"
    android:layout_alignParentBottom="true"/>
0

If you are the textviews to be evenly spaced in a row I suggest you use layout_weight within each LinearLayout, and arrange the LinearLayouts containing the textviews relative to each other. But I really suggest changing your approach. One you set the width and height of textviews to 100dp but Android has many screen sizes so they won't always fit exactly on many devices, and there is a performance hit, using these many views in multiple layout for lower end devices. My suggestion is use a RecyclerView with CardView containing your TextView then use a GridLayout with the recyclerview. You can change the layout of the TextView accordingly via recyclerview inside onClick for button.

SABANTO
  • 1,316
  • 9
  • 24
0

Change android:layout_height="match_parent" to android:layout_height="wrap_content" in LinearLayout.

<LinearLayout
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/LinearLayout3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="bottom"
        android:foregroundGravity="right"
        tools:context=".MainActivity">...
Yeldar Nurpeissov
  • 1,786
  • 14
  • 19
0

I manage to solve the problem by twiking the main file with some lines. Here is the added line:

            if(myll3.getOrientation() == LinearLayout.HORIZONTAL)
                myll3.setGravity(Gravity.BOTTOM | Gravity.CENTER);

            else if(myll3.getOrientation() == LinearLayout.VERTICAL)
                myll3.setGravity(Gravity.RIGHT | Gravity.CENTER);

And I made some changes in the xml file too. Here is the final XML code:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="0dp"
android:paddingRight="0dp" >



<LinearLayout
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="0dp"
    android:paddingTop="100dp"
    android:paddingRight="16dp"
    android:orientation="horizontal"
    android:gravity="top"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textv1"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:text="@string/text1"

        />

    <TextView
        android:id="@+id/textv2"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:text="@string/text2"


        />

    <TextView
        android:id="@+id/textv3"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:text="@string/text3"

        />

    <TextView
        android:id="@+id/textv4"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:text="@string/text4"

        />



</LinearLayout>
<LinearLayout
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:paddingRight="10dp"
    android:gravity="center"

    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textv5"
        android:layout_width="100dp"
        android:layout_height="100dp"

        android:text="@string/text5"

        />

    <TextView
        android:id="@+id/textv6"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:text="@string/text6"


        />

    <TextView
        android:id="@+id/textv7"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:text="@string/text7"

        />

    <TextView
        android:id="@+id/textv8"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:text="@string/text8"

        />


</LinearLayout>

<LinearLayout
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout3"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:paddingBottom="100dp"
    android:paddingTop="100dp"
    android:gravity="bottom"

    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textv9"
        android:layout_width="100dp"
        android:layout_height="100dp"

        android:text="@string/text9"

        />

    <TextView
        android:id="@+id/textv10"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:text="@string/text10"


        />

    <TextView
        android:id="@+id/textv11"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:text="@string/text11"

        />

    <TextView
        android:id="@+id/textv12"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:text="@string/text12"

        />


</LinearLayout>

<Button
    android:id="@+id/button2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Button"
    android:layout_alignParentBottom="true"/>

</RelativeLayout>

Thx for your support and your answers.

Here is the final view:- horizontal

vertical

maximus383
  • 584
  • 8
  • 25