1

I made a XML element that automatically creates a Button, then a ScrollView with a LinearLayout inside. The Button, when pressed, is supposed to change color and change the Visibility of the ScrollView. Right now, it only changes the color of the Button, but the Text inside the Scroll View stays visible.

This worked before when I created the Button and ScrollView through XML and created onClickListeners through the onCreate() method in the main activity.

my code:

public class AccordionWidget extends LinearLayout{


    public AccordionWidget(Context c, AttributeSet attrs) {
        super(c, attrs);

        final Context context = c;

        final Button btn = new Button(context);
        final LinearLayout ll = new LinearLayout(context);
        final ScrollView sv = new ScrollView(context);

        TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.accordion);

        LayoutParams btnparams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        btn.setText(a.getString(R.styleable.accordion_text));
        btn.setBackgroundColor(ContextCompat.getColor(context, R.color.button_not_pressed));
        LayoutParams llparams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        llparams.weight = 1f;
        LayoutParams swparams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);

        btn.setLayoutParams(btnparams);
        ll.setOrientation(LinearLayout.VERTICAL);
        ll.setLayoutParams(llparams);
        sv.setVisibility(View.GONE);
        sv.setLayoutParams(swparams);

        this.addView(sv);
        this.addView(btn);
        sv.addView(ll);

        btn.setOnClickListener(new OnClickListener() {
            boolean btnstate = false;
            @Override
            public void onClick(View v) {
                if (btnstate) {
                    btn.setBackgroundColor(ContextCompat.getColor(context, R.color.button_pressed));
                    sv.setVisibility(View.VISIBLE);
                    btnstate = false;
                } else {
                    btn.setBackgroundColor(ContextCompat.getColor(context, R.color.button_not_pressed));
                    sv.setVisibility(View.GONE);
                    btnstate = true;
                }
            }
        });

        a.recycle();
    }

}

lenny
  • 734
  • 2
  • 15
  • 43

3 Answers3

1

Add color for background of sv. Try this:

final ScrollView sv = new ScrollView(context);
sv.setBackgroundColor(0xffffff);//make it not transparent :)

Because your code does not mention about the text so I suppose that the text is in different layer of the scrollview and if scrollview is on top of it, by setting the bgcolor it can blind the text.

T D Nguyen
  • 7,054
  • 4
  • 51
  • 71
  • inside the scroll view is a linear layout, and in the LL are text views. I see what you're trying to do, but for my purposes it won't do – lenny Mar 22 '16 at 09:41
  • Why don't you try to set visibility for the `LL` to see what happen? or move `boolean btnstate ` to be global variable – T D Nguyen Mar 22 '16 at 09:49
  • 1
    I already tried setting the LL's Visibility, same results. I don't see how chaning the scope of the boolean would affect the outcome. it is only relevant to the if statement, which works, since the button does switch color each time you press it. just the scrollviews visibility doesn't change. – lenny Mar 22 '16 at 09:53
1

Currently you're setting the visibility of the ScrollView but not the visibility of its children. You could try to hide all the children of the ScrollView individually like shown in this answer:

for ( int i = 0; i < sv.getChildCount();  i++ ){
    View view = sv.getChildAt(i);
    view.setVisibility(View.GONE);
}

So the idea is simple: get the children one by one and set the visibility. Of course you could do more stuff with the children if you need to.

Community
  • 1
  • 1
l7r7
  • 1,134
  • 1
  • 7
  • 23
  • All the children of a View are invisible, when the view is invisible. It worked before when I created the Button and ScrollView through XML and created ´onClickListeners´ through the ´onCreate()´ method in the main activity. – lenny Mar 22 '16 at 09:22
  • What exactly do you mean by "before"? I don't understand exactly what you've changed – l7r7 Mar 22 '16 at 09:32
  • I went from creating the button, scroll view and linear layout manually in the xml file to making my own xml element and therefore creating them in the class you see above. – lenny Mar 22 '16 at 09:37
  • Ah, now I see. What are the reasons for creating your own class? Just code readability? – l7r7 Mar 22 '16 at 09:39
  • that and learning. I'm pretty new to android and this is my first project. – lenny Mar 22 '16 at 09:54
0

Have you tried swapping these two lines:

sv.setVisibility(View.GONE);
sv.setLayoutParams(swparams);

Maybe setLayoutParams overrides the visibility set by setVisibility

l7r7
  • 1,134
  • 1
  • 7
  • 23
  • tried it, doesnt seem to make a difference. this also wouldn't explain why it doesn't change the visibility when i press the button. thanks anyway – lenny Mar 22 '16 at 09:08