Search code examples
androidscrollviewandroid-scrollview

Scrollable Textview inside AlertDialog [Android]


I want to have a scrollable textview inside an Alertdialog. This is my xml for the scrollview which I inflate inside the alertDialog. I keep getting this error "IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first."

Could it be that there is something wrong with my layout? Because I'm only using the layout once.

<?xml version="1.0" encoding="utf-8"?>

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:id="@+id/invalid_recipients"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dip"
        android:layout_marginRight="16dip"
        android:layout_marginTop="4dip"
        android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>

</ScrollView>

EDIT: I am inflating the dialog and accessing the textview in an AsyncTask in the onPostExecute method. Here is the first bit of that method.

        @Override
        protected void onPostExecute(Void v) {
            if (!invalidRecipientEmails.isEmpty()) {
                AlertDialog.Builder certBuilder = new AlertDialog.Builder(
                        MessageCompose.this);
                final View recipientsLayout = getLayoutInflater().inflate(R.layout.message_recipient_scrollview, null);
                final TextView recipientsTextView = (TextView) recipientsLayout.findViewById(R.id.invalid_recipients);
                recipientsTextView.setText(invalidRecipientsString);
                certBuilder.setView(recipientsTextView);
                // set rest of alertdialog attributes
            }
         }

Solution

  • With xml no problem. Show your java code where you try use.

    UPDATE: you must set

    certBuilder.setView(recipientsLayout); 
    

    instead of

    certBuilder.setView(recipientsTextView);