-4

i have trouble in getText from EditText, i have make all the method that i know but it doesn't work Eventhough i filled the editText the answer allways says Field is required. Here is my code

 private void buildDialog(int animationSource) {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    View mView = getLayoutInflater().inflate(R.layout.dialog_activation, null);
    builder.setView(mView);
    AlertDialog dialog = builder.create();
    dialog.getWindow().getAttributes().windowAnimations = animationSource;
    dialog.show();

    Button mLogin = (Button) mView.findViewById(R.id.btnAktivasi);
    mLogin.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            AllowingToUpdate();
        }
    });
}

private void AllowingToUpdate() {
    View mView = getLayoutInflater().inflate(R.layout.dialog_activation, null);

    //I want to get Text From mMesin
    EditText mMesin = (EditText) mView.findViewById(R.id.etMesin);
    String NoMesin = mMesin.getText().toString();

    //Toast display always "Field is required" eventhough i have filled the editText

    if(mMesin.getText().toString().equals(""))
    {
        Toast.makeText(this,"Field Is Required....",Toast.LENGTH_SHORT).show();
    }else{
        Toast.makeText(this,"This Is Work....."+NoMesin,Toast.LENGTH_SHORT).show();
    }
}
4
  • What “trouble”?
    – Ken Sharp
    Aug 9, 2018 at 3:13
  • Always Display Toast "Field is Required" eventhough i have filled the edit text Aug 9, 2018 at 3:15
  • Pass your mView to the AllowingToUpdate() method as parameter as allowingToUpdate(mView) and from that view access the edit text. That will solve your problem
    – Devil10
    Aug 9, 2018 at 3:44
  • Yes you're right the solution is just pass mView as parameter and it works...Thanks for your help Aug 9, 2018 at 5:16

2 Answers 2

0

You are inflating the layout again which will give you a new layout(View) which does not attached to AlertDialog. Keep the things Simple and use the same view which is added to AlertDialog. See the code below you will get the idea .

 private void buildDialog(int animationSource) {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    View mView = getLayoutInflater().inflate(R.layout.dialog_activation, null);
    builder.setView(mView);
    AlertDialog dialog = builder.create();
    dialog.getWindow().getAttributes().windowAnimations = animationSource;
    dialog.show();
    final EditText mMesin = (EditText) mView.findViewById(R.id.etMesin);
    Button mLogin = (Button) mView.findViewById(R.id.btnAktivasi);
    mLogin.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (TextUtils.isEmpty(mMesin.getText().toString())) {
                Toast.makeText(ActivityName.this, "Field Is Required....", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(ActivityName.this, "This Is Work....." + mMesin.getText().toString().trim(), Toast.LENGTH_SHORT).show();
            }
        }
    });
}

Or you can pass mView as a parameter to method AllowingToUpdate. This will also solve it .

1
  • I also pass mView as a parameter and it also works...once again thanks for your help Aug 9, 2018 at 5:15
0

Change your buildDialog method with below:

Make your String NoMesin global.

private void buildDialog(int animationSource) {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    View mView = getLayoutInflater().inflate(R.layout.dialog, null);
    builder.setView(mView);
    AlertDialog dialog = builder.create();
    dialog.getWindow().getAttributes().windowAnimations = animationSource;
    dialog.show();

    final EditText mMesin = mView.findViewById(R.id.etMesin);

    mMesin.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            NoMesin = mMesin.getText().toString();
        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            NoMesin = mMesin.getText().toString();
        }

        @Override
        public void afterTextChanged(Editable editable) {
            NoMesin = mMesin.getText().toString();
        }
    });

    //Toast display always "Field is required" eventhough i have filled the editText

    Button mLogin = mView.findViewById(R.id.btnAktivasi);
    mLogin.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (TextUtils.isEmpty(NoMesin)) {
                Toast.makeText(MainActivity.this, "Field Is Required....", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(MainActivity.this, "This Is Work....." + NoMesin, Toast.LENGTH_SHORT).show();
            }
        }
    });
}
2
  • @ImmanuelSaragih updated my answer which will surely work for you. Aug 9, 2018 at 5:22
  • I have made it but the result still same Aug 9, 2018 at 5:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.