I need to add text to a ScrollableLayout after it’s remotely retrieved from an AsyncTask. Since I don’t know the number of strings involved, I need to programmatically create as many TextViews as needed.
Layout code
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/view_phone_mainView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="@+id/view_phone_linearLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="top" >
<TextView
android:id="@+id/view_phone_lblTitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal|center_vertical"
android:text="@string/view_phone_title"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/view_phone_lblWarning"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/view_phone_warning"
android:textAppearance="?android:attr/textAppearanceSmall"
android:visibility="invisible" />
</LinearLayout>
</ScrollView>
Activity
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
pDialog = new ProgressDialog(this);
pDialog.setIndeterminate(true);
pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
pDialog.setMessage(getString(R.string.view_phone_wait));
pDialog.show();
task = new MessageLoaderTask(); //Returns String[] on callback
task.execute((Void) null);
}
public void onRulesLoaded(String[] messages) { //Directly called by OnPostExecute
LinearLayout container = (LinearLayout) findViewById(R.id.view_phone_linearLayout);
if (messages != null) {
for (String m : messages) {
TextView tv = new TextView(this);
tv.setText(m);
tv.setTextAppearance(this, android.R.attr.textAppearanceSmall);
tv.setVisibility(View.VISIBLE);
tv.setBackgroundColor(Color.TRANSPARENT);
tv.setTextColor(Color.BLACK);
container.addView(tv, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
}
}
pDialog.dismiss();
}
I am sure, by debug, that strings are correctly valued. The result is that the title label gets displayed, the warning is hidden but below there is only white……
I tried to scroll and noticed that the scrolling area is veeeeeeeeeery long, compatible with the long Lorem ipsum stub text I used for testing. If I truncate via debug one of the strings to empty (there are only 2) the scrollable area is shorter in height. I use the light theme, so I expect black text on white background.
tv.setTextAppearance(this, android.R.attr.textAppearanceSmall);
tv.setVisibility(View.VISIBLE);
tv.setBackgroundColor(Color.TRANSPARENT);
tv.setTextColor(Color.BLACK);
were added at a second time when everything was failing. No difference whether they are in place or not. What can I do to fix?
Thanks
Not sure why your ScrollView would be increasing in length if this were the sole problem, but it seems like you need to set your LinearLayout’s orientation to vertical.