request Accessibility Focus - Android User Interface

Android examples for User Interface:AccessibilityEvent

Description

request Accessibility Focus

Demo Code

import android.graphics.Rect;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

public class Main {
  private static final Rect sRectForHit = new Rect();

  public static View requestAccessibilityFocus(ViewGroup viewGroup, MotionEvent motionEvent) {
    View view = searchContentDescribedView(viewGroup, motionEvent);
    if (view != null) {
      view.performAccessibilityAction(64, null);
      return view;
    }/*from   w  w  w . ja  v  a2s  .co m*/
    viewGroup.performAccessibilityAction(64, null);
    return view;
  }

  private static View searchContentDescribedView(ViewGroup viewGroup, MotionEvent motionEvent) {
    if (viewGroup == null || motionEvent == null) {
      return null;
    }
    int n = (int) motionEvent.getX();
    int n2 = (int) motionEvent.getY();
    int n3 = -1 + viewGroup.getChildCount();
    while (n3 >= 0) {
      View view;
      View view2 = viewGroup.getChildAt(n3);
      if (checkToTouch(view2, n, n2) && (view = view2.getContentDescription() != null ? view2
          : (view2 instanceof ViewGroup ? searchContentDescribedView((ViewGroup) view2, motionEvent) : null)) != null) {
        return view;
      }
      --n3;
    }
    return null;
  }

  private static boolean checkToTouch(View view, int n, int n2) {
    if (!(view != null && view.getVisibility() == 0 && view.getGlobalVisibleRect(sRectForHit)
        && sRectForHit.contains(n, n2))) {
      return false;
    }
    return true;
  }
}

Related Tutorials