How to add scrollview to linearlayout programtically.

Put your problem here if it does not fit any of the other categories.

How to add scrollview to linearlayout programtically.

Postby Archana » Tue Aug 11, 2009 12:29 pm

Hi,

I am trying to add scrollview to linearlayout but it showing error like
"ScrollView can have only one direct child".. I tried like this
Scrollview scrollview = new ScrollView(context)

linearlayout.removeAllViews();
linearlayout.addview(listview)
scrollView.addView(linearlayout);

Can anybody point out where i am doing mistake.
Archana
Archana
Experienced Developer
Experienced Developer
 
Posts: 53
Joined: Thu Feb 28, 2008 12:00 pm
Location: Bangalore

Top

Postby arams » Wed Aug 12, 2009 6:16 am

hi try this...

class app extends Scrollview{
Linearlayout...
..
.
}


you should keep this in mind...
ScrollView s;
s.add(linearlayout);
s.add(someotherview); if you write like this means it will show error :SCROLLVIEW HAVE DIRECTCHILD

write like this :
s.add(linearlayout);
linearlayout.add(remaining views);

if this dont help for you then,
post Your Complete Code....i will try on that...
arams
Experienced Developer
Experienced Developer
 
Posts: 89
Joined: Fri May 08, 2009 7:45 am

Postby Archana » Wed Aug 12, 2009 6:50 am

Hi,
I tried as you said but it showing this error now

08-12 11:13:46.907: ERROR/AndroidRuntime(1628): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
I extented class with ScrollView instead of linearlayout


listview.removeAllView();
listView.setListAdapter(adapter);
linearlayout.removeAllViews();
linearlayout.addView(listView);
scrollview.addView(linearlayout);
Archana
Archana
Experienced Developer
Experienced Developer
 
Posts: 53
Joined: Thu Feb 28, 2008 12:00 pm
Location: Bangalore

Postby arams » Wed Aug 12, 2009 9:59 am

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.  
  3.  
  4. import android.content.Context;
  5.  
  6. import android.widget.Button;
  7.  
  8. import android.widget.LinearLayout;
  9.  
  10. import android.widget.ScrollView;
  11.  
  12. public class TestScroll extends ScrollView {
  13.  
  14.  
  15.  
  16.         public TestScroll(Context context) {
  17.  
  18.                 super(context);
  19.  
  20.                 LinearLayout l=new LinearLayout(context);
  21.  
  22.                 Button b=new Button(context);
  23.  
  24.                 b.setText("Works");
  25.  
  26.                 l.addView(b);
  27.  
  28.                 this.addView(l);
  29.  
  30.                 // TODO Auto-generated constructor stub
  31.  
  32.         }
  33.  
  34.  
  35.  
  36. }
  37.  
  38.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


try this
arams
Experienced Developer
Experienced Developer
 
Posts: 89
Joined: Fri May 08, 2009 7:45 am

Postby IHeartAndroid » Mon Nov 16, 2009 11:03 am

Hi,
This does not work:


Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2. <?xml version="1.0" encoding="utf-8"?>
  3.  
  4. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  5.  
  6. android:id="@+id/ll_root"
  7.  
  8.         android:orientation="vertical" android:layout_width="fill_parent"
  9.  
  10.         android:layout_height="fill_parent">
  11.  
  12.  
  13.  
  14.  
  15.  
  16.       <!-- .. something else .. -->
  17.  
  18.  
  19.  
  20.         <my.package.MyScrollView
  21.  
  22.                 android:layout_width="fill_parent"
  23.  
  24.          android:layout_height="wrap_content"
  25.  
  26.          >
  27.  
  28.         </my.package.MyScrollView>
  29.  
  30.  
  31.  
  32. </LinearLayout>
  33.  
  34.  
Parsed in 0.002 seconds, using GeSHi 1.0.8.4



Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. package my.package;
  3.  
  4.  
  5.  
  6. import android.content.Context;
  7.  
  8. import android.util.AttributeSet;
  9.  
  10. import android.widget.Button;
  11.  
  12. import android.widget.LinearLayout;
  13.  
  14. import android.widget.ScrollView;
  15.  
  16.  
  17.  
  18. public class MyScrollView extends ScrollView {
  19.  
  20.  
  21.  
  22.        
  23.  
  24. public MyScrollView(Context context) {
  25.  
  26.         super(context);
  27.  
  28.         removeAllViews();
  29.  
  30.         LinearLayout l=new LinearLayout(context);
  31.  
  32.         Button b=new Button(context);
  33.  
  34.         b.setText("Works");
  35.  
  36.         l.addView(b);
  37.  
  38.         this.addView(l);
  39.  
  40.        
  41.  
  42.    }
  43.  
  44.        
  45.  
  46.        
  47.  
  48.      public MyScrollView(Context context, AttributeSet a) {
  49.  
  50.           super(context,a);
  51.  
  52.           removeAllViews();
  53.  
  54.           LinearLayout l=new LinearLayout(context);
  55.  
  56.           Button b=new Button(context);
  57.  
  58.           b.setText("Works");
  59.  
  60.           l.addView(b);
  61.  
  62.           l.addView(b);l.addView(b);l.addView(b);l.addView(b);l.addView(b);l.addView(b);l.addView(b);l.addView(b);l.addView(b);l.addView(b);l.addView(b);
  63.  
  64.           this.addView(l);
  65.  
  66.          
  67.  
  68.      }
  69.  
  70. }
  71.  
  72.  
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.


I dont understand this. I add more content to the LinearLayout not to the ScrollView.... ?
And I also call removeAllViews().
User avatar
IHeartAndroid
Junior Developer
Junior Developer
 
Posts: 24
Joined: Wed Jul 08, 2009 8:51 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 15 guests