0

i found this solution to the new line problem, but i cant seem to get it to work, i have been checking every comment and i cant seem to find anyone who has the same problem as me. Android - multi-line linear layout I have been trying to find similar threads on StackOverFlow, but cant find any.

My code:

public void setUpListToLinearLayout(LinearLayout l1, ArrayList collection, String header){

    Display display = getWindowManager().getDefaultDisplay();
    int maxWidth = display.getWidth() - 10;

    if (collection.size()>0){
        LinearLayout l1Also = new LinearLayout(this);
        l1Also.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
        l1Also.setOrientation(LinearLayout.HORIZONTAL);

        TextView txtSample = new TextView(this);
        txtSample.setText(header);

        l1Also.addView(txtSample);
        txtSample.measure(0, 0);

        int widthSoFar = txtSample.getMeasuredWidth();

        for (int i = 0; i < collection.size(); i ++){

            TextView txtSamItem = new TextView(this);
            txtSamItem.setText(collection.get(i).toString());
            txtSamItem.setPadding(10, 0, 10, 0);
            txtSamItem.setTag(collection.get(i));
            txtSamItem.measure(0, 0);
            widthSoFar += txtSamItem.getMeasuredWidth();

            if (widthSoFar>=maxWidth){
                l1.addView(l1Also);
                l1Also.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
                l1Also.setOrientation(LinearLayout.HORIZONTAL);

                l1Also.addView(txtSamItem);
                widthSoFar = txtSamItem.getMeasuredWidth();


            } else {
                l1Also.addView(txtSamItem);
            }

        }
        l1.addView(l1Also);

    }
}

Im getting this in the logcat:

E/AndroidRuntime(10872): FATAL EXCEPTION: main
E/AndroidRuntime(10872): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.xiloandroid/activities.DetailedOpenCasesActivity}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
E/AndroidRuntime(10872):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2265)
E/AndroidRuntime(10872):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2315)
E/AndroidRuntime(10872):    at android.app.ActivityThread.access$600(ActivityThread.java:149)
E/AndroidRuntime(10872):    at     android.app.ActivityThread$H.handleMessage(ActivityThread.java:1297)
E/AndroidRuntime(10872):    at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(10872):    at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(10872):    at android.app.ActivityThread.main(ActivityThread.java:5235)
E/AndroidRuntime(10872):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(10872):    at java.lang.reflect.Method.invoke(Method.java:525)             E/AndroidRuntime(10872):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:742)
E/AndroidRuntime(10872):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
E/AndroidRuntime(10872):    at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(10872): Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
E/AndroidRuntime(10872):    at android.view.ViewGroup.addViewInner(ViewGroup.java:3509)
E/AndroidRuntime(10872):    at android.view.ViewGroup.addView(ViewGroup.java:3380)
E/AndroidRuntime(10872):    at android.view.ViewGroup.addView(ViewGroup.java:3325)
E/AndroidRuntime(10872):    at android.view.ViewGroup.addView(ViewGroup.java:3301)
E/AndroidRuntime(10872):    at activities.DetailedOpenCasesActivity.setUpListToLinearLayout(DetailedOpenCasesActivity.java:287)
E/AndroidRuntime(10872):    at activities.DetailedOpenCasesActivity.update(DetailedOpenCasesActivity.java:242)
E/AndroidRuntime(10872):    at activities.DetailedOpenCasesActivity.onCreate(DetailedOpenCasesActivity.java:98)
E/AndroidRuntime(10872):    at android.app.Activity.performCreate(Activity.java:5133)
E/AndroidRuntime(10872):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
E/AndroidRuntime(10872):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2229)

i think it might be because of this line:

l1.addView(l1Also);

i have been stuck in this problem for half a day, i would really appreciate some help.

Community
  • 1
  • 1

2 Answers2

0

You have called l1.addView(l1Also); twice. in if() once and end once. So remove this and add once again.

So call this at end.

l1.removeView(l1Also);

and then

l1.addView(l1Also);
TechArcSri
  • 1,982
  • 1
  • 13
  • 20
0

You shouldn't add the same view to parent layout again and again in a loop. that causes the error:

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

Instead of this you should take a new linear layout l2Also in a loop, and add that in main layout.:

See this snippet:

for (int i = 0; i < collection.size(); i ++){

    LinearLayout l2Also = new LinearLayout(this);
    l2Also.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
    l2Also.setOrientation(LinearLayout.HORIZONTAL);
    TextView txtSamItem = new TextView(this);
    txtSamItem.setText(collection.get(i).toString());
    txtSamItem.setPadding(10, 0, 10, 0);
    txtSamItem.setTag(collection.get(i));
    txtSamItem.measure(0, 0);
    widthSoFar += txtSamItem.getMeasuredWidth();

    if (widthSoFar>=maxWidth){

        l2Also.addView(txtSamItem);  //in a new linear layout, your text will be saved.
        widthSoFar = txtSamItem.getMeasuredWidth();
    } else {
        l2Also.addView(txtSamItem);
    }
    l1Also.addView(l2Also);  // that new layout will be added in l1Also at each iteration
}
l1.addView(l1Also);  // finally add l1Also in parent layout.

P.S: You will need to change orientation of layout l1Also to VERTICAL for adding new layout l2Also in a new line.

Hope this helps.

MysticMagicϡ
  • 28,593
  • 16
  • 73
  • 124