1

How I can add LinearLayout and RelativeLayout programmatically in a ScrollView :

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

    <ScrollView
        android:id="@+id/scrollID"
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:layout_weight="1" >


    </ScrollView>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:baselineAligned="true"
        android:orientation="horizontal"
        android:paddingBottom="5dp"
        android:paddingLeft="5dp"
        android:paddingRight="5dp"
        android:weightSum="1.5" 
        android:background="@drawable/bottom_conversation">

           <TextView
            android:id="@+id/isTyping"
            android:layout_width="0dip"
            android:layout_weight="0.5"
            android:layout_height="wrap_content"
            android:hint="Chat" />

        <EditText
            android:id="@+id/txtInpuConversation"
            android:layout_width="200dip"
            android:layout_height="wrap_content"
            android:layout_weight="0.5"
            android:hint="@string/edt_Conversation" >

            <requestFocus />
        </EditText>

        <ImageButton
            android:id="@+id/someID"
            android:layout_width="0dip"
            android:layout_weight="0.5"
            android:layout_height="wrap_content"
            android:background="@drawable/background_selector"
            android:contentDescription="@null"
            android:layout_marginTop="5dp"
            android:src="@drawable/social_send_now" />
    </LinearLayout>

</LinearLayout>

Notice : I need that RelativeLayout insert in LinearLayout

2 Answers 2

3
TextView tv = new TextView(this);
tv.setText("Testsssssssssssssss");
tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
tv.setId(1);

RelativeLayout relativeLayout = new RelativeLayout(this);
LayoutParams LLParamsT = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
relativeLayout.setLayoutParams(LLParamsT);
relativeLayout.addView(tv,
        new LayoutParams(LayoutParams.WRAP_CONTENT,
               LayoutParams.WRAP_CONTENT));

LinearLayout firstLinearLayout= new LinearLayout(this);
firstLinearLayout.setOrientation(LinearLayout.VERTICAL);
LayoutParams LLParams = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
firstLinearLayout.setLayoutParams(LLParams);
firstLinearLayout.addView(relativeLayout);


ScrollView sclView = (ScrollView) findViewById(R.id.scrollID);
sclView.addView(firstLinearLayout, new
          LayoutParams(LayoutParams.WRAP_CONTENT,
                     LayoutParams.WRAP_CONTENT));

Good Luck...

Sign up to request clarification or add additional context in comments.

Comments

-2

You can add Views ( LinearLayout, RelativeLayout... ) with

scrollView.addView(newLinearLayoutView);

1 Comment

note, ScrollView can only have one direct child at a time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.