1

When I add a new button in Android Studio in constraint layout the first one disappears. I think the problem is with views.

There are two buttons that bring up a popup menu that let me choose from other buttons. I then add the new button to the place of the original. Unfortunately when I try to add a second button the first one disappear. I'm including some screenshots because I know this is hard to follow.

This is the popup menu, or the chord selector. I picked a chord and the app added it. But when I select another one for the grey, empty button on the right the blue one on the left disappears. It is actually under the new colored button, which is weird.

The 2 grey buttons are called empty1 and empty2 respectively. enter image description here

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    final ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.page_empty, container, false);



    final ConstraintLayout mConstraintLayout = (ConstraintLayout) rootView.findViewById(R.id.constraintEmpty);

   final ConstraintSet set = new ConstraintSet();

    set.clone(mConstraintLayout);

    final Typeface typeface = ResourcesCompat.getFont(getContext(), R.font.comfortaaregular);

    final AppCompatButton empty1 = (AppCompatButton) rootView.findViewById(R.id.empty1);
    empty1.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            PopupMenu popupMenu = new PopupMenu(getContext(), v);
            popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                @Override
                public boolean onMenuItemClick(MenuItem item) {
                    switch (item.getItemId()) {
                        case cm7popup:
                            Toast.makeText(getContext(), "Cm7 Chosen", Toast.LENGTH_LONG).show();
                            alreadychosen=true;
                            final AppCompatButton cm7button = new AppCompatButton(getContext());

                            cm7button.setText("Cm7");
                            cm7button.setTextSize(18);
                            cm7button.setPadding(16,0,0,0);

                            cm7button.setTypeface(typeface);

                            cm7button.setId(cm7);           // <-- Important
                            cm7button.setBackgroundResource(R.drawable.blue_button);
                            cm7button.setGravity(Gravity.TOP);
                            mConstraintLayout.addView(cm7button);
                            set.connect(cm7button.getId(),ConstraintSet.TOP,R.id.guidelinetest13,ConstraintSet.TOP,0);
                            set.connect(cm7button.getId(), ConstraintSet.BOTTOM, R.id.guidelinetest14, ConstraintSet.BOTTOM, 0);
                            set.connect(cm7button.getId(),ConstraintSet.RIGHT,R.id.guidelinetest2,ConstraintSet.RIGHT,0);
                            set.connect(cm7button.getId(),ConstraintSet.LEFT,R.id.guidelinetest1,ConstraintSet.LEFT,0);
                            // set.constrainHeight(cm7button.getId(), 200);
                            set.applyTo(mConstraintLayout);


                            cm7button.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    MainActivity.myVib.vibrate(vibstark);
                                    SoundEngine.playsound57();

                                }
                            });

                            cm7button.setOnLongClickListener(new View.OnLongClickListener(){

                                public boolean onLongClick(View v){

                                    mConstraintLayout.removeView(cm7button);
                                    Toast.makeText(getContext(), "Chord Removed", Toast.LENGTH_SHORT).show();

                                    return true;
                                }


                              });



                            return true;

Then there are 95 other cases here........ Then comes grey button 2.

    final AppCompatButton empty2 = (AppCompatButton) rootView.findViewById(R.id.empty2);
    empty2.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            PopupMenu popupMenu = new PopupMenu(getContext(), v);
            popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                @Override
                public boolean onMenuItemClick(MenuItem item) {
                    switch (item.getItemId()) {
                        case cm7popup:
                            Toast.makeText(getContext(), "Cm7 Chosen", Toast.LENGTH_LONG).show();
                            alreadychosen=true;
                            final AppCompatButton cm7button = new AppCompatButton(getContext());

                            cm7button.setText("Cm7");
                            cm7button.setTextSize(18);
                            cm7button.setPadding(16,0,0,0);


                            cm7button.setTypeface(typeface);

                            cm7button.setId(cm7_b);           // <-- Important
                            cm7button.setBackgroundResource(R.drawable.blue_button);
                            cm7button.setGravity(Gravity.TOP);
                            mConstraintLayout.addView(cm7button);
                            set.connect(cm7button.getId(),ConstraintSet.TOP,R.id.guidelinetest13,ConstraintSet.TOP,0);
                            set.connect(cm7button.getId(), ConstraintSet.BOTTOM, R.id.guidelinetest14, ConstraintSet.BOTTOM, 0);
                            set.connect(cm7button.getId(),ConstraintSet.RIGHT,R.id.guidelinetest4,ConstraintSet.RIGHT,0);
                            set.connect(cm7button.getId(),ConstraintSet.LEFT,R.id.guidelinetest3,ConstraintSet.LEFT,0);
                            // set.constrainHeight(cm7button.getId(), 200);
                            set.applyTo(mConstraintLayout);



                            cm7button.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    MainActivity.myVib.vibrate(vibstark);
                                    SoundEngine.playsound57();

                                }
                            });

                            cm7button.setOnLongClickListener(new View.OnLongClickListener(){

                                public boolean onLongClick(View v){

                                    mConstraintLayout.removeView(cm7button);
                                    Toast.makeText(getContext(), "Chord Removed", Toast.LENGTH_SHORT).show();
                                    return true;
                                }


                            });



                            return true;

And again 95 more cases.

derpaherpa
  • 79
  • 7
  • You haven't shown what else happens when the user selects a chord but my guess is that your button doesn't disappear, it's hidden behind the grey button. You aren't removing the grey button so when this is redrawn the grey button is drawn over the top of your blue button. – Bakon Jarser Jul 22 '19 at 20:30
  • When the user selects a chord the blue button appears. Then when I want to select a new one it disappears. – derpaherpa Jul 23 '19 at 05:35
  • But you're right. The grey one is not removed, it is just pushed in the background. Is there a hierarchy between buttons? Can I set which one should be higher? – derpaherpa Jul 23 '19 at 07:52
  • Why should the grey one still be there? greyButton.setVisibility(View.GONE); When you want to make it reappear greyButton.setVisibiltiy(View.VISIBLE); – Bakon Jarser Jul 23 '19 at 14:48
  • I tried that. Doesn't help. – derpaherpa Jul 23 '19 at 16:54
  • Can you post the whole method? Btw, you can cut down on code reuse here by creating a custom button style and setting the style on the button instead of setting all of the attributes individually each time. Check out section 3 in this answer: https://stackoverflow.com/a/29848987/3501286 – Bakon Jarser Jul 23 '19 at 17:09
  • I am adding some more code – derpaherpa Jul 23 '19 at 17:50

0 Answers0