Skip to content Skip to sidebar Skip to footer

How To Center Linearlayout With 6 Buttons On The Center On Portrait And Landscape Orientation?

What I want to achieve: Center layout with both orientation. Copy

In your onCreate() method first call the method above to get the values and then set the layout params (just a simple example):

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

Hopefully this helps you to create more general designs in the future :)

Solution 2:

Well ! Like This. Try This..

<ScrollViewxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:background="#fffffce0" ><LinearLayoutandroid: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" ><LinearLayoutandroid: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" ><Buttonandroid: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" /><Buttonandroid: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><LinearLayoutandroid:id="@+id/linearLayout2"android:layout_width="match_parent"android:layout_height="120dp"android:background="#fffffce0"android:gravity="center_horizontal"android:orientation="horizontal" ><Buttonandroid: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" /><Buttonandroid: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><LinearLayoutandroid:layout_width="match_parent"android:layout_height="120dp"android:background="#fffffce0"android:gravity="center_horizontal"android:orientation="horizontal" ><Buttonandroid: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" /><Buttonandroid: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>

Solution 3:

<ScrollViewxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:background="#fffffce0" ><RelativeLayoutandroid:id="@+id/layout"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"

    ><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:background="#fffffce0"android:orientation="vertical"android:weightSum="3" ><LinearLayoutandroid: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" ><Buttonandroid: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" /><Buttonandroid: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><LinearLayoutandroid: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" ><Buttonandroid: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" /><Buttonandroid: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><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="0dip"android:layout_weight="1"android:weightSum="2"android:background="#fffffce0"android:orientation="horizontal" ><Buttonandroid: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" /><Buttonandroid: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>

Post a Comment for "How To Center Linearlayout With 6 Buttons On The Center On Portrait And Landscape Orientation?"