0

I have some problem with add linearlayout dynamically. It's add on the top of screen, overlay other linearlayout.

Here XML,code and results.

XML:

<TextView
    android:id="@+id/top_km"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:background="#888"
    android:gravity="top"
    android:textColor="#fff"
    android:textSize="30dip"
    android:layout_centerHorizontal="true"/>

<RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@+id/top_km"
        android:id="@id/textLayout">

</RelativeLayout>

Code:

myLayout = (RelativeLayout) page.findViewById(R.id.textLayout);
LinearLayout linLayout = new LinearLayout(this);  
linLayout.setOrientation(LinearLayout.VERTICAL);
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
myLayout.addView(linLayout);
LinearLayout hozLayout = new LinearLayout(this);
hozLayout.setOrientation(LinearLayout.HORIZONTAL);
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
myLayout.addView(hozLayout);

Results: enter link description here

Thanks

Postnik
  • 21
  • 1
  • 2

3 Answers3

2

Don't use a RelativeLayout as your holder. Use a LinearLayout with orientation="vertical" instead.

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_below="@+id/top_km"
    android:orientation="vertical"
    android:id="@id/textLayout" />

then in code

myLayout = (LinearLayout) page.findViewById(R.id.textLayout);

followed by

// rest of your code
Ken Wolf
  • 23,133
  • 6
  • 63
  • 84
1

It's because you use RealativeLayout for proper adding use 1. RelativeLayout.LayoutParams for st LayoutParams 2. In LayoutParams use field below

Example:

RelativeLayout rl=new RelativeLayout(this);
LinearLayout ll1=new LinearLayout(this);
TextView tx1=new TextView(this);
tx1.setText("Test1");
ll1.addView(tx1);
rl.addView(ll1);
LinearLayout ll2=new LinearLayout(this);
TextView tx2=new TextView(this);
tx2.setText("Test1");
ll2.addView(tx1);
rl.addView(ll2);
RelativeLayout.LayoutParams lp2=(LayoutParams) ll2.getLayoutParams();

And then use lp2.addRule

Here some help:

Parameters verb One of the verbs defined by RelativeLayout, such as ALIGN_WITH_PARENT_LEFT. anchor The id of another view to use as an anchor, or a boolean value(represented as TRUE) for true or 0 for false). For verbs that don't refer to another sibling (for example, ALIGN_WITH_PARENT_BOTTOM) just use -1.

Dmitry Nelepov
  • 7,246
  • 8
  • 53
  • 74
  • I have tried to do this: RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); params.addRule(RelativeLayout.ALIGN_BOTTOM,id_value_layout-1); hozLayout.setLayoutParams(params); But the situation is the same. – Postnik Jun 04 '13 at 17:30
0

Maybe it's easier for you to add it in the XML file with android:visibility="GONE" and then in the code just show it (View.VISIBLE) or hide it (View.GONE).

Guillermo Merino
  • 3,197
  • 2
  • 17
  • 34
  • No (( I do not know the number of LinearLayout. May be 10 or may be 100. – Postnik Jun 04 '13 at 17:26
  • So, why don't use a ListView then, with your layouts as the rows, easier to code. Anyway, if you still want to do it programmatically (which is discouraged, see here http://stackoverflow.com/questions/9827819/best-practices-layouts-on-android-programmatic-vs-xml) then do it as Ken Wolf says, with a LinearLayout as a container – Guillermo Merino Jun 05 '13 at 07:32