Siddhartha Sadhukhan Siddhartha Sadhukhan - 2 months ago 21
Android Question

android layout allignment issue

I want to put "type 1" and "type 2" in the same line and make everything on center. everything means everything. I am working on the api level 23. I trued android:layout_centerHorizontal="true" in the inner linearlayout but it doesn't work

<?xml version="1.0" encoding="utf-8"?>
<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.sid.roommanager.BtRanCol">



<LinearLayout
android:id="@+id/LinearLayout02"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_alignParentTop="true"

>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Milliseconds"

android:id="@+id/tv_milli"
/>

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText"
android:inputType="number"

android:text="1000"
/>
</LinearLayout>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Type 1"
android:id="@+id/bt_led1"
android:layout_below="@+id/LinearLayout02"
android:layout_marginEnd="134dp"
android:layout_centerHorizontal="true"
/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Type 2"
android:id="@+id/bt_led2"
android:layout_below="@+id/bt_led1"
android:layout_centerHorizontal="true"
/>

</RelativeLayout>

Answer

There are many solutions to this problem. One of them is given below. We basically use nested <LinearLayout> elements with gravity set to center_horizontal:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:gravity="center_horizontal"
              android:orientation="vertical"
              android:paddingBottom="@dimen/activity_vertical_margin"
              android:paddingLeft="@dimen/activity_horizontal_margin"
              android:paddingRight="@dimen/activity_horizontal_margin"
              android:paddingTop="@dimen/activity_vertical_margin"
              tools:context="com.example.sid.roommanager.BtRanCol">


    <LinearLayout
        android:id="@+id/LinearLayout02"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal">

        <TextView
            android:id="@+id/tv_milli"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Milliseconds"
            />

        <EditText
            android:id="@+id/editText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:inputType="number"
            android:text="1000"
            />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal">

        <Button
            android:id="@+id/bt_led1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Type 1"
            />

        <Button
            android:id="@+id/bt_led2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Type 2"
            />

    </LinearLayout>


</LinearLayout>
Comments