gsmaker
gsmaker

Reputation: 211

LinearLayout problem

I am facing problem to customize Linear Layout. My xml is as follows:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ffffff"
    >

     <LinearLayout android:id="@+id/LinearLayout_LeftPanel" 
        android:layout_width="wrap_content" 
        android:layout_height="fill_parent" 
        android:background="#0000ff"    
        >
        <TextView android:id="@+id/LeftPanel_Title"
             android:text="Frequently asked questions"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:textColor="#000000"
            />

    </LinearLayout>


    <LinearLayout android:id="@+id/LinearLayout_MainContent"        
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"        
        android:gravity="center"
        android:layout_toRightOf="@+id/LinearLayout_LeftPanel"      
        >
        <TextView android:id="@+id/MainContent_Title"
             android:text="Alabama Rules of Civil Procedure"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content" 
             android:textColor="#ff0000"
             android:textSize="19px"
             android:textStyle="bold"
             />     
    </LinearLayout>

</RelativeLayout>

I want to set the width of @+id/LinearLayout_LeftPanel layout one third of the screen width pro-grammatically on the onCreate(). so how can do this ?

Upvotes: 0

Views: 574

Answers (2)

Matthias Schippling
Matthias Schippling

Reputation: 2953

The answer from Mina Samy is almost correct, but you're using a RelativeLayout as your topmost container. The android:layout_weight attribute only works when the surrounding container is a LinearLayout:

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

       <LinearLayout android:id="@+id/LeftPanel"
                     android:layout_weight="1">
            ...
       </LinearLayout>

       <LinearLayout android:id="@+id/MainContent"
                     android:layout_weight="3">
            ...
       </LinearLayout>

</LinearLayout>

EDIT: I just saw that you said you wanted to set it "programmatically in the onCreate method". If you really want to do this instead of setting it in the XML like I wrote above, you have to do do something like this:

protected void onCreate(Bundle b) {
    LinearLayout leftPanel = (LinearLayout)findViewById(R.id.LeftPanel);
    LinearLayout.LayoutParams leftPanelParams = (LinearLayout.LayoutParams)leftPanel.getLayoutParams();
    leftPanelParams.weight = 1;

    // do the same thing for MainContent but set the Weight to 3;
}

Upvotes: 2

Mina Wissa
Mina Wissa

Reputation: 10971

you can use set the weight property of both Linearlayouts

set the first android:layout_weight="1" and the left panel android:layout_weight="3"

thanks

Upvotes: 1

Related Questions