Skip to content

Commit

Permalink
Fix Android AlertFragment Title Accessibility (facebook#45048)
Browse files Browse the repository at this point in the history
Summary:
[Android] [Fix] - Fix AlertDialog title accessibility
Pull Request resolved: facebook#45048

Making title accessible for android AlertFragment

Reviewed By: blavalla

Differential Revision: D58684576
  • Loading branch information
Susan Chen authored and facebook-github-bot committed Jun 25, 2024
1 parent 4a8f0ee commit dd4ec34
Showing 1 changed file with 44 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,18 @@
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.os.Build;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.View;
import android.widget.TextView;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.core.view.AccessibilityDelegateCompat;
import androidx.core.view.ViewCompat;
import androidx.core.view.accessibility.AccessibilityNodeInfoCompat;
import androidx.fragment.app.DialogFragment;
import com.facebook.infer.annotation.Nullsafe;

Expand Down Expand Up @@ -66,14 +74,47 @@ private static boolean isAppCompatTheme(Context activityContext) {
return isAppCompat;
}

/**
* Creates the dialog title custom TextView that has role of "Heading" and focusable for
* accessibility purpose.
*
* @returns accessible TextView title
*/
private static TextView getAccessibleTitle(Context activityContext, Bundle arguments) {
TextView accessibleTitle = new TextView(activityContext);
accessibleTitle.setText(arguments.getString(ARG_TITLE));
accessibleTitle.setFocusable(true);
accessibleTitle.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18);
Resources r = activityContext.getResources();
int paddingInPx =
(int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 24, r.getDisplayMetrics());
accessibleTitle.setPadding(paddingInPx, paddingInPx, paddingInPx, 0);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
accessibleTitle.setAccessibilityHeading(true);
} else {
ViewCompat.setAccessibilityDelegate(
accessibleTitle,
new AccessibilityDelegateCompat() {
@Override
public void onInitializeAccessibilityNodeInfo(
View view, AccessibilityNodeInfoCompat info) {
super.onInitializeAccessibilityNodeInfo(accessibleTitle, info);
info.setHeading(true);
}
});
}
return accessibleTitle;
}

/**
* Creates a dialog compatible only with AppCompat activities. This function should be kept in
* sync with {@link createAppDialog}.
*/
private static Dialog createAppCompatDialog(
Context activityContext, Bundle arguments, DialogInterface.OnClickListener fragment) {
TextView accessibleTitle = getAccessibleTitle(activityContext, arguments);
AlertDialog.Builder builder =
new AlertDialog.Builder(activityContext).setTitle(arguments.getString(ARG_TITLE));
new AlertDialog.Builder(activityContext).setCustomTitle(accessibleTitle);

if (arguments.containsKey(ARG_BUTTON_POSITIVE)) {
builder.setPositiveButton(arguments.getString(ARG_BUTTON_POSITIVE), fragment);
Expand Down Expand Up @@ -104,9 +145,9 @@ private static Dialog createAppCompatDialog(
*/
private static Dialog createAppDialog(
Context activityContext, Bundle arguments, DialogInterface.OnClickListener fragment) {
TextView accessibleTitle = getAccessibleTitle(activityContext, arguments);
android.app.AlertDialog.Builder builder =
new android.app.AlertDialog.Builder(activityContext)
.setTitle(arguments.getString(ARG_TITLE));
new android.app.AlertDialog.Builder(activityContext).setCustomTitle(accessibleTitle);

if (arguments.containsKey(ARG_BUTTON_POSITIVE)) {
builder.setPositiveButton(arguments.getString(ARG_BUTTON_POSITIVE), fragment);
Expand Down

0 comments on commit dd4ec34

Please sign in to comment.