Java Code Examples for android.widget.TextView#announceForAccessibility()

The following examples show how to use android.widget.TextView#announceForAccessibility() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AccountChooserDialog.java    From 365browser with Apache License 2.0 4 votes vote down vote up
private void showTooltip(View view, String message, int layoutId) {
    Context context = view.getContext();
    Resources resources = context.getResources();
    LayoutInflater inflater = LayoutInflater.from(context);

    TextView text = (TextView) inflater.inflate(layoutId, null);
    text.setText(message);
    text.announceForAccessibility(message);

    // This is a work-around for a bug on Android versions KitKat and below
    // (http://crbug.com/693076). The tooltip wouldn't be shown otherwise.
    if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.LOLLIPOP) {
        text.setSingleLine(false);
    }

    // The tooltip should be shown above and to the left (right for RTL) of the info button.
    // In order to do so the tooltip's location on the screen is determined. This location is
    // specified with regard to the top left corner and ignores RTL layouts. For this reason the
    // location of the tooltip is also specified as offsets to the top left corner of the
    // screen. Since the tooltip should be shown above the info button, the height of the
    // tooltip needs to be measured. Furthermore, the height of the statusbar is ignored when
    // obtaining the icon's screen location, but must be considered when specifying a y offset.
    // In addition, the measured width is needed in LTR layout, so that the right end of the
    // tooltip aligns with the right end of the info icon.
    final int[] screenPos = new int[2];
    view.getLocationOnScreen(screenPos);

    text.measure(MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
            MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));

    final int width = view.getWidth();

    final int xOffset = ApiCompatibilityUtils.isLayoutRtl(view)
            ? screenPos[0]
            : screenPos[0] + width - text.getMeasuredWidth();

    final int statusBarHeightResourceId =
            resources.getIdentifier("status_bar_height", "dimen", "android");

    final int statusBarHeight = statusBarHeightResourceId > 0
            ? resources.getDimensionPixelSize(statusBarHeightResourceId)
            : 0;

    final int tooltipMargin = resources.getDimensionPixelSize(R.dimen.psl_info_tooltip_margin);

    final int yOffset =
            screenPos[1] - tooltipMargin - statusBarHeight - text.getMeasuredHeight();

    // The xOffset is with regard to the left edge of the screen. Gravity.LEFT is deprecated,
    // which is why the following line is necessary.
    final int xGravity = ApiCompatibilityUtils.isLayoutRtl(view) ? Gravity.END : Gravity.START;

    Toast toast = new Toast(context);
    toast.setGravity(Gravity.TOP | xGravity, xOffset, yOffset);
    toast.setDuration(Toast.LENGTH_SHORT);
    toast.setView(text);
    toast.show();
}