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:com.android.messaging.util.AccessibilityUtil.java

public static void announceForAccessibilityCompat(final View view,
        @Nullable AccessibilityManager accessibilityManager, final CharSequence text) {
    final Context context = view.getContext().getApplicationContext();
    if (accessibilityManager == null) {
        accessibilityManager = (AccessibilityManager) context.getSystemService(Context.ACCESSIBILITY_SERVICE);
    }/*from w w w  . j a v a  2s. co  m*/

    if (!accessibilityManager.isEnabled()) {
        return;
    }

    // Jelly Bean added support for speaking text verbatim
    final int eventType = OsUtil.isAtLeastJB() ? AccessibilityEvent.TYPE_ANNOUNCEMENT
            : AccessibilityEvent.TYPE_VIEW_FOCUSED;

    // Construct an accessibility event with the minimum recommended
    // attributes. An event without a class name or package may be dropped.
    final AccessibilityEvent event = AccessibilityEvent.obtain(eventType);
    event.getText().add(text);
    event.setEnabled(view.isEnabled());
    event.setClassName(view.getClass().getName());
    event.setPackageName(context.getPackageName());

    // JellyBean MR1 requires a source view to set the window ID.
    final AccessibilityRecordCompat record = AccessibilityEventCompat.asRecord(event);
    record.setSource(view);

    // Sends the event directly through the accessibility manager. If we only supported SDK 14+
    // we could have done:
    // getParent().requestSendAccessibilityEvent(this, event);
    accessibilityManager.sendAccessibilityEvent(event);
}

From source file:com.flexible.flexibleadapter.utils.DrawableUtils.java

/**
 * Helper method to set the background depending on the android version
 *
 * @param view        the view to apply the drawable
 * @param drawableRes drawable resource id
 * @since 5.0.0-b7//w w w .  j  ava  2 s.co m
 * @deprecated Use {@link #setBackgroundCompat(View, int)} instead.
 */
@Deprecated
public static void setBackground(View view, @DrawableRes int drawableRes) {
    setBackgroundCompat(view, getDrawableCompat(view.getContext(), drawableRes));
}

From source file:Main.java

/**
 * Sets up a cheat sheet (tooltip) for the given view by setting its {@link
 * View.OnLongClickListener}. When the view is long-pressed, a {@link Toast} with
 * the given text will be shown either above (default) or below the view (if there isn't room
 * above it).//from   ww w.j a  v  a2  s . c  o m
 *
 * @param view      The view to add a cheat sheet for.
 * @param textResId The string resource containing the text to show on long-press.
 */
public static void setup(View view, final int textResId) {
    view.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View view) {
            return show(view, view.getContext().getString(textResId));
        }
    });
}

From source file:Main.java

public static void traverseAndRecolor(View root, int color, boolean withStates,
        boolean setClickableItemBackgrounds) {
    Context context = root.getContext();

    if (setClickableItemBackgrounds && root.isClickable()) {
        StateListDrawable selectableItemBackground = new StateListDrawable();
        selectableItemBackground.addState(new int[] { android.R.attr.state_pressed },
                new ColorDrawable((color & 0xffffff) | 0x33000000));
        selectableItemBackground.addState(new int[] { android.R.attr.state_focused },
                new ColorDrawable((color & 0xffffff) | 0x44000000));
        selectableItemBackground.addState(new int[] {}, null);
        root.setBackground(selectableItemBackground);
    }/*from  w w w  .  j av a2  s  .  co  m*/

    if (root instanceof ViewGroup) {
        ViewGroup parent = (ViewGroup) root;
        for (int i = 0; i < parent.getChildCount(); i++) {
            traverseAndRecolor(parent.getChildAt(i), color, withStates, setClickableItemBackgrounds);
        }

    } else if (root instanceof ImageView) {
        ImageView imageView = (ImageView) root;
        Drawable sourceDrawable = imageView.getDrawable();
        if (withStates && sourceDrawable != null && sourceDrawable instanceof BitmapDrawable) {
            imageView.setImageDrawable(
                    makeRecoloredDrawable(context, (BitmapDrawable) sourceDrawable, color, true));
        } else {
            imageView.setColorFilter(color, PorterDuff.Mode.MULTIPLY);
        }

    } else if (root instanceof TextView) {
        TextView textView = (TextView) root;
        if (withStates) {
            int sourceColor = textView.getCurrentTextColor();
            ColorStateList colorStateList = new ColorStateList(
                    new int[][] { new int[] { android.R.attr.state_pressed },
                            new int[] { android.R.attr.state_focused }, new int[] {} },
                    new int[] { sourceColor, sourceColor, color });
            textView.setTextColor(colorStateList);
        } else {
            textView.setTextColor(color);
        }

    } else if (root instanceof AnalogClock) {
        AnalogClock analogClock = (AnalogClock) root;
        try {
            Field hourHandField = AnalogClock.class.getDeclaredField("mHourHand");
            hourHandField.setAccessible(true);
            Field minuteHandField = AnalogClock.class.getDeclaredField("mMinuteHand");
            minuteHandField.setAccessible(true);
            Field dialField = AnalogClock.class.getDeclaredField("mDial");
            dialField.setAccessible(true);
            BitmapDrawable hourHand = (BitmapDrawable) hourHandField.get(analogClock);
            if (hourHand != null) {
                Drawable d = makeRecoloredDrawable(context, hourHand, color, withStates);
                d.setCallback(analogClock);
                hourHandField.set(analogClock, d);
            }
            BitmapDrawable minuteHand = (BitmapDrawable) minuteHandField.get(analogClock);
            if (minuteHand != null) {
                Drawable d = makeRecoloredDrawable(context, minuteHand, color, withStates);
                d.setCallback(analogClock);
                minuteHandField.set(analogClock, d);
            }
            BitmapDrawable dial = (BitmapDrawable) dialField.get(analogClock);
            if (dial != null) {
                Drawable d = makeRecoloredDrawable(context, dial, color, withStates);
                d.setCallback(analogClock);
                dialField.set(analogClock, d);
            }
        } catch (NoSuchFieldException ignored) {
        } catch (IllegalAccessException ignored) {
        } catch (ClassCastException ignored) {
        } // TODO: catch all exceptions?
    }
}

From source file:Main.java

/**
 * Chiude la tastiera virtuale./* w w  w  . j  a v a 2  s.c  o  m*/
 * 
 * @param activity Attivit&agrave;.
 */
public static void hideSoftInput(Activity activity) {
    View view;
    InputMethodManager inputMgr;

    if (activity == null) {
        throw new NullPointerException("Argument activity is null.");
    }

    view = activity.getCurrentFocus();
    if (view == null) {
        return;
    }

    inputMgr = (InputMethodManager) view.getContext().getSystemService(Activity.INPUT_METHOD_SERVICE);
    inputMgr.hideSoftInputFromWindow(view.getWindowToken(), 0);
}

From source file:Main.java

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public static void makeCircle(final View view, final int dimenResId) {

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        ViewOutlineProvider viewOutlineProvider = new ViewOutlineProvider() {
            @Override//  w w w  .j  a v a  2 s.  c  om
            public void getOutline(View view, Outline outline) {
                Context context = view.getContext();
                int size = context.getResources().getDimensionPixelSize(dimenResId);
                outline.setOval(0, 0, size, size);
            }
        };

        view.setOutlineProvider(viewOutlineProvider);
    }
}

From source file:com.android.messaging.ui.animation.ViewGroupItemVerticalExplodeAnimation.java

/**
 * Starts a vertical explode animation for a given view situated in a given container.
 *
 * @param container the container of the view which determines the explode animation's final
 *        size/*from w  w  w .j a  v a2 s. c  o m*/
 * @param viewToAnimate the view to be animated. The view will be highlighted by the explode
 *        highlight, which expands from the size of the view to the size of the container.
 * @param animationStagingView the view that stages the animation. Since viewToAnimate may be
 *        removed from the view tree during the animation, we need a view that'll be alive
 *        for the duration of the animation so that the animation won't get cancelled.
 * @param snapshotView whether a snapshot of the view to animate is needed.
 */
public static void startAnimationForView(final ViewGroup container, final View viewToAnimate,
        final View animationStagingView, final boolean snapshotView, final int duration) {
    if (OsUtil.isAtLeastJB_MR2() && (viewToAnimate.getContext() instanceof Activity)) {
        new ViewExplodeAnimationJellyBeanMR2(viewToAnimate, container, snapshotView, duration).startAnimation();
    } else {
        // Pre JB_MR2, this animation can cause rendering failures which causes the framework
        // to fall back to software rendering where camera preview isn't supported (b/18264647)
        // just skip the animation to avoid this case.
    }
}

From source file:com.appeaser.sublimepickerlibrary.utilities.AccessibilityUtils.java

public static void makeAnnouncement(View view, CharSequence announcement) {
    if (view == null)
        return;/*from  w w w  .  ja v  a  2 s .  c o  m*/
    if (SUtils.isApi_16_OrHigher()) {
        view.announceForAccessibility(announcement);
    } else {
        // For API 15 and earlier, we need to construct an accessibility event
        Context ctx = view.getContext();
        AccessibilityManager am = (AccessibilityManager) ctx.getSystemService(Context.ACCESSIBILITY_SERVICE);
        if (!am.isEnabled())
            return;

        AccessibilityEvent event = AccessibilityEvent
                .obtain(AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED);
        AccessibilityRecordCompat arc = AccessibilityEventCompat.asRecord(event);
        arc.setSource(view);
        event.setClassName(view.getClass().getName());
        event.setPackageName(view.getContext().getPackageName());
        event.setEnabled(view.isEnabled());
        event.getText().add(announcement);
        am.sendAccessibilityEvent(event);
    }
}

From source file:com.android.gallery3d.util.AccessibilityUtils.java

public static void makeAnnouncement(View view, CharSequence announcement) {
    if (view == null)
        return;/*from w  w  w . ja v a  2  s. c  o  m*/
    if (ApiHelper.HAS_ANNOUNCE_FOR_ACCESSIBILITY) {
        view.announceForAccessibility(announcement);
    } else {
        // For API 15 and earlier, we need to construct an accessibility event
        Context ctx = view.getContext();
        AccessibilityManager am = (AccessibilityManager) ctx.getSystemService(Context.ACCESSIBILITY_SERVICE);
        if (!am.isEnabled())
            return;
        AccessibilityEvent event = AccessibilityEvent
                .obtain(AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED);
        AccessibilityRecordCompat arc = new AccessibilityRecordCompat(event);
        arc.setSource(view);
        event.setClassName(view.getClass().getName());
        event.setPackageName(view.getContext().getPackageName());
        event.setEnabled(view.isEnabled());
        event.getText().add(announcement);
        am.sendAccessibilityEvent(event);
    }
}

From source file:com.aoppp.gatewaymaster.dialogs.DialogUtil.java

/**
 * ?View?.//from  w w w.ja  v  a 2 s  . c o m
 * @param icon
 * @param title ?
 * @param view ???
 * @param onClickListener ?
 */
public static AlertDialogFragment showAlertDialog(int icon, String title, View view,
        AlertDialogFragment.DialogOnClickListener onClickListener) {
    FragmentActivity activity = (FragmentActivity) view.getContext();
    AlertDialogFragment newFragment = AlertDialogFragment.newInstance(icon, title, null, view, onClickListener);
    FragmentTransaction ft = activity.getFragmentManager().beginTransaction();
    //    
    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
    newFragment.show(ft, mDialogTag);
    return newFragment;
}