Passing Extra data over android custom alert dialog

I want to put phone number from editText to custom alert dialog when i click "NEXT" like this : 1 I have been put getStringExtra to textView custom dialog but i should click "Yes", then number success displayed. But what i want is when i click "NEXT", number displayed.

Code :

public void sendVerificationCode() {
    phoneNumber = countryCode.getFullNumberWithPlus(); // The number i want to display to custom alert dialog

    Intent moveVerification = new Intent(CreateUserActivity.this, VerificationCode.class);
    moveVerification.putExtra("sendPhoneNumber", phoneNumber);
    startActivity(moveVerification);
    finish();
}

public void dialogVerification() {
    dialogVerification = new Dialog(CreateUserActivity.this);
    dialogVerification.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialogVerification.setContentView(R.layout.custom_alert_dialog);
    dialogVerification.setCanceledOnTouchOutside(false);

    Button buttonYes = dialogVerification.findViewById(R.id.buttonYes);
    Button buttonEdit = dialogVerification.findViewById(R.id.buttonEdit);
    TextView displayNumber = dialogVerification.findViewById(R.id.displayNumber); // I want to display the number at here

    dialogVerification.getWindow().setLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    dialogVerification.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    dialogVerification.show();
}

Upvotes: 1

Views: 749

Answers (4)

Snow Star
Snow Star

Reputation: 86

I'm using the View Binding in android latest version.

I customized the Alert Dialog and passed the required values to the dialog.

private ConnectDialogBinding connectDialogBinding;
private String chargerID;

private void connectDialog() {
    // Create the object of
    // AlertDialog Builder class
    AlertDialog.Builder builder = new AlertDialog.Builder(ConnectActivity.this);
    connectDialogBinding = ConnectDialogBinding.inflate(getLayoutInflater());
    builder.setView(connectDialogBinding.getRoot());
    connectDialogBinding.txtID.setText(chargerID);
    builder.setCancelable(false);

    // Create the Alert dialog
    AlertDialog alertDialog = builder.create();

    // Show the Alert Dialog box
    alertDialog.show();

    connectDialogBinding.cancelBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            alertDialog.cancel();
        }
    });
}

enter image description here

Upvotes: 0

Yashwant Kumar
Yashwant Kumar

Reputation: 143

public class MainActivity extends AppCompatActivity {
EditText phonenum;
Button next;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    phonenum = findViewById(R.id.editText);
    next = findViewById(R.id.button);
    next.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // Perform action on click
            go();
        }
    });

}
void go(){
    String somenumber = String.valueOf(phonenum.getText());
    AlertDialog dialog;
    final AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);
    alertDialog.setTitle("Number verification");
    alertDialog.setMessage(somenumber + "\n" + "is your number above correct ?");
    alertDialog.setCancelable(false);
    alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            //call function to start next activity
        }
    });

    alertDialog.setNegativeButton("Edit", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            finish();
        }
    });
    dialog = alertDialog.create();
    dialog.show();

}

}

Upvotes: 1

Lalit Fauzdar
Lalit Fauzdar

Reputation: 6363

  • If your code is in the same activity:

Either pass the phoneNumber as a parameter to the dialogVerification() or declare it as a global variable to use it in any function directly.

  1. To pass it as a parameter:

    call dialogVerification() as from sendVerificationCode() :

    dialogverification(phoneNumber);

and replace this -> public void dialogVerification() {

with

public void dialogVerification(int phoneNumber) {

And simply put the phone number in the textView as

displayNumber.setText(phoneNumber.toString());

  1. Declaring it as a global variable: declare the phoneNumber before the onStart() just below the class declaration as int phonNumber;

And simply put the phone number in the textView as

displayNumber.setText(phoneNumber.toString());

  • Else if this dialogVerification is in another VerificationCode

pass it to the other activity as:

Intent intent = new Intent(CreateUserActivity.this, VerificationCode.class);
intent.putExtra("Phone_Number", phoneNumber);
startActivity(intent);

and access that number in VerificationCode as:

int phoneNumber= getIntent().getIntExtra("Phone_Number");

And simply put the phone number in the textView as

displayNumber.setText(phoneNumber.toString());

PS: It it's a string, just replace every int with String, getIntExtra as getStringExtra and remove that .toString().

Upvotes: 1

ChickenSoups
ChickenSoups

Reputation: 975

You can create a static class and create a static field to hold your phoneNumber variable value. Then you can access your phoneNumber variable anywhere. Not a best practice, but this way always work out

Upvotes: 0

Related Questions