Example usage for android.view View getContext

List of usage examples for android.view View getContext

Introduction

In this page you can find the example usage for android.view View getContext.

Prototype

@ViewDebug.CapturedViewProperty
public final Context getContext() 

Source Link

Document

Returns the context the view is running in, through which it can access the current theme, resources, etc.

Usage

From source file:Main.java

@Nullable
public static Activity tryGetActivity(View view) {
    if (view == null) {
        return null;
    }//from  w w  w  .j a va 2  s .c om

    Context context = view.getContext();

    Activity activityFromContext = tryGetActivity(context);
    if (activityFromContext != null) {
        return activityFromContext;
    }

    ViewParent parent = view.getParent();
    if (parent instanceof View) {
        View parentView = (View) parent;
        return tryGetActivity(parentView);
    }

    return null;
}

From source file:Main.java

/**
 * @param top//www .ja  v  a  2  s.  c o m
 * @param left
 * @param bottom
 * @param right
 * @param delegate
 */
public static void increaseHitRectBy(final int top, final int left, final int bottom, final int right,
        final View delegate) {
    final View parent = (View) delegate.getParent();
    if (parent != null && delegate.getContext() != null) {
        parent.post(new Runnable() {
            // Post in the parent's message queue to make sure the parent
            // lays out its children before we call getHitRect()
            public void run() {
                final float densityDpi = delegate.getContext().getResources().getDisplayMetrics().densityDpi;
                final Rect r = new Rect();
                delegate.getHitRect(r);
                r.top -= transformToDensityPixel(top, densityDpi);
                r.left -= transformToDensityPixel(left, densityDpi);
                r.bottom += transformToDensityPixel(bottom, densityDpi);
                r.right += transformToDensityPixel(right, densityDpi);
                parent.setTouchDelegate(new TouchDelegate(r, delegate));
            }
        });
    }
}

From source file:Main.java

public static void hideKeyboard(View view) {
    if (view == null) {
        return;/*w w w  .j  av  a2s .  com*/
    }
    try {
        InputMethodManager imm = (InputMethodManager) view.getContext()
                .getSystemService(Context.INPUT_METHOD_SERVICE);
        if (!imm.isActive()) {
            return;
        }
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void setSoftInputVisible(final View content, boolean visible, int delay) {
    final InputMethodManager imm = (InputMethodManager) content.getContext()
            .getSystemService(Context.INPUT_METHOD_SERVICE);

    if (visible) {
        if (delay > 0) {
            new Handler().postDelayed(new Runnable() {

                @Override//from w  w w . ja v  a  2  s.  c om
                public void run() {
                    imm.showSoftInput(content, 0);
                }
            }, delay);
        } else {
            imm.showSoftInput(content, 0);
        }

    } else {
        if (delay > 0) {
            new Handler().postDelayed(new Runnable() {

                @Override
                public void run() {
                    imm.hideSoftInputFromWindow(content.getWindowToken(), 0);
                }
            }, delay);
        } else {
            imm.hideSoftInputFromWindow(content.getWindowToken(), 0);
        }

    }
}

From source file:Main.java

public static void showKeyboard(View view) {
    if (view == null) {
        return;//from   w w w  .  j  a v  a2 s  .  c  om
    }
    try {
        InputMethodManager inputManager = (InputMethodManager) view.getContext()
                .getSystemService(Context.INPUT_METHOD_SERVICE);
        inputManager.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static boolean isKeyboardShowed(View view) {
    if (view == null) {
        return false;
    }/*w w w  . ja v a  2s .c  o  m*/
    try {
        InputMethodManager inputManager = (InputMethodManager) view.getContext()
                .getSystemService(Context.INPUT_METHOD_SERVICE);
        return inputManager.isActive(view);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return false;
}

From source file:Main.java

public static void startAnimation(View target, int aniResId, int duration) {
    if (target == null)
        return;/*from   www .  j a v  a  2 s  .  com*/

    Animation animation = AnimationUtils.loadAnimation(target.getContext(), aniResId);
    if (animation != null) {
        animation.setDuration(duration);
        target.startAnimation(animation);
    }
}

From source file:Main.java

public static void closeSoftKeyboard(View view) {
    if (view == null || view.getWindowToken() == null) {
        return;/*from   w  ww  .j a v  a  2s. com*/
    }
    InputMethodManager imm = (InputMethodManager) view.getContext()
            .getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}

From source file:Main.java

public static void softKeyboardDelayed(final View view) {
    view.post(new Runnable() {
        @Override//from ww  w  .j a v a2 s  .  c o m
        public void run() {
            if (!isHardwareKeyboardAvailable(view.getContext())) {
                InputMethodManager imm = (InputMethodManager) view.getContext()
                        .getSystemService(Context.INPUT_METHOD_SERVICE);
                if (imm != null) {
                    imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
                }
            }
        }
    });
}

From source file:Main.java

public static void startAnimation(View target, int aniResId, AnimationListener listener) {
    if (target == null)
        return;//from  w  ww.  ja  v a  2  s  .com

    Animation animation = AnimationUtils.loadAnimation(target.getContext(), aniResId);
    if (animation != null) {
        if (listener != null) {
            animation.setAnimationListener(listener);
        }
        target.startAnimation(animation);
    }
}