DEJAVU SG
DEJAVU SG

Reputation: 1

how can i set alert dialogs into if and else statement

I have 2 EditTexts that get a String and when the Button is clicked it converts char of this String to int. It then sums all int of chars and when sum of int is odd (totals&1 == 0) a Dialog shows "that is odd". And after else, the second Dialog shows "that is even". I need to set AlertDialog to show that.

final EditText ed1 = (EditText) findViewById(R.id.edtFather);
        final EditText ed2 = (EditText) findViewById(R.id.edtMother);
        //final TextView tv = (TextView) findViewById(R.id.txtfather);
        Button btn = (Button) findViewById(R.id.button1);

        btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {

                String dataF = ed1.getText().toString();
                char[] charArray = dataF.toCharArray();
                int totalF = 0;
                for (char ch: charArray) {
                    totalF += characterMap.get(ch);
                }

                String dataM = ed2.getText().toString();
                char[] charArr = dataM.toCharArray();
                int totalM = 0;
                for (char ch2: charArr) {
                    totalM += characterMap.get(ch2);
                }

                int sum = totalF + totalM;
                int totals = sum % 5;
                if ((totals & 1) == 0)

                    //alert dialog 1 show "that is odd"

                else {

                    //alert dialog 2 show "that is even"

                }

            }
        });

Upvotes: 0

Views: 949

Answers (3)

DEJAVU SG
DEJAVU SG

Reputation: 1

i found that i use AlertDialog like this :

 btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                AlertDialog.Builder builder = new AlertDialog.Builder(StartActivity.this);
                builder.setMessage("that is odd");
                builder.setCancelable(true);
                AlertDialog alert = builder.create();

                String dataF = ed1.getText().toString();
                char[] charArray = dataF.toCharArray();
                int totalF = 0;
                for (char ch: charArray) {
                    totalF += characterMap.get(ch);
                }

                String dataM = ed2.getText().toString();
                char[] charArr = dataM.toCharArray();
                int totalM = 0;
                for (char ch2: charArr) {
                    totalM += characterMap.get(ch2);
                }

                int sum = totalF + totalM;
                int totals = sum % 5;

                if ((totals & 1) == 0)

                    alert.show();

                else {

                    AlertDialog.Builder builder1 = new AlertDialog.Builder(StartActivity.this);
                    builder1.setMessage("that is even");
                    builder1.setCancelable(true);
                    AlertDialog alert1 = builder1.create();
                    alert1.show();

                }

            }
        });

Upvotes: 0

Cloud57
Cloud57

Reputation: 66

Use Toast for this purpose, they are a lot easier and fast for a single message.

    Toast.makeText(this, "Your Message here", 
   Toast.LENGTH_LONG).show();

Upvotes: 0

Giorgio Antonioli
Giorgio Antonioli

Reputation: 16214

AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage("that is odd");
builder.setCancelable(true);
AlertDialog alert = builder.create();
alert.show();

Just write "that is even" instead of "that is odd" for the second dialog.

Upvotes: 2

Related Questions