usr-local-ΕΨΗΕΛΩΝ
usr-local-ΕΨΗΕΛΩΝ

Reputation: 26874

Adding TextViews at the end of an AsyncTask

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

Upvotes: 0

Views: 473

Answers (2)

Pavandroid
Pavandroid

Reputation: 1586

Use the below code.

   <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textMultiLine" >

        <requestFocus />
    </EditText>

add all the Strings to this EditText by keeping '\n' between the strings. So that every thing will be aligned properly.

Upvotes: 0

kabuko
kabuko

Reputation: 36302

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.

Upvotes: 1

Related Questions