4

This question seems to have been asked a dozen times already bu I haven't been able to deduce the solution from them.

I have a layout called tile.xml that looks like this:

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="50dp"
    android:layout_marginTop="10dp"
    android:text="May Fong Robinson"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textStyle="bold" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="50dp"
    android:layout_marginTop="10dp"
    android:text="Beautiful star-shaped spillway, Kechut Reservoir, Jermuk, Armenia"
    android:textAppearance="?android:attr/textAppearanceMedium" />

I need to add this one of my views called feed.xml which I've declared as a ScrollView because I want to have multiple objects in it so I can vertically scroll though them. Here's my feed.xml file:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scrollView1"
    android:layout_width="fill_parent"
    android:background="#F1F1F1"
    android:layout_height="fill_parent" >

</ScrollView>

I'd like to dynamically add my "tile" from tile.xml to my "feed" feed.xml view while changing the text of the TextViews on the fly.

How can I do this? Thanks


The new feed.xml:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#F1F1F1" >

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

</ScrollView>

...and the new tile.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginTop="10dp"
    android:background="#FFFFFF"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="50dp"
        android:layout_marginTop="10dp"
        android:text="May Fong Robinson"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="50dp"
        android:layout_marginTop="10dp"
        android:text="Beautiful star-shaped spillway, Kechut Reservoir, Jermuk, Armenia"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</LinearLayout>
1
  • 1
    one thing you should know that ScrollView can add only one View in it. if you want to add multiple View then use some layouts e.g. LinearLayout and add view in it and then in the end add this LinearLayout in the ScrollView Commented Aug 14, 2012 at 18:28

3 Answers 3

4

First of all, add the 2 TextViews inside a layout.

then, in your code, where you want to dynamically add the TextViews you do the following :

ScrollView scroll = (ScrollView) findViewById(R.id.scrollView1);
LayoutInflater inflater =  (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.tile, null);
scroll.addView(view);

To access the TextViews you have to do the following :

TextView textView1 = (TextView) view.findViewById(R.id.textView1);
textView1.setText("");

Also make sure to give your TextViews different IDs in the XML file. Currently they are both textView1

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

2 Comments

HI Eddy. That sort of worked for me. I added a LinerLayout to the ScrollView and I also added the two TextViews inside a LinearLayout. I then added the LinearLayout (contianing the TextViews) to the other LinearLayout (which was nested in the ScrollView). It now seems to add it dynamically. The second part of my question: how can I modify the text in the TextViews when adding it dynamically? Thanks
I had to modify ScrollView scroll = (ScrollView) findViewById(R.id.scrollView1); to LinearLayout scroll = (LinearLayout) findViewById(R.id.tiles);
1

ScrollView can only have 1 child, meaning the current structure of your "tile" layout can't be added to the ScrollView. What you need is to add another Layout, let's say LinearLayout to your ScrollView so that it would be the ScrollView's only direct child. To this LinearLayout you can add as many childs as you want. This can be done by using the LayoutInflater which you've probably encountered in numerous examples.

Comments

0
**TRY THIS**
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#F1F1F1" >

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

   <include layout="@layout/tile" />

</LinearLayout>

1 Comment

That's not adding dynamically.

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.