Martynas Kiela
Martynas Kiela

Reputation: 3

How to make different button open different activities?

I am working on my first app that needs a few windows. I made six buttons but al of them open the same activity. How to fix that? Sorry for the messy code I am just beginning. Also, a few tips on how to make my code tidier would also help.
This is my code:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button button1 = findViewById(R.id.button1);
        Button button2 = findViewById(R.id.button2);
        Button button3 = findViewById(R.id.button3);
        Button button4 = findViewById(R.id.button4);
        Button button5 = findViewById(R.id.button5);
        Button button6 = findViewById(R.id.button6);

        button1.setOnClickListener(this);
        button2.setOnClickListener(this);
        button3.setOnClickListener(this);
        button4.setOnClickListener(this);
        button5.setOnClickListener(this);
        button6.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
    switch (v.getId()){
        case R.id.button1:
            Intent intent1 = new Intent(this, objektas1.class);
            startActivity(intent1);
            break;
        case R.id.button2:
            Intent intent2 = new Intent(this, objektas2.class);
            startActivity(intent2);
            break;
        case R.id.button3:
            Intent intent3 = new Intent(this, objektas3.class);
            startActivity(intent3);
            break;
        case R.id.button4:
            Intent intent4 = new Intent(this, objektas4.class);
            startActivity(intent4);
            break;
        case R.id.button5:
            Intent intent5 = new Intent(this, objektas5.class);
            startActivity(intent5);
            break;
        case R.id.button6:
            Intent intent6 = new Intent(this, objektas6.class);
            startActivity(intent6);
            break;
    }
    }
}

Upvotes: 0

Views: 48

Answers (1)

Azhagthott
Azhagthott

Reputation: 502

Your code seems to be ok, I just copy your code and made some changes

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button button1 = findViewById(R.id.button1);
    Button button2 = findViewById(R.id.button2);
    Button button3 = findViewById(R.id.button3);

    button1.setOnClickListener(this);
    button2.setOnClickListener(this);
    button3.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.button1:
            Intent intent1 = new Intent(this, FirstActivity.class);
            startActivity(intent1);
            break;
        case R.id.button2:
            Intent intent2 = new Intent(this, SecondActivity.class);
            startActivity(intent2);
            break;
        case R.id.button3:
            Intent intent3 = new Intent(this, ThirdActivity.class);
            startActivity(intent3);
            break;
    }
}
}

Just to be sure see the next images

enter image description here

check the structure of your project, you should have something very similar but with 7 activities (MainActivity + objektas1...objektas6).

Remember every time you create a new activity the layout is empty so try to add a TextView or something similar to see the difference.

And one last advice try to use names like ObjektasActivity instead of objektas1

Upvotes: 2

Related Questions