So, I’m making an app that needs several activities, for the first time, ever… This was really nice, I found out how to make a new activity, and call it (open it). 
However, there was a big twist. I needed 5 new activities, and I needed only one of them to open, based on number of filled out fields (editText). 

MainActivity



Here is how I did it:

public class MainActivity extends AppCompatActivity {

    public EditText playerOne,playerTwo,playerThree,playerFour,playerFive,playerSix;

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

        Button btn = findViewById(R.id.btn);
        playerOne = findViewById(R.id.playerOne);
        playerTwo = findViewById(R.id.playerTwo);
        playerThree = findViewById(R.id.playerThree);
        playerFour = findViewById(R.id.playerFour);
        playerFive = findViewById(R.id.playerFive);
        playerSix = findViewById(R.id.playerSix);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                int filledFileds = countFilledFields();
                Log.d("filled", String.valueOf(filledFileds));
                Class newClass = null;

                switch (filledFileds){

                    case 1:

                        Context context = getApplicationContext();
                        CharSequence text = "You need more players!";
                        int duration = Toast.LENGTH_SHORT;

                        Toast toast = Toast.makeText(context, text, duration);
                        toast.show();

                        break;

                    case 2:

                        newClass = Activity2.class;
                        System.out.println("Activity2");

                        break;

                    case 3:

                        newClass = Activity3.class;
                        System.out.println("Activity3");

                        break;

                    case 4:

                        newClass = Activity4.class;
                        System.out.println("Activity4");

                        break;

                    case 5:

                        newClass = Activity5.class;
                        System.out.println("Activity5");

                        break;

                    case 6:

                        newClass = Activity6.class;
                        System.out.println("Activity6");

                        break;

                    default:


                }

                if (newClass != null) {
                    Intent intent = new Intent(MainActivity.this, newClass);
                    startActivity(intent);
                }

                Intent intent = new Intent(MainActivity.this, newClass);
                startActivity(intent);
            }
        });
    }

    private int countFilledFields() {

        ArrayList<EditText> editTexts = new ArrayList<>();
        editTexts.add(playerOne);
        editTexts.add(playerTwo);
        editTexts.add(playerThree);
        editTexts.add(playerFour);
        editTexts.add(playerFive);
        editTexts.add(playerSix);

        int filledNumber = 0;

        for(int i = 0;i < editTexts.size() ;i++){

            if(editTexts.get(i).getText()!=null && !editTexts.get(i).getText().toString().matches("")){
                filledNumber += 1;
            }

        }
        return filledNumber;
    }

}
Social Follow and Likes:

Leave a Reply

Your email address will not be published. Required fields are marked *