addisonElliott / SegmentedButton

Segmented Control/Button with animation for Android API 16+
Apache License 2.0
148 stars 39 forks source link

setting divider programmatically not working as expected #21

Closed mgarfinkle closed 4 years ago

mgarfinkle commented 4 years ago

I made a button group programmatically, but when I set the divider, it only shows them all grouped tightly to the left of the first button.

Screen Shot 2019-09-29 at 2 28 48 PM

Code:

Screen Shot 2019-09-29 at 2 25 25 PM
addisonElliott commented 4 years ago

Hi, thanks for the issue. That doesn't surprise me it's an issue, I didn't do a lot of testing with programatically creating the view.

Can you upload the code as text so I can copy/paste and play around with it?

I won't be able to get to this for awhile unfortunately. Any PRs are welcome.

mgarfinkle commented 4 years ago

See below for the code. In addition, I found another issue. When programmatically increasing the text size, the width of the individual buttons do not scale appropriately.

        SegmentedButtonGroup rg = new SegmentedButtonGroup(ctx);
        rg.setBorder(3,Color.LTGRAY,0,0);
        rg.setOnPositionChangedListener(new     
        SegmentedButtonGroup.OnPositionChangedListener() {
            @Override
            public void onPositionChanged(final int position) {
                if (doneSetup) {
                    for (int i=0;i < valnams.size();i++) {
                        segButArray.get(num).getButton(i).setSelectedTextColor(Color.WHITE);
                        segButArray.get(num).getButton(i).setSelectedBackgroundColor(getResources().getColor(R.color.colorPrimary));
                    }
                    clicked(num, position);
                }
            }
        });

        segButArray.add(rg);
        LayoutParams segLayoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        segLayoutParams.setMargins(32, 16, 32, 16);

        for (int i = 0; i < valnams.size(); i++) {
            SegmentedButton rb = new SegmentedButton(ctx);

            rb.setText(valnams.get(i));
            rb.setTextSize(Globals.convertDpToPixel(18));
            rb.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, (float) 1.0));
            rb.setPadding(16,16,16,16);
            rb.setTextColor(Color.BLACK);
            rb.setSelectedTextColor(Color.BLACK);
            rg.addView(rb);
        }

        layout.addView(rg, segLayoutParams);
addisonElliott commented 4 years ago

@mgarfinkle The bugs you mentioned should be fixed now in v3.1.7.

I took your example code and placed it in the example project so that any future changes can ensure it still works properly.

As a note, the issue with the dividers was related to how dividers were positioned. They required a layout_weight to be set on each of the buttons to function. I refactored that to allow fixed-size buttons.