2

I can't quite get this to work, hoping to get some hints, it seems like from my research the code should work, any thoughts would be greatly appreciated...

I have an existing layout.xml file that includes:

<RelativeLayout style="@style/bodyLayout">
    <ScrollView 
        android:id="@+id/scrollBody"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="false"
    >
        <LinearLayout
            android:id="@+id/scrollLinearLayout"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
        >           

        </LinearLayout>
    </ScrollView>
</RelativeLayout>

Then I have programatic code as follows:

    LinearLayout ll = new LinearLayout(this);
    ll = (LinearLayout)findViewById(R.id.scrollLinearLayout);

    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);

    TextView tv = new TextView(this);
    tv.setId(1);
    tv.setTextSize(R.dimen.text_size_medium);
    tv.setText("test");
    tv.setLayoutParams(lp);
    ll.addView(tv);

The new TextView doesn't appear when the activity is displayed, I know I a missing something obvious... the new TextView should appear in the LinearLayout section...

Telegard
  • 137
  • 3
  • 12
  • this link can help you: https://stackoverflow.com/questions/4203506/how-can-i-add-a-textview-to-a-linearlayout-dynamically-in-android/27781046#27781046 – Mohammad Mirzakhani May 29 '15 at 15:50

1 Answers1

2

I have run your code works fine

LinearLayout ll = (LinearLayout)findViewById(R.id.subLinear);

    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);

    TextView tv = new TextView(this);
    tv.setId(1);
    tv.setTextSize(15);
    tv.setText("test adding");
    tv.setLayoutParams(lp);
    ll.addView(tv);

if you add new View(anything) from any button click event then add this line

ll.invalidate();

it will refresh it your component

Pratik
  • 30,639
  • 18
  • 84
  • 159
  • Thanks for the followup, the problem ended up being this line in my code: **LinearLayout ll = new LinearLayout(this);** After removing that line, everything worked well. Thanks for testing and confirming my code, and the tip on the ll.invalidate(); Cheers. – Telegard Aug 02 '11 at 08:08
  • 1
    In follow up that wasn't the issue (seemed strange which is why I checked further), I tested variations on this, but it seems to be related to using **R.dimen.text_size_medium** instead of a straight numeric value. In the dimens file the value is stated as 18sp, which doesn't translate over as a proper parameter to the setTextSize method... entering a straight value works. Going to have to research this more, I would rather pull values from my xml resources as opposed to hardcoding values... – Telegard Aug 02 '11 at 08:24
  • This seemed to work: tv.setTextSize(this.getApplicationContext().getResources().getDimensionPixelSize(R.dimen.text_size_medium)); Cheers – Telegard Aug 02 '11 at 08:28
  • Or more simply... **tv.setTextSize(getResources().getDimensionPixelSize(‌​R.dimen.text_size_medium));** also works... – Telegard Aug 02 '11 at 08:32