0

I have got a requirement in which i have to autoScroll my tableView.. that is, the rows are getting added dynamically and I have to enable the autoScroll feature as the rows are getting added..

Help me out. Thanks in advance.

LOGCAT :

10-05 11:26:39.474: E/AndroidRuntime(373): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. 
10-05 11:26:39.474: E/AndroidRuntime(373): at android.view.ViewGroup.addViewInner(ViewGroup.java:1970) 
10-05 11:26:39.474: E/AndroidRuntime(373): at android.view.ViewGroup.addView(ViewGroup.java:1865) 
10-05 11:26:39.474: E/AndroidRuntime(373): at android.widget.ScrollView.addView(ScrollView.java:231) 
10-05 11:26:39.474: E/AndroidRuntime(373): at android.view.ViewGroup.addView(ViewGroup.java:1822) 
10-05 11:26:39.474: E/AndroidRuntime(373): at android.widget.ScrollView.addView(ScrollView.java:213) 
10-05 11:26:39.474: E/AndroidRuntime(373): at android.view.ViewGroup.addView(ViewGroup.java:1802) 
10-05 11:26:39.474: E/AndroidRuntime(373): at android.widget.ScrollView.addView(ScrollView.java:204) 
10-05 11:26:39.474: E/AndroidRuntime(373): at com.example.animation_linear.Animation$LongOperation$1.run(Animation.java:89) 
10-05 11:26:39.474: E/AndroidRuntime(373): at android.os.Handler.handleCallback(Handler.java:587) 
10-05 11:26:39.474: E/AndroidRuntime(373): at android.os.Handler.dispatchMessage(Handler.java:92) 
10-05 11:26:39.474: E/AndroidRuntime(373): at android.os.Looper.loop(Looper.java:123) 
10-05 11:26:39.474: E/AndroidRuntime(373): at android.app.ActivityThread.main(ActivityThread.java:4627)
10-05 11:26:39.474: E/AndroidRuntime(373): at java.lang.reflect.Method.invokeNative(Native Method) 
10-05 11:26:39.474: E/AndroidRuntime(373): at java.lang.reflect.Method.invoke(Method.java:521)
10-05 11:26:39.474: E/AndroidRuntime(373): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)

I think the problem is that am initiating it inside asyncTask like this as my requirement is to animate image after image

protected Void doInBackground(Void... params) {

            runOnUiThread(new Runnable() {
                @Override
                public void run() {             
                    int a =0;
                    for (int row = 0; row < rows; row++) {

                        tableRow = new TableRow(getApplicationContext());
                        for (int col = 0; col < layout; col++) {

                            image = new ImageView(getApplicationContext());
                            iv[a]   =   image;
                            image.setPadding(20, 20, 20, 20);
                            android.view.animation.Animation animation =    animate(a);
                            image.setAnimation(animation);
                            image.setImageResource(R.drawable.images);

                            tableRow.addView(image);

                            a++;

                            VSC.post(new Runnable() {
                                @Override
                                public void run() {
                                   VSC.fullScroll(tableLayout.FOCUS_DOWN);
                                }

                            });
                        }
                        tableLayout.addView(tableRow);
                    }
                    //VSC.addView(tableLayout);
                    HSC.addView(VSC);
                    setContentView(HSC);





                }
            });

            return null;
        }
        @Override
        protected void onPostExecute(Void result) {  
            super.onPostExecute(result);
        }
    }

See the link java.lang.IllegalStateException when adding a View in debug mode. How to debug the issue? Please help me

Community
  • 1
  • 1
Priety
  • 308
  • 1
  • 4
  • 19
  • do you also want it to scroll automatically to the end of the table when a row is added? – Buffalo Oct 04 '12 at 11:56
  • Yeah... On the view only 6 rows can appear at a time in the emulator. When the 7th row is added the tableView has to autoScroll to down to automaticaaly show up the added row. – Priety Oct 04 '12 at 11:57
  • @Shruti: None of the answers helped yaar. :( I was not able to do that. – Priety Oct 22 '12 at 13:43

4 Answers4

4


In android tableView called as TableLayout here is the tutorial, will show you how to add rows to TableLayout dynamically.
For scrolling feature you can always use ScrollView mentioned in older answers.
Good Luck!

Pratik
  • 831
  • 2
  • 8
  • 13
  • Thanks for the reply. But the rows are getting added dynamically and even the ScrollViews are added. Its not the problem. But my requirement is when a new row adds up the ScrollBar should auto scroll in order to show up the newly added row. How can I do this? Can you help me!! – Priety Oct 04 '12 at 12:01
  • 2
    http://stackoverflow.com/questions/3087877/scroll-to-last-line-of-tablelayout-within-a-scrollview – Pratik Oct 04 '12 at 12:11
1

try like this in your xml

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

    <TableLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/tLayout"
        android:scrollbars="vertical"
        >   
</TableLayout>
</Scrollview>

In your .java class, use id of TableLayout to add rows dynamically, When number of rows will exceed the screen height, scrollbar will be shown.

OR

Simply add :

android:isScrollContainer="true"

as shown below in your TableLayout

<TableLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/tLayout"
            android:isScrollContainer="true"
            >   
    </TableLayout>

EDIT

As told by you

the rows are getting added dynamically

for that you can use

TableLayout tl=(TableLayout)findViewById(R.id.tLayout);    
TableRow tr1 = new TableRow(this);
//to add row on table ,you can use loops to add multiple rows on table
tl.addView(tr1);

refer this urls

And to remove exception try this

if(tl !=null)
tl.removeAllViews();

if you don't want to remove all views and add new views everytime

you can add row using index like this

tl.addView(child, index); //where child is row 

see this answer Android TableRow - How to add View dynamically to certain postion?

Community
  • 1
  • 1
Shruti
  • 1
  • 13
  • 55
  • 95
  • When i refer my table layout by the ID, the program is crashing. – Priety Oct 04 '12 at 12:31
  • is your tablelayout not empty ? – Shruti Oct 05 '12 at 06:06
  • I have added a tableRow in it. – Priety Oct 05 '12 at 06:09
  • I have added the rows in the same way. Somewhere I am missing out something. So am getting the exceptiopn. I am trying to debug. Will let you know if it works out :) – Priety Oct 05 '12 at 06:33
  • You are adding some views on row that are added on a row before ,that why you are getting this exception.. got thru these answers http://stackoverflow.com/questions/8132867/android-java-lang-illegalstateexception-the-specified-child-already-has-a-pare ,http://stackoverflow.com/questions/8729573/java-lang-illegalstateexception-the-specified-child-already-has-a-parent-you-m, try giving an Unique ID for the table rows or the views you are adding to the table – Shruti Oct 05 '12 at 07:06
1

Put the Table Layout inside the ScrollView layout as

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TableLayout android:id="@+id/score_table"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"> 
        <TableRow android:id="@+id/header"/>                    
    </TableLayout>
</ScrollView>
Rajeev N B
  • 1,365
  • 2
  • 12
  • 24
  • Thanks for the reply. But I am not creating tableView from Xml. Am creating it through the code itself. So can you tell me how to enable auto scrolling for the dynamically added TableView? – Priety Oct 04 '12 at 11:58
  • Create scroll view dynamically and start adding child view Table Layout to the ScrollView dynamically – Rajeev N B Oct 04 '12 at 12:04
  • @Shruti: Yeah I am trying. Thanks – Priety Oct 04 '12 at 12:09
1

To scroll automatically to the end (assuming your TableView is contained by a ScrollView):

scrollView.post(new Runnable() {            
    @Override
    public void run() {
           scroll.fullScroll(View.FOCUS_DOWN);              
    }
});

Edit: make sure your scrollview only has one child.

Let's say you have this layout:

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tableLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="5dip" >

        <TextView
            android:id="@+id/textView1"
            android:text="Column 1"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <Button
            android:id="@+id/button1"
            android:text="Column 2" />
    </TableRow>

</TableLayout>

And you want to use a scrollView. It should NOT look like this:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/settingsContactInformationScrollView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

        <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/tableLayout1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <TableRow
            android:id="@+id/tableRow1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="5dip" >

            <TextView
                android:id="@+id/textView1"
                android:text="Column 1"
                android:textAppearance="?android:attr/textAppearanceLarge" />

            <Button
                android:id="@+id/button1"
                android:text="Column 2" />
        </TableRow>

    </TableLayout>

    <Button
        android:id="@+id/myButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
    />
</ScrollView>

Instead, you should group everything you want to include in the scrollView into one layout and use that as a child of the scrollView, like so:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/myScrollView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_gravity="center"
        android:gravity="center"
        android:orientation="vertical" >

        <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/tableLayout1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >

            <TableRow
                android:id="@+id/tableRow1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="5dip" >

                <TextView
                    android:id="@+id/textView1"
                    android:text="Column 1"
                    android:textAppearance="?android:attr/textAppearanceLarge" />

                <Button
                    android:id="@+id/button1"
                    android:text="Column 2" />
            </TableRow>

        </TableLayout>

        <Button
            android:id="@+id/myButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
        />

    </LinearLayout>
</ScrollView>
Buffalo
  • 3,861
  • 8
  • 44
  • 69
  • On using the code which you had posted i got the exception10-05 11:44:51.674: E/AndroidRuntime(538): java.lang.IllegalStateException: ScrollView can host only one direct child My code was- VSC.fullScroll(tableLayout.FOCUS_DOWN); where VSC is the ScrollView and tableLayout is my Table.. Can you help me?How to debug it?? – Priety Oct 05 '12 at 06:13
  • The ScrollView can only have one child. Check my updated answer please. – Buffalo Oct 05 '12 at 07:18