Unable to add more than one Layout within a layout

618 Views Asked by At

I am creating an application in android which supports Horizontal and vertical scrolling. I have placed a ViewGroup as base. I have loaded a Vertical Scroll Bar (extended from FrameLayout) inside the view group. The vertical scroll bar contains a Horizontal scroll bar(derived from FrameLayout) as its child. My idea is to add a LinearLayout within the HorizontalScrollbar so that I can add more than one child linearly. However I could not add more than one LinearLayout within the Layout that I have added inside the HorizontalScrollBar.  loading one LinearLayout within the Layout present inside the Horizontal scroll Bar.

Error when Loading more than one LinearLayout within the layout present inside the Horizontal Scroll Bar

I tried this way. This is my code:

     private VScrollViewer Vscroll;
    //VScrollViewer is a class extended from android.widget.ScrollView

    public AndroidGrid(Context paramContext)
    {
        super(paramContext);
        init();
    }

    public void init()
    {
        Vscroll = new VScrollViewer(getContext());
        LinearLayout lr=new LinearLayout(getContext());
        LinearLayout lr1=new LinearLayout(getContext());
        LinearLayout lr2=new LinearLayout(getContext());
        lr.setOrientation(LinearLayout.VERTICAL);
        lr1.setOrientation(LinearLayout.VERTICAL);
        lr2.setOrientation(LinearLayout.VERTICAL);
        lr.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
        LinearLayout hlr= new LinearLayout(getContext());
        hlr.setOrientation(LinearLayout.HORIZONTAL);
        for(int i = 20; i < 550; i+=20){
            TextView myText = new TextView(getContext());
            myText.setY(i*3);
            myText.setText("HELLLLLOOOO");
            lr.addView(myText);
            lr1.addView(myText);
            lr2.addView(myText);
        }
        hlr.addView(lr);
        hlr.addView(lr1);
        hlr.addView(lr2);
        Vscroll.container.addView(hlr);
        addView(Vscroll);
    }

Error Log:

11-12 10:04:57.031: E/AndroidRuntime(18425): FATAL EXCEPTION: main
11-12 10:04:57.031: E/AndroidRuntime(18425): Process: com.example.baseexample, PID: 18425
11-12 10:04:57.031: E/AndroidRuntime(18425): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.baseexample/com.example.baseexample.MainActivity}: android.view.InflateException: Binary XML file line #12: Error inflating class com.example.baseexample.AndroidGrid
11-12 10:04:57.031: E/AndroidRuntime(18425):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2198)
11-12 10:04:57.031: E/AndroidRuntime(18425):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257)
11-12 10:04:57.031: E/AndroidRuntime(18425):    at android.app.ActivityThread.access$800(ActivityThread.java:139)
11-12 10:04:57.031: E/AndroidRuntime(18425):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
11-12 10:04:57.031: E/AndroidRuntime(18425):    at android.os.Handler.dispatchMessage(Handler.java:102)
11-12 10:04:57.031: E/AndroidRuntime(18425):    at android.os.Looper.loop(Looper.java:136)
11-12 10:04:57.031: E/AndroidRuntime(18425):    at android.app.ActivityThread.main(ActivityThread.java:5086)
11-12 10:04:57.031: E/AndroidRuntime(18425):    at java.lang.reflect.Method.invokeNative(Native Method)
11-12 10:04:57.031: E/AndroidRuntime(18425):    at java.lang.reflect.Method.invoke(Method.java:515)
11-12 10:04:57.031: E/AndroidRuntime(18425):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
11-12 10:04:57.031: E/AndroidRuntime(18425):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
11-12 10:04:57.031: E/AndroidRuntime(18425):    at dalvik.system.NativeStart.main(Native Method)
11-12 10:04:57.031: E/AndroidRuntime(18425): Caused by: android.view.InflateException: Binary XML file line #12: Error inflating class com.example.baseexample.AndroidGrid
11-12 10:04:57.031: E/AndroidRuntime(18425):    at android.view.LayoutInflater.createView(LayoutInflater.java:620)
11-12 10:04:57.031: E/AndroidRuntime(18425):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
11-12 10:04:57.031: E/AndroidRuntime(18425):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
11-12 10:04:57.031: E/AndroidRuntime(18425):    at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
11-12 10:04:57.031: E/AndroidRuntime(18425):    at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
11-12 10:04:57.031: E/AndroidRuntime(18425):    at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
11-12 10:04:57.031: E/AndroidRuntime(18425):    at android.support.v7.app.ActionBarActivityDelegateBase.setContentView(ActionBarActivityDelegateBase.java:228)
11-12 10:04:57.031: E/AndroidRuntime(18425):    at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:102)
11-12 10:04:57.031: E/AndroidRuntime(18425):    at com.example.baseexample.MainActivity.onCreate(MainActivity.java:14)
11-12 10:04:57.031: E/AndroidRuntime(18425):    at android.app.Activity.performCreate(Activity.java:5248)
11-12 10:04:57.031: E/AndroidRuntime(18425):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110)
11-12 10:04:57.031: E/AndroidRuntime(18425):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2162)
11-12 10:04:57.031: E/AndroidRuntime(18425):    ... 11 more
11-12 10:04:57.031: E/AndroidRuntime(18425): Caused by: java.lang.reflect.InvocationTargetException
11-12 10:04:57.031: E/AndroidRuntime(18425):    at java.lang.reflect.Constructor.constructNative(Native Method)
11-12 10:04:57.031: E/AndroidRuntime(18425):    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
11-12 10:04:57.031: E/AndroidRuntime(18425):    at android.view.LayoutInflater.createView(LayoutInflater.java:594)
11-12 10:04:57.031: E/AndroidRuntime(18425):    ... 22 more
11-12 10:04:57.031: E/AndroidRuntime(18425): Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
11-12 10:04:57.031: E/AndroidRuntime(18425):    at android.view.ViewGroup.addViewInner(ViewGroup.java:3564)
11-12 10:04:57.031: E/AndroidRuntime(18425):    at android.view.ViewGroup.addView(ViewGroup.java:3417)
11-12 10:04:57.031: E/AndroidRuntime(18425):    at android.view.ViewGroup.addView(ViewGroup.java:3362)
11-12 10:04:57.031: E/AndroidRuntime(18425):    at android.view.ViewGroup.addView(ViewGroup.java:3338)
11-12 10:04:57.031: E/AndroidRuntime(18425):    at com.example.baseexample.AndroidGrid.init(AndroidGrid.java:55)
11-12 10:04:57.031: E/AndroidRuntime(18425):    at com.example.baseexample.AndroidGrid.<init>(AndroidGrid.java:28)
11-12 10:04:57.031: E/AndroidRuntime(18425):    ... 25 more

Can anyone suggest me way to add the LinearLayout lr1 and lr2 inside the LinearLayout hlr?

2

There are 2 best solutions below

0
On

The problem is that you can't have multiple children inside a scrollview. If you want to acheive the same thing, then just create two ScrollViews in View or two LinearLayouts each containing a ScrollView.

Also you have a Scrollview inside of your HorizontalScrollView. You needn't do that. If you need a HorizontalScrollView then remove the child ScrollView, it will have the same affect.

Hope this helps. Happy coding :)

P.S. If this answer helps please mark it as the correct answer.

1
On

Logcat says:

11-12 10:04:57.031: E/AndroidRuntime(18425): Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

Issue is in this:

for(int i = 20; i < 550; i+=20){
    TextView myText = new TextView(getContext());
    myText.setY(i*3);
    myText.setText("HELLLLLOOOO");
    lr.addView(myText);
    lr1.addView(myText);
    lr2.addView(myText);
}

You are trying to add a same TextView to three layouts. You should create a new TextView for each layout as shown below:

for(int i = 20; i < 550; i+=20){
    TextView myText1 = new TextView(getContext());
    TextView myText2 = new TextView(getContext());
    TextView myText3 = new TextView(getContext());

    myText1.setText("HELLLLLOOOO text 1");
    myText1.setText("HELLLLLOOOO text 2");
    myText1.setText("HELLLLLOOOO text 3");
    lr.addView(myText1);
    lr1.addView(myText2);
    lr2.addView(myText3);
}

Hope this helps.