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.
@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.