views:

67

answers:

3

I create a very simple activity where I create and set the view in java code instead of xml. The width I pass to the outer LinearLayout though has no effect at all (200). The view is displayed on the entire width of the screen, no matter what value I pass here.

(Note that this is just sample code; I know that in a real app you don't use fixed values. I just want to point out my problem here for easier clarification).

public class MyActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // outer linear layout
        LinearLayout ll = new LinearLayout(this);
        ll.setLayoutParams(new LinearLayout.LayoutParams(
                200,   // *** this param has no effect, regardless of the value I set here ***
                LinearLayout.LayoutParams.FILL_PARENT
        ));
        ll.setBackgroundColor(Color.parseColor("#00ff00"));


        // inner linear layout
        LinearLayout ll2 = new LinearLayout(this);
        ll2.setLayoutParams(new LinearLayout.LayoutParams(
                100,    // ** this width for the inner view is working fine **
                LinearLayout.LayoutParams.FILL_PARENT
        ));
        ll2.setBackgroundColor(Color.parseColor("#0000ff"));
        ll.addView(ll2);

        setContentView(ll);
    }
}

But if I replace setContentView(ll); and use a xml layout instead where the outer LinearLayout has a value of 200px, it's applied properly and the view only takes 200px of the screen.

setContentView(com.example.R.layout.main);

where main.xml is:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="200px"
    android:layout_height="fill_parent"
    android:background="#ff0000"
    >
    <TextView  
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello"
        />
</LinearLayout>

Why does setting a fixed width in java code for the outer layout has no effect?

A: 

Hi,

You can try this way.

  LinearLayout ll = new LinearLayout(this);       
  LinearLayout.LayoutParams lparam = new LinearLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT);
  lparam.width = 200;

  ll.setLayoutParams(lparam);
  ll.setBackgroundColor(Color.parseColor("#00ff00"));

  setContentView(ll);
krunal shah
tried it, doesn't work either.
Mathias Lin
+1  A: 

I figure it's a bug in Android, filed a bug report at

http://code.google.com/p/android/issues/detail?id=12244

Mathias Lin
A: 

I can't tell you 'why' exactly, but perhaps the xml has a sort of 'invisible parent' to provide an anchor. If you're desperate for a workaround, then the obvious answer is to wrap it with an invisible outer parent like this:

    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // very, very outer Linear layout!
    LinearLayout ll_InvisibleParent = new LinearLayout(this);
    ll_InvisibleParent.setBackgroundColor(Color.parseColor("#000000"));

    // outer Linear layout
    LinearLayout ll = new LinearLayout(this);
    //
    ll.setLayoutParams(new LinearLayout.LayoutParams(
            200,   // *** this param has no effect, regardless of the value I set here ***
            LinearLayout.LayoutParams.FILL_PARENT
    ));
    ll.setBackgroundColor(Color.parseColor("#00ff00"));// green

    // inner Linear layout
    LinearLayout ll2 = new LinearLayout(this);
    ll2.setLayoutParams(new LinearLayout.LayoutParams(
            100,    //  this width for the inner view is working fine 
            LinearLayout.LayoutParams.FILL_PARENT
    ));
    ll2.setBackgroundColor(Color.parseColor("#0000ff"));// blue
    ll.addView(ll2);
    //setContentView(ll); REPLACED WITH INVISIBLE PARENT
    ll_InvisibleParent.addView(ll);
    setContentView(ll_InvisibleParent);
}

that works for me

NickT
Thanks for your reply, I'm not desparate for a workaround, I already have a workaround in place. Just wondering why the param doesn't work. About: 'invisible parent' to provide an anchor -- that should be the job of the DecorView, not the outer LinearLayout. The outer LinearLayout should be the same as llOuter in the xml layout.
Mathias Lin
But in my case, your workaround wouldn't work because of my quite special case: I have an ActivityGroup, where it's LinearLayout viewgroup has the width of 3 times the screen width (since it shall be scrollable). Into this activityGroup I embed three activities (it's DecorViews). With the invisibleParent as above, I think it would take too much space. btw: what would be the default width of the ll_InvisibleParent though, if you don't set it explicitly.
Mathias Lin
Not sure what the default might be, but I'm sure you're correct in raising a bug see http://android.git.kernel.org/?p=platform/frameworks/base.git;a=commit;h=c1f9c40c23a756a11394a35f37053f796494b224 It crashes under 1.5 with mBaselineAlignedChildIndex of LinearLayout set to an index that is out of bounds. The index is 0 in 1.5 and -1 under 2.1
NickT