5

I have an abc.xml with the following structure.

<ScrollView
  android:layout_width="match_parent"
  android:layout_height="match_parent">
 <RelativeView
   android:layout_width="match_parent"
   android:layout_height="wrap_content">
  <LinearLayout
    android:id="@+id/linear"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    >
  </LinearLayout>
 </RelativeLayout>
</ScrollView>

I want to add textviews dynamically to linear layout. Below is my code. I don't get any errors but I am not getting the desired results.

LayoutInflater Inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = Inflater.inflate(R.layout.abc, null);

LinearLayout layout = (LinearLayout) view.findViewById(R.id.linear);

        TextView Tag = new TextView(getActivity());
        Tag.setText("textString");
        Tag.setBackgroundResource(R.color.bg_color);
        Tag.setTextAppearance(getActivity(), R.style.SmallFont);
        layout.addView(Tag);
2
  • Why you are using RelativeLayout ? Commented Dec 18, 2013 at 10:59
  • I have other views in the relative layout as well like textviews and buttonviews Commented Dec 18, 2013 at 11:00

3 Answers 3

6

your xml should be

<ScrollView
  android:layout_width="match_parent"
  android:layout_height="match_parent">
 <LinearLayout
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:orientation="vertical">
  <LinearLayout
    android:id="@+id/linear"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    >
  </LinearLayout>
 </LinearLayout>
</ScrollView>

and the java code to add your text view is

LinearLayout layout = (LinearLayout) view.findViewById(R.id.linear);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
    LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
TextView Tag = new TextView(getActivity());
tag.setLayoutParams(params);
Tag.setText("textString");
Tag.setBackgroundResource(R.color.bg_color);
Tag.setTextAppearance(getActivity(), R.style.SmallFont);
layout.addView(Tag);
Sign up to request clarification or add additional context in comments.

Comments

0

I do not know you have found the answer yet, but I had just come across this problem and I found 1 way. I think you need to adjust the layout of one bit as follows:

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <RelativeView
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <LinearLayout
                android:id="@+id/linear"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >
            </LinearLayout>
        </LinearLayout>

    </RelativeView>

</ScrollView>

Comments

0

You must give,

android:layout_orientation="either horizontal or vertical"

to the Linear Layout with id:linear.

Comments

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.