Android talkback not reading the content of the dialog box

Viewed 3585

I have tried to show the dialog box while the user giving the wrong username or password, using the below code.

private void showAlert(String title, String msg) {
    customDialog = new Dialog(LoginActivity.this,
            android.R.style.Theme_Dialog);
    customDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    customDialog.setContentView(R.layout.custom_alert_dialog);

    tvTitle = (TextView) customDialog
            .findViewById(R.id.dialog_title);
    tvMsg = (TextView) customDialog
            .findViewById(R.id.dialog_message);
    btnNeutral = (Button) customDialog
            .findViewById(R.id.closeAlert);

    tvMsg.setText(msg);
    tvTitle.setText(title);

    tvMsg.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
    tvMsg.setFocusable(true);

    btnNeutral.setText("Close");
    btnNeutral.setVisibility(View.VISIBLE);
    btnNeutral.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            customDialog.dismiss();
        }
    });
    customDialog.show();
    tvMsg.requestFocus();
}

The code working fine but my concern is, when i am trying to use the android talkback. It reads only the title of the dialog box. The talkback needs to read the content(message) of the dialog box instead of title. Can anyone help me to do this?

1 Answers

First, announcing just the title of a new dialog is very standard. Doing otherwise would probably be counter productive in terms of accessibility. This sounds to me like an accessibility requirement from someone motivated to do good, that doesn't really understand the needs of users with disabilities. Shoving focus around arbitrarily is usually bad. Let the operating system do what it wants with focus, it is what Assistive Technology (TalkBack) users will be accustomed to.

This said there are two overarching issues with your code. First, when you say focus, you mean accessibility focus.

tvMsg.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
tvMsg.setFocusable(true);
tvMsg.requestFocus();

All of these lines are referring to keyboard, or input focus, none of which are particularly meaningful for a TextView. These are only meaningful for active elements like Buttons and EditText boxes. Will this work if you do it correctly, yes. But, it comes with awkward side effects, like a TextView being added to Tab ordering, which is awkward for Keyboard only users, because TextViews don't have focus highlights, so Focus navigation disappears. What you really want is the following event type:

AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED

Now, for the second point. You're doing all of this before your view actually renders. Replace this line:

tvMsg.requestFocus();

With this line:

tvMsg.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED);

Delete the other lines mentioned above, and you should be golden. Though, again, my ultimate recommendation would just be dropping all of this, and removing those three lines outright, and forgetting about this. Let the operating system do its thing!

Related