Shoaib
Shoaib

Reputation: 90

Dynamic Layout generation Android

I am having trouble generating below mentioned android layout with code .

<RelativeLayout
    android:id="@+id/profile_info_gender"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <LinearLayout
        android:id="@+id/ProfileTable"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#D9D9D9"
        android:baselineAligned="false"
        android:orientation="horizontal" >

        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_weight="0.5"
            android:background="#F3F3F3"
            android:orientation="horizontal" >

            <TextView
                android:id="@+id/gender_txt"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="15dp"
                android:layout_marginTop="8dp"
                android:textColor="@color/info_left"
                android:textSize="14sp"
                android:textStyle="bold" />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginRight="10dp"
            android:layout_weight="0.5"
            android:background="@color/white"
            android:orientation="horizontal" >

            <TextView
                android:id="@+id/profile_info_text_gender"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="15dp"
                android:layout_marginTop="8dp"
                android:textSize="14sp" />
        </RelativeLayout>
    </LinearLayout>
</RelativeLayout>

i tried alot of things from google and blogs but could not end up with this layout any help will highly be appreciated and will be admired thanks

Upvotes: 0

Views: 172

Answers (2)

Lucian Novac
Lucian Novac

Reputation: 1265

Why not do this:

RelativeLayout profile_info_gender =  new RelativeLayout(your activity);
LinearLayout ProfileTable = new LinearLayout(your activity);
profile_info_gender.addView(ProfileTable);

For orientation, use LayoutParams.

Upvotes: 0

Hariharan
Hariharan

Reputation: 24853

Try this..

    LinearLayout first_lay = new LinearLayout(this);

    LinearLayout.LayoutParams lp_icon = new LinearLayout.LayoutParams(
              LinearLayout.LayoutParams.MATCH_PARENT, 
              LinearLayout.LayoutParams.MATCH_PARENT);

    first_lay.setOrientation(LinearLayout.HORIZONTAL);
    first_lay.setLayoutParams(lp_icon);

    LinearLayout left_lay = new LinearLayout(this);

    LinearLayout.LayoutParams left_icon = new LinearLayout.LayoutParams(
              0, 
              LinearLayout.LayoutParams.WRAP_CONTENT,1);

    left_lay.setGravity(Gravity.CENTER);

    left_lay.setBackgroundColor(Color.parseColor("#696969"));

    left_lay.setLayoutParams(left_icon);


     LinearLayout.LayoutParams tests = new LinearLayout.LayoutParams(
              LinearLayout.LayoutParams.WRAP_CONTENT, 
              LinearLayout.LayoutParams.WRAP_CONTENT, Gravity.CENTER);

     TextView text1 = new TextView(this);
     left_lay.addView(text1);
     text1.setLayoutParams(tests);
     text1.setText("Left Text");
     text1.setTextSize(18);

     first_lay.addView(left_lay);

     LinearLayout right_lay = new LinearLayout(this);

    LinearLayout.LayoutParams right_icon = new LinearLayout.LayoutParams(
              0, 
              LinearLayout.LayoutParams.WRAP_CONTENT,1);

    right_lay.setGravity(Gravity.CENTER);

    right_lay.setBackgroundColor(Color.parseColor("#D9D9D9"));

    right_lay.setLayoutParams(right_icon);

    TextView text2 = new TextView(this);
    right_lay.addView(text2);
     text2.setLayoutParams(tests);
     text2.setText("Right Text");
     text2.setTextSize(18);

     first_lay.addView(right_lay);

Upvotes: 1

Related Questions