How to center a linearlayout with 6 buttons centered in portrait and landscape?

What I want to achieve: center location on both sides.

    <ScrollView
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="#fffffce0">
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="#fffffce0"
    android:weightSum="1">


    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="121dp"
        android:background="#fffffce0"
        android:layout_marginTop="42dp"
        android:id="@+id/linearLayout"
        android:gravity="center_horizontal"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true">

        <Button
            android:layout_width="120dp"
            android:layout_height="120dp"
            android:id="@+id/button9"
            android:background="@drawable/custom_button_square"
            android:text="@string/a_d"
            android:textColor="#ffff"
            android:textSize="35sp"
            android:onClick="goToAd"/>

        <Button
            android:layout_width="120dp"
            android:layout_height="120dp"
            android:id="@+id/button10"
            android:background="@drawable/custom_button_square"
            android:text="@string/e_h"
            android:textColor="#ffff"
            android:textSize="35sp" />
    </LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="120dp"
        android:layout_below="@+id/linearLayout"
        android:layout_centerHorizontal="true"
        android:gravity="center_horizontal"
        android:background="#fffffce0"
        android:id="@+id/linearLayout2">

        <Button
            android:layout_width="120dp"
            android:layout_height="120dp"
            android:id="@+id/button11"
            android:background="@drawable/custom_button_square"
            android:text="@string/i_l"
            android:textColor="#ffff"
            android:textSize="35sp" />

        <Button
            android:layout_width="120dp"
            android:layout_height="120dp"
            android:id="@+id/button12"
            android:background="@drawable/custom_button_square"
            android:text="@string/m_p"
            android:textColor="#ffff"
            android:textSize="35sp" />
    </LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="120dp"
        android:layout_below="@+id/linearLayout2"
        android:layout_centerHorizontal="true"
        android:background="#fffffce0"
        android:gravity="center_horizontal">

        <Button
            android:layout_width="120dp"
            android:layout_height="120dp"
            android:id="@+id/button13"
            android:background="@drawable/custom_button_square"
            android:text="@string/m_p"
            android:textColor="#ffff"
            android:textSize="35sp" />

        <Button
            android:layout_width="120dp"
            android:layout_height="120dp"
            android:id="@+id/button14"
            android:background="@drawable/custom_button_square"
            android:text="@string/q_t"
            android:textColor="#ffff"
            android:textSize="35sp" />
    </LinearLayout>

</RelativeLayout>

      

PROBLEM: Everything was fine until I tested my app on a large screen tablet. When the orientation is landscape everything is fine, but when I rotate the screen then it looks like this: http://zapodaj.net/b2780f7e637ab.png.html

I tried to set gravity so that Llayout is centered but it didn't work.

+3


source to share


3 answers


Good! Like this. Try it.

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fffffce0" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#fffffce0"
    android:gravity="center"
    android:layout_gravity="center"
    android:orientation="vertical"
    android:weightSum="1" >

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="match_parent"
        android:layout_height="121dp"
        android:layout_marginTop="42dp"
        android:background="#fffffce0"
        android:gravity="center_horizontal"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/button9"
            android:layout_width="120dp"
            android:layout_height="120dp"
            android:background="@drawable/ic_launcher"
            android:onClick="goToAd"
            android:text="a_d"
            android:textColor="#ffff"
            android:textSize="35sp" />

        <Button
            android:id="@+id/button10"
            android:layout_width="120dp"
            android:layout_height="120dp"
            android:background="@drawable/ic_launcher"
            android:text="e_h"
            android:textColor="#ffff"
            android:textSize="35sp" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayout2"
        android:layout_width="match_parent"
        android:layout_height="120dp"
        android:background="#fffffce0"
        android:gravity="center_horizontal"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/button11"
            android:layout_width="120dp"
            android:layout_height="120dp"
            android:background="@drawable/ic_launcher"
            android:text="i_l"
            android:textColor="#ffff"
            android:textSize="35sp" />

        <Button
            android:id="@+id/button12"
            android:layout_width="120dp"
            android:layout_height="120dp"
            android:background="@drawable/ic_launcher"
            android:text="m_p"
            android:textColor="#ffff"
            android:textSize="35sp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="120dp"
        android:background="#fffffce0"
        android:gravity="center_horizontal"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/button13"
            android:layout_width="120dp"
            android:layout_height="120dp"
            android:background="@drawable/ic_launcher"
            android:text="m_p"
            android:textColor="#ffff"
            android:textSize="35sp" />

        <Button
            android:id="@+id/button14"
            android:layout_width="120dp"
            android:layout_height="120dp"
            android:background="@drawable/ic_launcher"
            android:text="q_t"
            android:textColor="#ffff"
            android:textSize="35sp" />
    </LinearLayout>
</LinearLayout>

      



0


source


due to different screen resolutions, try not to use these hardcoded numbers for example android:layout_height="70dp"

. You can automatically set layout options for each element of your layout to match the actual screen resolution, in which case it will work with every screen size and orientation. Try something like this:

//declare two global variables that will have the width and height values
private int width;
private int height;

//define a method that will set the values to the both variables
private static void getScreenResolution(Context context){
    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    Display display = wm.getDefaultDisplay();
    DisplayMetrics metrics = new DisplayMetrics();
    display.getMetrics(metrics);
    width = metrics.widthPixels;
    height = metrics.heightPixels;
}

      

In your method, onCreate()

first call the method above to get the values ​​and then set the layout parameters (just an example):



getScreenResolution(this); //if you are in a fragment, type getActivity() instead of this
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,                             LayoutParams.MATCH_PARENT);
layoutParams.setMargins(width/10, height/15, 0 ,0);   //left, top, right, bottom
yourLinearLayout.setLayoutParams(layoutParams);

      

Hope this helps you build more general projects in the future :)

+1


source


   <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fffffce0" >

<RelativeLayout android:id="@+id/layout" 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"

    >
<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#fffffce0"
    android:orientation="vertical"
    android:weightSum="3" >

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="wrap_content"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:background="#fffffce0"
        android:orientation="horizontal"
        android:weightSum="2" >

        <Button
            android:id="@+id/button9"
            android:layout_width="0dip"
            android:layout_height="120dp"
            android:layout_weight="1"
            android:background="@drawable/ic_launcher"
            android:onClick="goToAd"
            android:text="a_d"
            android:textColor="#ffff"
            android:textSize="35sp" />

        <Button
            android:id="@+id/button10"
            android:layout_width="0dip"
            android:layout_height="120dp"
            android:layout_weight="1"
            android:background="@drawable/ic_launcher"
            android:text="e_h"
            android:textColor="#ffff"
            android:textSize="35sp" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayout2"
        android:layout_width="wrap_content"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:background="#fffffce0"
        android:orientation="horizontal"
        android:weightSum="2" >

        <Button
            android:id="@+id/button11"
             android:layout_width="0dip"
            android:layout_height="120dp"
            android:layout_weight="1"
            android:background="@drawable/ic_launcher"
            android:text="i_l"
            android:textColor="#ffff"
            android:textSize="35sp" />

        <Button
            android:id="@+id/button12"
            android:layout_width="0dip"
            android:layout_height="120dp"
            android:layout_weight="1"
            android:background="@drawable/ic_launcher"
            android:text="m_p"
            android:textColor="#ffff"
            android:textSize="35sp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:weightSum="2"
        android:background="#fffffce0"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/button13"
            android:layout_width="0dip"
            android:layout_height="120dp"
            android:layout_weight="1"
            android:background="@drawable/ic_launcher"
            android:text="m_p"
            android:textColor="#ffff"
            android:textSize="35sp" />

        <Button
            android:id="@+id/button14"
            android:layout_width="0dip"
            android:layout_height="120dp"
            android:layout_weight="1"
            android:background="@drawable/ic_launcher"
            android:text="q_t"
            android:textColor="#ffff"
            android:textSize="35sp" />
    </LinearLayout>
</LinearLayout>

      

0


source







All Articles