Si Thu
Si Thu

Reputation: 21

Want to Ask users to confirm his info is correct before startActivity, android

Hello I m newbie in Android development. So i practiced with one sample which is sending ussd code to operator such as recharging bill. I am okay with that.

But my main problem is to show dialog box when clicking button1 after filling PIN code to examine the user correct or not. I don't know how to add and what to add.

My codes are -

for MainActivity.java

package com.messagealert;

import android.net.Uri;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {

 [MENTION=439709]override[/MENTION]
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button button1 = (Button) findViewById(R.id.button1);
        button1.setOnClickListener(new OnClickListener(){
            public void onClick (View v){
                EditText editText1 =(EditText) findViewById(R.id.editText1);
                String pin = editText1.getText().toString();
                String encodedHash = Uri.encode("#");
                String ussd = "*" + "124" + pin + encodedHash;
                Intent button1 = new Intent(android.content.Intent.ACTION_CALL, Uri.parse("tel:" + ussd));      
                startActivity(button1);
            }
        });

    }

 [MENTION=439709]override[/MENTION]
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

for activity_main.xml

<RelativeLayout xmlns:android="h t t p://schemas. android. com/apk/res/android"
    xmlns:tools="h t t p:// schemas. android. com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="22dp"
        android:layout_centerHorizontal="true"
        android:ems="10"
        android:inputType="number"
        android:maxLength="6"
        android:hint="Enter 6 numbers here"
         >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText1"
        android:layout_marginTop="31dp"
        android:layout_centerHorizontal="true"
        android:text="@string/button1" />

</RelativeLayout>

Please guide what i need to put & where exactly to show a dialog box "Is that your PIN code - xxxxxx" & Yes or No

Sorry for my bad English.

With Best Regards,

Upvotes: 0

Views: 351

Answers (6)

Si Thu
Si Thu

Reputation: 21

Firstly, I am very thank you for your answers. Sorry i can't vote up your answers because i haven't enough reputations.

By adapting MSS's answers i changed a bit at MainActivity.java

package com.messagealert;

import android.net.Uri;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {

    final Context context = this;

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

        Button button1 = (Button) findViewById(R.id.button1);
        button1.setOnClickListener(new OnClickListener(){


            public void onClick (View v){
                EditText editText1 =(EditText) findViewById(R.id.editText1);
                String pin = editText1.getText().toString();

                AlertDialog.Builder dlgAlert  = new AlertDialog.Builder(context);

                    dlgAlert.setTitle("PIN VERIFICATION");
                    dlgAlert.setMessage("Is that your PIN code -" + pin );



                    dlgAlert.setPositiveButton("Yes",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                String encodedHash = Uri.encode("#");
                EditText editText1 =(EditText) findViewById(R.id.editText1);
                String pin = editText1.getText().toString();
                String ussd = "*" + "124" + pin + encodedHash;
                Intent button1 = new Intent(android.content.Intent.ACTION_CALL, Uri.parse("tel:" + ussd));      
                startActivity(button1);


                        }
                    });
dlgAlert.setNegativeButton("NO",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            EditText editText1 =(EditText) findViewById(R.id.editText1);
              editText1 .setText(" ");
            }
        });

dlgAlert.setCancelable(true);
dlgAlert.create().show();

            };
        });
 }}

You guys did a great help on me!

With Regards,

Upvotes: 0

NIPHIN
NIPHIN

Reputation: 1081

use an alertDialog for that inside you button onClick

AlertDialog.Builder dialog = new Builder(con);

 dialog.setTitle("Confirm PIN");
dialog.setMessage("You have entered pincode as"+pin+"\n Please Confirm to Continue");
 dialog.setNegativeButton("Edit Pin", new DialogInterface.OnClickListener() {

    @Override
    public void onClick(DialogInterface dialog, int which) {
        // TODO Auto-generated method stub
    dialog.dismiss();   
    }
});
 dialog.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {

    @Override
    public void onClick(DialogInterface dialog, int which) {
        // TODO Auto-generated method stub
         EditText editText1 =(EditText) findViewById(R.id.editText1);
        String pin = editText1.getText().toString();
        String encodedHash = Uri.encode("#");
        String ussd = "*" + "124" + pin + encodedHash;
        Intent button1 = new Intent(android.content.Intent.ACTION_CALL, Uri.parse("tel:" + ussd));      
            startActivity(button1);
        dialog.dismiss();
    }
});
 dialog.show();

Upvotes: 0

Sekhar Madhiyazhagan
Sekhar Madhiyazhagan

Reputation: 889

      Button button1 = (Button) findViewById(R.id.button1);
            button1.setOnClickListener(new OnClickListener(){
                public void onClick (View v){
                    EditText editText1 =(EditText) findViewById(R.id.editText1);
                    String pin = editText1.getText().toString();

     AlertDialog.Builder dlgAlert  = new AlertDialog.Builder(this);

                        dlgAlert.setMessage("PIN VERIFICATION");
                        dlgAlert.setTitle("Is that your PIN code -"+pin );
                        dlgAlert.setPositiveButton("OK", null);
                        dlgAlert.setCancelable(true);
                        dlgAlert.create().show();

                        dlgAlert.setPositiveButton("Yes",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int which) {
      String encodedHash = Uri.encode("#");
                    String ussd = "*" + "124" + pin + encodedHash;
                    Intent button1 = new Intent(android.content.Intent.ACTION_CALL, Uri.parse("tel:" + ussd));      
                    startActivity(button1);


                            }
                        });
  dlgAlert.setNegativeButton("NO",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {

                  editText1 .setText(" ");
                }
            });

Upvotes: 1

Balvinder Singh
Balvinder Singh

Reputation: 580

Use code below, this will help you:

     Button button1 = (Button) findViewById(R.id.button1);
    button1.setOnClickListener(new OnClickListener(){
        public void onClick (View v){
           dialog();
        }
    });


   public void dialog()
 {
     AlertDialog.Builder dialog = new Builder(con);

     dialog.setTitle("Message:");
    dialog.setMessage("Is that your PIN code - xxxxxx");
     dialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub
        dialog.dismiss();   
        }
    });
     dialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub
             EditText editText1 =(EditText) findViewById(R.id.editText1);
            String pin = editText1.getText().toString();
            String encodedHash = Uri.encode("#");
            String ussd = "*" + "124" + pin + encodedHash;
            Intent button1 = new Intent(android.content.Intent.ACTION_CALL, Uri.parse("tel:" + ussd));      
            startActivity(button1);
            dialog.dismiss();
        }
    });
     dialog.show();
 }

use variable global if needed.

Upvotes: 0

Meenal
Meenal

Reputation: 2877

Just add dialog in your button click only

 EditText editText1 =(EditText) findViewById(R.id.editText1);

Button button1 = (Button) findViewById(R.id.button1);
    button1.setOnClickListener(new OnClickListener(){
        public void onClick (View v){


           //call dialog here and on dialog ok press start your activity

            String pin = editText1.getText().toString();
            String encodedHash = Uri.encode("#");
            String ussd = "*" + "124" + pin + encodedHash;
            Intent button1 = new Intent(android.content.Intent.ACTION_CALL, Uri.parse("tel:" + ussd));      
            startActivity(button1);
        }
    });

Upvotes: 0

Hanish Sharma
Hanish Sharma

Reputation: 869

For this you have to add alert box Just like the below code

new AlertDialog.Builder(this)
    .setTitle("Delete entry")
    .setMessage("Are you sure you want to delete this entry?")
    .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) { 
            // continue with delete
        }
     })
    .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) { 
            // do nothing
        }
     })
    .setIcon(android.R.drawable.ic_dialog_alert)
     .show();

or try this

AlertDialog.Builder builder1 = new AlertDialog.Builder(context);
            builder1.setMessage("Write your message here.");
            builder1.setCancelable(true);
            builder1.setPositiveButton("Yes",
                    new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                }
            });
            builder1.setNegativeButton("No",
                    new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                }
            });

            AlertDialog alert11 = builder1.create();
            alert11.show();

Upvotes: 0

Related Questions