Search code examples
androidlayoutandroid-linearlayout

adding view to linear layout not visible


I have a problem adding programmatically buttons to linearLayout, the first button gets added but the others aren't visible.

nextButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            //show or load next

                Button curButton = new Button(myClass.this);
                curButton.setText("" + text);
                curButton.setBackgroundResource(R.drawable.tran_mini);

                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
                        LayoutParams.WRAP_CONTENT);
                curButton.setLayoutParams(params);

                curButton.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View arg0)
                    {
                        //do smg
                    }
                });

                paginglibrary.addView(curButton);
       }
 });

this is the xml file where the buttons are being added:

 <HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="150dp"
        android:layout_height="match_parent"
        android:gravity="center"
        android:layout_centerHorizontal="true"
        android:layout_centerInParent="true">

        <LinearLayout
            android:id="@+id/pagingView1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:layout_centerHorizontal="true"
            android:layout_centerInParent="true"
            android:orientation="horizontal">

        </LinearLayout>
    </HorizontalScrollView>

the first time I click on nextButton the button gets added and is show but the second time I add another button, it doesn't show. I am sure they are getting added as I checked through the following code:

 ViewGroup layout = (ViewGroup)findViewById(R.id.pagingView1);
 System.out.println("layout count: "+ layout.getChildCount());

the count shows that the button is getting added but it's not showing. Any idea what could be the cause?

I tried setting the abscissa of the button to make sure it's not being outside of the view but still the same scenario occurs.


Solution

  • <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <Button
            android:id="@+id/btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="new button"/>
    
        <HorizontalScrollView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center"
            android:layout_centerHorizontal="true"
            android:layout_centerInParent="true">
    
            <LinearLayout
                android:id="@+id/pagingView1"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:gravity="center"
                android:orientation="horizontal">
    
            </LinearLayout>
    
        </HorizontalScrollView>
    
    </RelativeLayout>
    

    The only difference is on the HorizontalScrollView width.

    btn.setOnClickListener(new View.OnClickListener() {
    
                @Override
                public void onClick(View arg0) {
                    //show or load next
    
                    Button curButton = new Button(MainActivity.this);
                    curButton.setText("generated button");
    
                    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                            LinearLayout.LayoutParams.WRAP_CONTENT);
                    curButton.setLayoutParams(params);
    
                    curButton.setOnClickListener(new View.OnClickListener() {
    
                        @Override
                        public void onClick(View arg0)
                        {
                            //do smg
                        }
                    });
    
                    ll.addView(curButton);
                }
            });