29

I'm trying to add dynamic content to a view that has been created with XML.

I have a view "profile_list" that has a ScrollView that I like to add some elements to.

Here is some code of what I'm trying to do.

// Find the ScrollView 
ScrollView sv = (ScrollView) this.findViewById(R.id.scrollView1);

// Create a LinearLayout element
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);

// Add text
tv = new TextView(this);
tv.setText("my text");
ll.addView(tv);

// Add the LinearLayout element to the ScrollView
sv.addView(ll);

// Display the view
setContentView(R.layout.profile_list);

The plan is to add a TableLayout and fill it dynamically and not just a dummy text but first I must get this to work.

Any help is welcome.

Kind Regards Olle

I found the solution!

Stupid me I had left a element in my ScrollView in my XML-file!

Anyway her is a working example:

LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View v = inflater.inflate(R.layout.profile_list, null);

    // Find the ScrollView 
    ScrollView sv = (ScrollView) v.findViewById(R.id.scrollView1);

    // Create a LinearLayout element
    LinearLayout ll = new LinearLayout(this);
    ll.setOrientation(LinearLayout.VERTICAL);

    // Add text
    TextView tv = new TextView(this);
    tv.setText("my text");
    ll.addView(tv);

    // Add the LinearLayout element to the ScrollView
    sv.addView(ll);

    // Display the view
    setContentView(v);

And the XML-file to match

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

    <ScrollView 
        android:id="@+id/scrollView1" 
        android:clickable="true" 
        android:layout_weight="1" 
        android:layout_width="fill_parent" 
        android:layout_marginBottom="50px" 
        android:layout_height="fill_parent">
    </ScrollView>

</LinearLayout>

Hope this will help someone. Thanks for all help!

Marek Sebera
  • 39,650
  • 37
  • 158
  • 244
Olle
  • 315
  • 1
  • 4
  • 8
  • i don't quietly understand what u want to do. How about create custom view ??? I think it's better – Hein Jun 16 '11 at 08:36
  • By custom view you mean creating the whole view dynamically? I'm quite new at his so forgive me if a ask a stupid question. I tried this but I could not get the design and layout to work that way so I started all over. – Olle Jun 16 '11 at 08:55

1 Answers1

6

The way that you are adding things to the view is basically right - you just get the container object by its ID and then add them.

It looks like you are setting the content view to be the wrong view though. You should inflate the view, add the children and set it as the content view, or set the content view from a resource ID then do the manipulation. What you are doing is manipulating a view and then adding a different one. Try moving your setContentView(...) to the start of the block.

fleetway76
  • 1,600
  • 9
  • 12
  • 1
    It didn´t work for me. I tried this at the top `LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); View v = inflater.inflate(R.layout.profile_list, null); // Find the ScrollView ScrollView sv = (ScrollView) v.findViewById(R.id.scrollView1);` And I put the `// Display the view setContentView(v);` last – Olle Jun 16 '11 at 09:03