11

In my Code I have a XML file having multiple child layouts. Aand I just want to set one of these child layout's width programmatically according to device's screen width. till now I had tried Layout params, setparam, and other methods but it shows null pointer exception on getting layout id.

my parent layout is a Relative Layout, and 1st Child Layout is a Linear Layout("for this layout I need to change width")

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/parentlayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <LinearLayout
        android:id="@+id/childlayout"
        android:layout_width="250dip"
        android:layout_height="fill_parent"
        android:orientation="vertical" 
        android:background="#153746">
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:background="@drawable/sub_title"
            android:gravity="center_vertical" >
            <TextView
                android:id="@+id/menu_header_label"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textStyle="bold"
                android:text="asd"
                android:textColor="#ffffff"
                android:layout_marginLeft="10dip" />

            <ImageView
                android:id="@+id/menu_header"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal" />
        </LinearLayout>
        <ListView
            android:id="@+id/menu_listview"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:divider="@android:color/darker_gray"
            android:dividerHeight="1dip"
            android:scrollingCache="false" />
    </LinearLayout>

    <FrameLayout
        android:id="@+id/overlay"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    </FrameLayout>

</LinearLayout>

Java Code:-

     public class SlideMenu extends LinearLayout {

    // keys for saving/restoring instance state
    public final static String KEY_MENUSHOWN = "menuWasShown";
    private final static String KEY_STATUSBARHEIGHT = "statusBarHeight";
    private final static String KEY_SUPERSTATE = "superState";

//  Display display = ((WindowManager) getWindowToken()).getDefaultDisplay(); 
//  int width = display.getWidth();  // deprecated
//  int height = display.getHeight();  // deprecated
//  float layWidth = (width*70)/100;
//  
////    LinearLayout view= (LinearLayout)findViewById(R.id.childlayout);
////    LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) view.getLayoutParams();
////    params.width = 130;
////    view.setLayoutParams(params);
//  view.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, theSizeIWant));

    public static class SlideMenuItem {
        public int id;
        public Drawable icon;
        public String label;
    } 

here is my code snippet and i comment the part what i tried so far and get any fine result..``

Dipak Keshariya
  • 22,193
  • 18
  • 76
  • 128
Manish
  • 1,259
  • 3
  • 11
  • 20

2 Answers2

12

Can you do this: Set the id to this child linear layout and then:

LinearLayout linLayout = (LinearLayout) findViewById(R.id.yourID);
LinearLayout .LayoutParams layoutParams= new 
LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 
LinearLayout.LayoutParams.MATCH_PARENT);//or whatever , you can set the width and height programmatically and then setLayoutParams()

EDIT:

I see you already tried this, sorry. But there are two linear layouts no ?

Dipak Keshariya
  • 22,193
  • 18
  • 76
  • 128
v0d1ch
  • 2,738
  • 1
  • 22
  • 27
6

Put this in java code:

int width = 300;
int height = LayoutParams.WRAP_CONTENT;
LinearLayout childLayout = (LinearLayout) findViewById(R.id.childLayout);
childLayout.setLayoutParams(new LayoutParams(width, height));
Dipak Keshariya
  • 22,193
  • 18
  • 76
  • 128
MysticMagicϡ
  • 28,593
  • 16
  • 73
  • 124
  • @ shreya: thnx for such a quick response ...but i also tried that, it gives null pointer exception at 4th line where we set layout params :(. – Manish Dec 05 '12 at 10:47
  • Then I think you haven't found layout reference correctly. childLayout was still null at that point. Please post your code – MysticMagicϡ Dec 05 '12 at 10:47
  • May be these lines are creating issue when not commented: LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) view.getLayoutParams(); params.width = 130; view.setLayoutParams(params); – MysticMagicϡ Dec 05 '12 at 11:27
  • no actually i tried diff code, and just put them commented for future references.. BTW i found my silly mistake its just i have to give reference to my childlayout via slidemenu.xml what i used in my program.. – Manish Dec 05 '12 at 13:02