Fcps
Fcps

Reputation: 345

How to set multiple views in ScrollView?

I am working with ScrollView and I want to set in my ScrollView some buttons or some textview or edittext.. Here is my class MyScrollView who is a child of ScrollView class :

public  class MyScrollView extends ScrollView {

private JSONParser  jParser;
private JSONObject  contenuScrollView;
private Context     context;

@SuppressLint("NewApi")
public MyScrollView(Context context, JSONArray listScrollView) throws JSONException {
    super(context);

    this.context = context;
}

public void onCreate(Bundle savedInstanceState) {

}

/* Création de TextView */
public void setClassicLabel(JSONArray listLabel, RelativeLayout rl) throws JSONException {
        if (listLabel.length() > 0)
            {
            DisplayMetrics metrics = new DisplayMetrics();
            for (int i = 0; i < listLabel.length(); i++) {
                LinearLayout ll = new LinearLayout(getContext());
                ll.setOrientation(LinearLayout.HORIZONTAL);

                TextView tv = new TextView(context);
                tv.setText(listLabel.getJSONObject(i).getString("text"));
                tv.setTextColor(Color.parseColor(listLabel.getJSONObject(i).getString("colortext")));
                tv.setBackgroundColor(Color.parseColor(listLabel.getJSONObject(i).getString("color")));
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams((int) (getContext().getResources().getDisplayMetrics().widthPixels * listLabel.getJSONObject(i).getDouble("size_x")),
                                                                                (int) (getContext().getResources().getDisplayMetrics().heightPixels * listLabel.getJSONObject(i).getDouble("size_y")));
                params.leftMargin = (int) (getContext().getResources().getDisplayMetrics().widthPixels * listLabel.getJSONObject(i).getDouble("position_x"));
                params.topMargin = (int) (getContext().getResources().getDisplayMetrics().heightPixels * listLabel.getJSONObject(i).getDouble("position_y"));
                ll.addView(tv);
                //rl.addView(ll);
                this.addView(ll);
            }
        }
    }
}

My problem is the next : I can set one Label with my method setClassicLabel and the result is this : enter image description here

When I'm trying to set a second label, I have a message error who said to me this :

03-12 16:41:11.054: E/AndroidRuntime(5355): FATAL EXCEPTION: main
03-12 16:41:11.054: E/AndroidRuntime(5355): Process: com.fchps.bya, PID: 5355
03-12 16:41:11.054: E/AndroidRuntime(5355): java.lang.RuntimeException: Unable to start       activity ComponentInfo{com.fchps.bya/com.fchps.bya.classicview.ClassicView}:   java.lang.IllegalStateException: ScrollView can host only one direct child
03-12 16:41:11.054: E/AndroidRuntime(5355):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
03-12 16:41:11.054: E/AndroidRuntime(5355):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
03-12 16:41:11.054: E/AndroidRuntime(5355):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
03-12 16:41:11.054: E/AndroidRuntime(5355):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
03-12 16:41:11.054: E/AndroidRuntime(5355):     at android.os.Handler.dispatchMessage(Handler.java:102)
03-12 16:41:11.054: E/AndroidRuntime(5355):     at android.os.Looper.loop(Looper.java:136)
03-12 16:41:11.054: E/AndroidRuntime(5355):     at android.app.ActivityThread.main(ActivityThread.java:5017)
03-12 16:41:11.054: E/AndroidRuntime(5355):     at java.lang.reflect.Method.invokeNative(Native Method)
03-12 16:41:11.054: E/AndroidRuntime(5355):     at java.lang.reflect.Method.invoke(Method.java:515)
03-12 16:41:11.054: E/AndroidRuntime(5355):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-12 16:41:11.054: E/AndroidRuntime(5355):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-12 16:41:11.054: E/AndroidRuntime(5355):     at dalvik.system.NativeStart.main(Native Method)
03-12 16:41:11.054: E/AndroidRuntime(5355): Caused by: java.lang.IllegalStateException: ScrollView can host only one direct child
03-12 16:41:11.054: E/AndroidRuntime(5355):     at android.widget.ScrollView.addView(ScrollView.java:237)
03-12 16:41:11.054: E/AndroidRuntime(5355):     at   com.fchps.bya.myScrollView.MyScrollView.setClassicLabel(MyScrollView.java:57)
03-12 16:41:11.054: E/AndroidRuntime(5355):     at com.fchps.bya.classicview.ClassicView.setClassicScrollView(ClassicView.java:136)
03-12 16:41:11.054: E/AndroidRuntime(5355):     at com.fchps.bya.classicview.ClassicView.onCreate(ClassicView.java:106)
03-12 16:41:11.054: E/AndroidRuntime(5355):     at android.app.Activity.performCreate(Activity.java:5231)
03-12 16:41:11.054: E/AndroidRuntime(5355):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-12 16:41:11.054: E/AndroidRuntime(5355):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
03-12 16:41:11.054: E/AndroidRuntime(5355):     ... 11 more

Someone has an idea what i'm making wrong ? Thanks

Upvotes: 1

Views: 438

Answers (2)

JRowan
JRowan

Reputation: 7104

you can only set one view containing all of the content to be scrolled in a scrollview your for loop is creating a linear layout every iteration of the loop and adding it, you can only add 1, so make 1 linear layout then in the for loop add everything else to the linearlayout

something like this

public void setClassicLabel(JSONArray listLabel, RelativeLayout rl) throws JSONException {
        if (listLabel.length() > 0)
            {
            DisplayMetrics metrics = new DisplayMetrics();
            LinearLayout ll = new LinearLayout(getContext());
            ll.setOrientation(LinearLayout.HORIZONTAL);
            for (int i = 0; i < listLabel.length(); i++) {



                TextView tv = new TextView(context);
                tv.setText(listLabel.getJSONObject(i).getString("text"));
                tv.setTextColor(Color.parseColor(listLabel.getJSONObject(i).getString("colortext")));
                tv.setBackgroundColor(Color.parseColor(listLabel.getJSONObject(i).getString("color")));
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams((int) (getContext().getResources().getDisplayMetrics().widthPixels * listLabel.getJSONObject(i).getDouble("size_x")),
                                                                                (int) (getContext().getResources().getDisplayMetrics().heightPixels * listLabel.getJSONObject(i).getDouble("size_y")));
                params.leftMargin = (int) (getContext().getResources().getDisplayMetrics().widthPixels * listLabel.getJSONObject(i).getDouble("position_x"));
                params.topMargin = (int) (getContext().getResources().getDisplayMetrics().heightPixels * listLabel.getJSONObject(i).getDouble("position_y"));
                ll.addView(tv);
                //rl.addView(ll);

            }
this.addView(ll);
        }
    }
}

so you add the linearlayout only once and the linear layout has all the textviews in it

Upvotes: 1

nikis
nikis

Reputation: 11234

By calling this.addView(ll) you add view to the ScrollView host. But ScrollView has a limitation - it can have only one child (it's clearly said in LogCat), so second call of setClassicLabel will fail (you are trying to add one more child). What's the solution? Add a container mContainer (of type LinearLayout, RelativeLayout, whatever) to the ScrollView during it's initialization and then add views to this container mContainer.addView(ll).

Upvotes: 0

Related Questions