T
T
tema862016-06-16 17:25:06
Android
tema86, 2016-06-16 17:25:06

How to make a clickable menu of buttons using switch?

Hello, I am making a menu of several buttons using two different methods.
One of them provides a transition to another activity at the click of a button, and the second method does not, that is, nothing happens when you click it.
Working method:

public class main extends AppCompatActivity {

    Button b1,b2,b3;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LinearLayout l1 = new LinearLayout(this);
        l1.setOrientation(LinearLayout.VERTICAL);
        LinearLayout.LayoutParams l2 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        setContentView(l1,l2);


        b1 = (Button) findViewById(R.id.b1);
        b2 = (Button) findViewById(R.id.b2);
        b3 = (Button) findViewById(R.id.b3);


        LinearLayout.LayoutParams l3 = new LinearLayout.LayoutParams(200, ViewGroup.LayoutParams.WRAP_CONTENT);
        

        b1 = new Button(this);
        b1.setText("Button 1");
        l1.addView(b1,l3);

        b2= new Button(this);
        b2.setText("Button 2");
        l1.addView(b2,l3);

        b3 = new Button(this);
        b3.setText("Button 3");
        l1.addView(b3,l3);


        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(main.this,m2.class);
                startActivity(intent);
            }
        });

        b2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(main.this,m3.class);
                startActivity(intent);
            }
        });
        b3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(main.this,m4.class);
                startActivity(intent);
            }
        });
    }

and not working:
public class main extends AppCompatActivity {

    Button b1,b2,b3;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LinearLayout l1 = new LinearLayout(this);
        l1.setOrientation(LinearLayout.VERTICAL);
        LinearLayout.LayoutParams l2 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        setContentView(l1,l2);


        b1 = (Button) findViewById(R.id.b1);
        b2 = (Button) findViewById(R.id.b2);
        b3 = (Button) findViewById(R.id.b3);


        LinearLayout.LayoutParams l3 = new LinearLayout.LayoutParams(200, ViewGroup.LayoutParams.WRAP_CONTENT);
        

        b1 = new Button(this);
        b1.setText("Button 1");
        l1.addView(b1,l3);

        b2= new Button(this);
        b2.setText("Button 2");
        l1.addView(b2,l3);

        b3 = new Button(this);
        b3.setText("Button 3");
        l1.addView(b3,l3);


       View.OnClickListener onClickListener = (new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch(v.getID()) {
                    case R.id.b1:
                            Intent intent = new Intent(main.this,m2.class);
                            startActivity(intent);
                            break;
                    case R.id.b2:
                            Intent intent1 = new Intent(main.this,m3.class);
                            startActivity(intent1);
                            break;
                    case R.id.b3:
                            Intent intent2 = new Intent(main.this,m4.class);
                            startActivity(intent2);
                            break;
                }
            }
        });
    
        b1.setOnClickListener(onClickListener);
        b2.setOnClickListener(onClickListener);
        b3.setOnClickListener(onClickListener);

}

What could be the problem? Thank you

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
viktormarkov, 2016-06-16
@tema86

First you assign to the variables b1, b2, b3 the Button objects created by the findViewById method

b1 = (Button) findViewById(R.id.b1);
        b2 = (Button) findViewById(R.id.b2);
        b3 = (Button) findViewById(R.id.b3);

Then assign new Button objects to the same variables
b1 = new Button(this);
....

Accordingly, these variables refer to completely different buttons, the id of which is automatically generated.
Therefore, in the switch, the condition does not work.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question