0

I'm having a difficulty adding buttons dynamically to a ScrollView. The code below is adding the buttons BUT there is no scroller. If I'm putting the buttons directly in the XML (not dynamically) it's working and I can scroll down/up.

My view:

<ScrollView android:id="@+id/ScrollView01"
android:layout_width="264dp"
android:layout_height="match_parent"
android:fillViewport="true"
>

    <LinearLayout
        android:id="@+id/buttons"
        android:layout_width="264dp"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:scrollbars="vertical"
         >         

     ** HERE THE BUTTONS SHOULD BE ADDED DYNAMICALLY **     

    </LinearLayout>

</ScrollView>

The code which adding buttons:

    // create new button
    final Button newbutton = new Button(this);

    // set background color
    newbutton.setBackgroundColor(Color.GRAY);

    // set width and height
    newbutton.setWidth(50);
    newbutton.setHeight(20);

    // set position
    newbutton.setY(((float)numOfButton*20)+20);
    newbutton.setX(100);

    // set text
    newbutton.setText(Integer.toString(numOfButton));

    // create patameter
    final LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(
            LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT
            );

    //set listener
    android.view.View.OnClickListener buttonListener = new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            // make all the DrawView invisible
            for(View view : comments){
                view.setVisibility(View.INVISIBLE);
            }

            // set the chosen comment visible
            comments.get(numOfButton).setVisibility(View.VISIBLE);

            boardsHandler.setCurrenBoard(numOfButton);
        }};

        newbutton.setOnClickListener(buttonListener);

        // creating a thread to add button
        buttons.post(new Runnable() { 
            @Override
            public void run() {
                buttons.addView(newbutton, p);
            }
        });

Is it something with the LinearLayout.LayoutParams p ?

Thanks!

etaiso
  • 2,736
  • 3
  • 26
  • 38
  • have a look at the following links. http://stackoverflow.com/questions/6081685/android-listview-dynamic-buttons-for-each-row-calling-dynamic-listeners http://stackoverflow.com/questions/1998483/listview-dynamic-add-item – Ayush May 05 '13 at 17:40
  • @Ayush How those links are related to my problem? I can add buttons dynamically but my problem is that I don't have the scroller to scroll up/down. – etaiso May 05 '13 at 17:48

3 Answers3

1

Try following code first do

LinearLayout myContainer = findViewById(R.id.layoutId);

When you set parameters for a view, they need to correspond to the parent view for your widget.

LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, 
LinearLayout.LayoutParams.FILL_PARENT);

finally add button as you are doing.

try and tell if it works

karan
  • 8,637
  • 3
  • 41
  • 78
  • I'm not sure where to add the first line you wrote. As you can see I already have the LinearLayout inside the ScrollView. – etaiso May 05 '13 at 18:10
  • you can add it in oncreate() but if you are doing certain task onclick() then use it there – karan May 05 '13 at 18:17
  • etaiso is already using the variable `buttons` for the `LinearLayout`, so this seems redundant. Also, the ID is `R.id.buttons`, according to the layout file. – Ridcully May 05 '13 at 18:35
  • So what the purpose of `myContainer`? Where do I use it? – etaiso May 05 '13 at 18:38
  • @etaiso this simply will point to your container.and this will ease out stuffs and you do not need to remember them. – karan May 05 '13 at 18:41
0

Setting X and Y position will not work. The LinearLayout layouts it's children vertically or horizontally, only taking their width/height into account.

Besides this -- have you tried calling buttons.invalidate() after buttons.addView(...). This should refresh the layout and should show your newbutton.

Ridcully
  • 23,362
  • 7
  • 71
  • 86
  • Actually it does works.. The buttons are added each at it's appropriate time. The positioning is also fine , despite the fact it exceeds the bottom border when there is no space on screen - this is what I'm trying to achieve with the ScrollView. – etaiso May 05 '13 at 18:45
  • Oh sorry, I think I miss-read your question a bit. I understood that the buttons are not showing. But perhaps you should still try calling `invalidate()` as it will recalculate the size of the LinearLayout and this should finally also tell the scroll view when it's child needs scrolling. – Ridcully May 05 '13 at 18:52
  • So when should I call `invalidate()` and from what View? The code above is in a class which extends `Activity` ... But if the buttons are shown, why invalidate() is needed? Thanks for help. – etaiso May 05 '13 at 19:03
  • As I said, call buttons.invalidate() directly after buttons.addView(...). I'm not sure if it helps but it's worth a try. – Ridcully May 06 '13 at 05:46
0

This is a rather old post but I found it quickly when doing research on that kind of problem. So I'll post am answer anyway, maybe it'll be of help to anyone..

I had a similar problem with a relative layout to which buttons were added dynamically. I found a workaround in defining the layout's size manually when adding the buttons. For your case, adding the line

buttons.getLayoutParams().height = numOfButton*20+40;

after

buttons.addView(newbutton, p);

might help, though it's probably not the best solution. I thought my mistake was using the RelativeLayout at all, but since you appear to have the same problem... Ever thought of using a table layout?

Murc
  • 1