Search code examples
androidandroid-linearlayoutandroid-scrollview

Linear Layout add scrollView in programmatically


enter image description here

everything ok but I could not add Scrollview because I'm getting an error in scrollView.addView(mainlinearLayout);

There are similar questions but i didnt find the answer. I would be glad if you help me. Thanks

This is error

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

This is activity_main.xml's Code

  <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/LinLay"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

    </LinearLayout>

This is MainActivity's Code

public void drawer() {
        String[] word=s.split(" ");
        scrollView=new ScrollView(this);
        mainlinearLayout = (LinearLayout) findViewById(R.id.LinLay);
        mainlinearLayout.setVerticalScrollBarEnabled(true);
        scrollView.addView(mainlinearLayout);
        childLinearLayout = getLinearLayout();


        for (int i = 1; i < word.length; i++) {
            final TextView textView = new TextView(this);
            textView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            textView.setTextSize(17);
            textView.setPadding(5, 5, 5, 5);
            textView.setText(word[i-1].toString());
            childLinearLayout.addView(textView);

            if (i % separatorNum == 0 && i != 0) {
                mainlinearLayout.addView(childLinearLayout, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
                setContentView(mainlinearLayout);
                childLinearLayout=getLinearLayout();
            }

            textView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Toast.makeText(getApplicationContext(), textView.getText().toString(), Toast.LENGTH_LONG).show();
                }
            });
        }
     }

Solution

  • You can try this, and try to set setContentView(R.layout.activity_main) before calling this.

    The problem is your LinearLayout already attached to activity view same Layout you are trying to add to scrollview so it was giving error.

    public void drawer() {
        //String s = "sdfsdfsdf sdfsdfsd sdfsdf sdfs sdf sdf sdf sdfdsfsdf sfsdf ssdfdsf sdfsd";
        int separatorNum = 5;
        String[] word = s.split(" ");
        scrollView = new ScrollView(this);
        mainlinearLayout = (LinearLayout) findViewById(R.id.LinLay);
        mainlinearLayout.setVerticalScrollBarEnabled(true);
    
        mainlinearLayout.addView(scrollView);
    
        LinearLayout linearLayout = new LinearLayout(this);
        linearLayout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        linearLayout.setOrientation(LinearLayout.VERTICAL);
        scrollView.addView(linearLayout);
    
        LinearLayout childLinearLayout = getLinearLayout();
    
    
        for (int i = 1; i < word.length; i++) {
            final TextView textView = new TextView(this);
            textView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            textView.setTextSize(17);
            textView.setPadding(5, 5, 5, 5);
            textView.setText(word[i - 1].toString());
            childLinearLayout.addView(textView);
    
            if (i % separatorNum == 0 && i != 0) {
                linearLayout.addView(childLinearLayout, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
               // setContentView(mainlinearLayout);
                childLinearLayout = getLinearLayout();
            }
    
            textView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Toast.makeText(getApplicationContext(), textView.getText().toString(), Toast.LENGTH_LONG).show();
                }
            });
        }
    
    }
    
    private LinearLayout getLinearLayout() {
        LinearLayout childLinearLayout = new LinearLayout(this);
        childLinearLayout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        childLinearLayout.setOrientation(LinearLayout.HORIZONTAL);
    
        return childLinearLayout;
    }