Developer
Developer

Reputation: 6350

How could i divide my layout in two parts in Android

I want to dive my layout in two parts that have common design.Layout orientation is horizontal and i want to have 2 layout of that layout how could i do that .Here is my XML please suggest me how could i do that.

Upvotes: 0

Views: 2819

Answers (4)

keerthana murugesan
keerthana murugesan

Reputation: 581

  1. Make the root layout which is a <RelativeLayout> currently as <LinearLayout>.
  2. Set the orientation of that linear layout to horizontal.
  3. set android:weight of all the child layouts to '1'.
  4. use yourChildLayout.setVisibility(View.VISIBLE) to show the two layouts which are needed and yourChildLayout.setVisibility(View.GONE) to hide the rest.

Upvotes: 0

hemantsb
hemantsb

Reputation: 2059

Copy and paste below code in your xml file, and made changes as you like, You can ask for any help if you want

  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >

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

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

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

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

</LinearLayout>

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="461dp"
    android:layout_weight="1.00"
    android:orientation="vertical" >

        <Button 
          android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

        <Button
            android:id="@+id/button4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />

        <Button
            android:id="@+id/button5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />

        <Button
            android:id="@+id/button6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />

</LinearLayout>

Upvotes: 1

Exceptional
Exceptional

Reputation: 3004

For instance I have just replicated the same views everywhere, don't mind it...

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button1"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

    <Button
        android:id="@+id/button1"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

    <Button
        android:id="@+id/button1"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />
</LinearLayout>

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button1"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

    <Button
        android:id="@+id/button1"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />
</LinearLayout>

Upvotes: 0

Sumit Dhaniya
Sumit Dhaniya

Reputation: 596

Take the references of layout objects at runtime using their id and depending upon requirement write condition in your code to show hide layouts.

Upvotes: 0

Related Questions