2

I need to change positive button's text of an AlertBox from nothing to OK or Add according to the entered text. I have the following code for the AlertBox creation and displaying:

public void show() {
    View inputView = LinearLayout.inflate(mContext, R.layout.goal_tag_input, null);
    AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
    builder.setView(inputView);
    mInput = inputView.findViewById(R.id.goal_tag_input);
    mInput.addTextChangedListener(mTagNameTextWatcher);
    List<Tag> availableTags = AppDatabase.getInstance(mContext).tagDao().getAll();
    mAvailableTagLabels = new ArrayList<>();
    for (Tag tag : availableTags) {
        mAvailableTagLabels.add(tag.getName());
    }
    ArrayAdapter<String> inputAdapter = new ArrayAdapter<>(mContext, 
    android.R.layout.select_dialog_item, mAvailableTagLabels);
    mInput.setAdapter(inputAdapter);
    builder.setCancelable(true);
    builder.setPositiveButton("", mAddTagClickListener);
    builder.setNegativeButton(R.string.Cancel, null);
    mDialog = builder.create();
    mDialog.show();
}

Also I have a TextWatcher implementation:

private TextWatcher mTagNameTextWatcher = new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {}

    @Override
    public void afterTextChanged(Editable s) {
        String tagName = mInput.getText().toString();
        if (tagName.trim() != "") {
            Button buttonPositive = mDialog.getButton(DialogInterface.BUTTON_POSITIVE);
            if (mAvailableTagLabels.contains(tagName)) {
                buttonPositive.setText(R.string.OK);
            } else {
                buttonPositive.setText(R.string.Add);
            }
        }
    }
};

During debugging I observed that the text value of the buttonPositive is changed appropriately, but it is not reflected in the interface. Do you have any ideas why is it so? I checked this answer but it didn't help.

Vitalii Isaenko
  • 941
  • 1
  • 14
  • 37

2 Answers2

2

Well, it appeared that the problem was in the setting a positive button using the AlertDialog builder here: builder.setPositiveButton("", mAddTagClickListener);. Apparently, a button is not created if you pass an empty string as the first argument. The minute I tried to change it to (at least) one-space-string - everything began to work as expected.
Later on I changed the approach to enabling/disabling the button.

Vitalii Isaenko
  • 941
  • 1
  • 14
  • 37
0

Well you can try this

 public void show() {
        View inputView = LinearLayout.inflate(AppIntroActivity.this, R.layout.goal_tag_input, null);
        AlertDialog.Builder builder = new AlertDialog.Builder(AppIntroActivity.this);
        builder.setView(inputView);
       final EditText mInput =(EditText) inputView.findViewById(R.id.goal_tag_input);
        final Button buttonPositive = (Button) inputView.findViewById(R.id.button_id);
        mInput.addTextChangedListener( new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {}

            @Override
            public void afterTextChanged(Editable s) {
                String tagName = mInput.getText().toString();
                if (tagName.trim() != "") {
                    if (mAvailableTagLabels.contains(tagName)) {
                    buttonPositive.setText(R.string.OK);
                } else {
                    buttonPositive.setText(R.string.Add);
                }
                }
            }
        };);
        List<Tag> availableTags = AppDatabase.getInstance(mContext).tagDao().getAll();
        mAvailableTagLabels = new ArrayList<>();
        for (Tag tag : availableTags) {
            mAvailableTagLabels.add(tag.getName());
        }
        ArrayAdapter<String> inputAdapter = new ArrayAdapter<>(mContext,
                android.R.layout.select_dialog_item, mAvailableTagLabels);
        mInput.setAdapter(inputAdapter);
        builder.setCancelable(true);
        builder.setPositiveButton("", mAddTagClickListener);
        builder.setNegativeButton(R.string.Cancel, null);
        mDialog = builder.create();
        mDialog.show();
    }
Quick learner
  • 10,632
  • 4
  • 45
  • 55
  • How can I get an id of the button? `final Button buttonPositive = (Button) inputView.findViewById(R.id.button_id);`. In general, did you want just to move `TextWatcher` implementation in the show method, or what was your intention? – Vitalii Isaenko May 31 '18 at 13:09
  • just add the Button in dialog xml and manage the text change in listener – Quick learner May 31 '18 at 13:10
  • I don't get what you mean. Have you worked with `AlertDialog` before? There is no layout for it. Button is created here: `builder.setPositiveButton("", mAddTagClickListener);` – Vitalii Isaenko May 31 '18 at 13:14
  • add button in goal_tag_input layout and in on text change listener just manage the text – Quick learner May 31 '18 at 13:16
  • check the second answer using this , which shows how to show a dialog without using alert dialog builder – Quick learner May 31 '18 at 13:19
  • https://stackoverflow.com/questions/13341560/how-to-create-a-custom-dialog-box-in-android – Quick learner May 31 '18 at 13:19
  • I would not like to reinvent the wheel, I believe it is not the situation when creation of the custom dialog box would be justified. Anyway, thank you for your time! – Vitalii Isaenko Jun 07 '18 at 21:31