Spirit Spirit - 5 months ago 19
Android Question

Can't get string value from custom dialog in my fragment

I have my fragment where user can send points to another user now I need to create custom dialog where user need to reenter his pin. I done it following few tutorials but value is always null;

public void onViewCreated(View view, Bundle savedInstanceState) {
LayoutInflater inflater = LayoutInflater.from(getContext());
View confirmation=inflater.inflate(R.layout.confirmation,null);
pin = (EditText) confirmation.findViewById(R.id.PinChecker);

}


SendPointsButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());

builder.setView(R.layout.confirmation);

builder.setPositiveButton("DA", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
pinCheck= String.valueOf(pin.getText().toString());
Log.d("Pin",pinCheck);
}


I want to assing value from pin(EditText) to string called pinCheck;

Answer

I think you should pass the inflated layout (confirmation) to builder.setView() as below:

  final View confirmation=inflater.inflate(R.layout.confirmation,null);
  pin  = (EditText) confirmation.findViewById(R.id.PinChecker);

....


SendPointsButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getContext());

        builder.setView(confirmation);

        builder.setPositiveButton("DA", new    DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                pinCheck= String.valueOf(pin.getText().toString());
                Log.d("Pin",pinCheck);
} 

Hope this will help.

Comments