Example usage for android.support.v4.content ContextCompat getDrawable

List of usage examples for android.support.v4.content ContextCompat getDrawable

Introduction

In this page you can find the example usage for android.support.v4.content ContextCompat getDrawable.

Prototype

public static final Drawable getDrawable(Context context, int i) 

Source Link

Usage

From source file:android.support.design.widget.NavigationView.java

/**
 * Set the background of our menu items to the given resource.
 *
 * @param resId The identifier of the resource.
 *
 * @attr ref R.styleable#NavigationView_itemBackground
 *///  ww  w  .j  a va  2 s  . c  o m
public void setItemBackgroundResource(@DrawableRes int resId) {
    setItemBackground(ContextCompat.getDrawable(getContext(), resId));
}

From source file:cnedu.ustcjd.widget.MultiSlider.java

public MultiSlider(Context context, AttributeSet attrs, int defStyle, int styleRes) {
    super(context, attrs, defStyle);
    if ((Build.VERSION.SDK_INT >= 21) && getBackground() == null) {
        setBackgroundResource(R.drawable.control_background_multi_material);
    }/* w w w. j  ava2 s  .c  om*/

    mUiThreadId = Thread.currentThread().getId();

    a = context.obtainStyledAttributes(attrs, R.styleable.MultiSlider, defStyle, styleRes);
    mNoInvalidate = true;
    int numThumbs = a.getInt(R.styleable.MultiSlider_thumbNumber, 2);
    initMultiSlider(numThumbs);

    Drawable trackDrawable = a.getDrawable(R.styleable.MultiSlider_android_track);
    if (trackDrawable == null) {
        trackDrawable = ContextCompat.getDrawable(getContext(), R.drawable.multislider_track_material);
    }

    setTrackDrawable(getTintedDrawable(trackDrawable, a.getColor(R.styleable.MultiSlider_trackColor, 0)));

    //TODO
    //        mMinWidth = a.getDimensionPixelSize(R.styleable.MultiSlider_minWidth, mMinWidth);
    //        mMaxWidth = a.getDimensionPixelSize(R.styleable.MultiSlider_maxWidth, mMaxWidth);
    //        mMinHeight = a.getDimensionPixelSize(R.styleable.MultiSlider_minHeight, mMinHeight);
    //        mMaxHeight = a.getDimensionPixelSize(R.styleable.MultiSlider_maxHeight, mMaxHeight);

    setStep(a.getInt(R.styleable.MultiSlider_scaleStep, mStep));
    setStepsThumbsApart(a.getInt(R.styleable.MultiSlider_stepsThumbsApart, mStepsThumbsApart));
    setDrawThumbsApart(a.getBoolean(R.styleable.MultiSlider_drawThumbsApart, mDrawThumbsApart));
    setMax(a.getInt(R.styleable.MultiSlider_scaleMax, mScaleMax), true);
    setMin(a.getInt(R.styleable.MultiSlider_scaleMin, mScaleMin), true);

    mMirrorForRtl = a.getBoolean(R.styleable.MultiSlider_mirrorForRTL, mMirrorForRtl);

    // --> now place thumbs

    defThumbDrawable = a.getDrawable(R.styleable.MultiSlider_android_thumb);

    if (defThumbDrawable == null) {
        if (Build.VERSION.SDK_INT >= 21) {
            defThumbDrawable = ContextCompat.getDrawable(getContext(),
                    R.drawable.multislider_thumb_material_anim);
        } else {
            defThumbDrawable = ContextCompat.getDrawable(getContext(), R.drawable.multislider_thumb_material);
        }
    }

    defRangeDrawable = a.getDrawable(R.styleable.MultiSlider_range);
    if (defRangeDrawable == null) {
        defRangeDrawable = ContextCompat.getDrawable(getContext(), R.drawable.multislider_range_material);
    }

    Drawable range1Drawable = a.getDrawable(R.styleable.MultiSlider_range1);
    Drawable range2Drawable = a.getDrawable(R.styleable.MultiSlider_range2);

    defRangeColor = a.getColor(R.styleable.MultiSlider_rangeColor, 0);
    defThumbColor = a.getColor(R.styleable.MultiSlider_thumbColor, 0);
    setThumbDrawables(defThumbDrawable, defRangeDrawable, range1Drawable, range2Drawable); //
    // will
    // guess thumbOffset if
    // thumb != null...
    // ...but allow layout to override this

    int thumbOffset = a.getDimensionPixelOffset(R.styleable.MultiSlider_android_thumbOffset,
            defThumbDrawable.getIntrinsicWidth() / 2);
    setThumbOffset(thumbOffset);

    repositionThumbs();

    mScaledTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
    mNoInvalidate = false;
    a.recycle();
}

From source file:com.filemanager.free.fragments.Main.java

@SuppressLint("InflateParams")
@Override/*from   ww  w .  j  av a 2 s .  c  o  m*/
public void onActivityCreated(final Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    setHasOptionsMenu(false);
    //MAIN_ACTIVITY = (MainActivity) getActivity();
    initNoFileLayout();
    utils = new Futils();
    String x = PreferenceUtils.getSelectionColor(skin);
    skinselection = Color.parseColor(x);
    color = PreferenceUtils.calculatevalues(x);
    ColorMatrix colorMatrix = new ColorMatrix(PreferenceUtils.calculatefilter(color));
    colorMatrixColorFilter = new ColorMatrixColorFilter(colorMatrix);
    ROOT_MODE = Sp.getBoolean("rootmode", false);
    SHOW_HIDDEN = Sp.getBoolean("showHidden", false);
    COLORISE_ICONS = Sp.getBoolean("coloriseIcons", true);
    folder = ContextCompat.getDrawable(getContext(), R.drawable.ic_grid_folder_new);
    getSortModes();
    DARK_IMAGE = ContextCompat.getDrawable(getContext(), R.drawable.ic_doc_image_dark);
    DARK_VIDEO = ContextCompat.getDrawable(getContext(), R.drawable.ic_doc_video_dark);
    this.setRetainInstance(false);
    f = new HFile(HFile.UNKNOWN, CURRENT_PATH);
    f.generateMode(getActivity());
    MAIN_ACTIVITY.initiatebbar();
    IS_LIST = savedInstanceState != null ? savedInstanceState.getBoolean("IS_LIST", IS_LIST) : IS_LIST;
    ic = new IconHolder(getActivity(), SHOW_THUMBS, !IS_LIST);
    if (theme1 == 1) {
        listView.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.holo_dark_background));
    } else {
        if (IS_LIST)
            listView.setBackgroundColor(ContextCompat.getColor(getContext(), android.R.color.background_light));
    }
    listView.setHasFixedSize(true);
    columns = Integer.parseInt(Sp.getString("columns", "-1"));
    if (IS_LIST) {
        mLayoutManager = new LinearLayoutManager(getActivity());
        listView.setLayoutManager(mLayoutManager);
    } else {
        if (columns == -1 || columns == 0)
            mLayoutManagerGrid = new GridLayoutManager(getActivity(), 3);
        else
            mLayoutManagerGrid = new GridLayoutManager(getActivity(), columns);
        listView.setLayoutManager(mLayoutManagerGrid);
    }

    // use a linear layout manager
    footerView = getActivity().getLayoutInflater().inflate(R.layout.divider, null);
    dividerItemDecoration = new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL_LIST, false,
            SHOW_DIVIDERS);
    listView.addItemDecoration(dividerItemDecoration);
    mSwipeRefreshLayout.setColorSchemeColors(Color.parseColor(fabSkin));
    DefaultItemAnimator animator = new DefaultItemAnimator();
    listView.setItemAnimator(animator);
    mToolbarContainer.getViewTreeObserver()
            .addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    if ((columns == 0 || columns == -1)) {
                        int screen_width = listView.getWidth();
                        int dptopx = dpToPx(115);
                        columns = screen_width / dptopx;
                        if (columns == 0 || columns == -1)
                            columns = 3;
                        if (!IS_LIST)
                            mLayoutManagerGrid.setSpanCount(columns);
                    }
                    if (savedInstanceState != null && !IS_LIST)
                        retrieveFromSavedInstance(savedInstanceState);
                    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
                        mToolbarContainer.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                    } else {
                        mToolbarContainer.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                    }
                }

            });
    if (savedInstanceState == null) {
        loadlist(CURRENT_PATH, false, openMode);

    } else {
        if (IS_LIST)
            retrieveFromSavedInstance(savedInstanceState);
    }
}

From source file:com.goka.flickableview.ImageViewTouchBase.java

@Override
public void setImageResource(int resId) {
    setImageDrawable(ContextCompat.getDrawable(getContext(), resId));
}

From source file:android.support.v7.widget.AppCompatSpinner.java

public void setPopupBackgroundResource(@DrawableRes int resId) {
    setPopupBackgroundDrawable(ContextCompat.getDrawable(getPopupContext(), resId));
}

From source file:com.esri.arcgisruntime.sample.featurelayerupdateattributes.MainActivity.java

/**
 * Displays Callout//from   w  w w. j  a v  a  2 s.  c  om
 * @param title the text to show in the Callout
 */
private void showCallout(String title) {

    // create a text view for the callout
    RelativeLayout calloutLayout = new RelativeLayout(getApplicationContext());

    TextView calloutContent = new TextView(getApplicationContext());
    calloutContent.setId(R.id.textview);
    calloutContent.setTextColor(Color.BLACK);
    calloutContent.setTextSize(18);
    calloutContent.setPadding(0, 10, 10, 0);

    calloutContent.setText(title);

    RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    relativeParams.addRule(RelativeLayout.RIGHT_OF, calloutContent.getId());

    // create image view for the callout
    ImageView imageView = new ImageView(getApplicationContext());
    imageView.setImageDrawable(
            ContextCompat.getDrawable(getApplicationContext(), R.drawable.ic_info_outline_black_18dp));
    imageView.setLayoutParams(relativeParams);
    imageView.setOnClickListener(new ImageViewOnclickListener());

    calloutLayout.addView(calloutContent);
    calloutLayout.addView(imageView);

    mCallout.setLocation(mMapView.screenToLocation(mClickPoint));
    mCallout.setContent(calloutLayout);
    mCallout.show();
}

From source file:com.github.andrewlord1990.materialandroid.component.textfield.PasswordEditText.java

/**
 * Set the drawable to use for the visibility toggle when the password is currently hidden and being displayed as
 * asterisk characters./*  ww w  .jav a2  s.  c  o m*/
 *
 * @param hiddenDrawableRes Drawable to use for visibility toggle whilst password is being hidden.
 */
public void setHiddenDrawable(@DrawableRes int hiddenDrawableRes) {
    hiddenIcon = ContextCompat.getDrawable(getContext(), hiddenDrawableRes);
    setPasswordVisibility();
}

From source file:com.farmerbb.notepad.fragment.NoteEditFragment.java

@Override
public void onResume() {
    super.onResume();

    SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
    // Disable restoring drafts if user launched Notepad through a share intent
    if (!listener.isShareIntent()) {
        if (filename.equals("draft")) {

            // Restore draft preferences
            draftName = sharedPref.getLong("draft-name", 0);
            isSavedNote = sharedPref.getBoolean("is-saved-note", false);

            // Restore filename of draft
            filename = Long.toString(draftName);

            // Reload old file into memory, so that correct contentsOnLoad is set
            if (isSavedNote) {
                try {
                    contentsOnLoad = listener.loadNote(filename);
                } catch (IOException e) {
                    showToast(R.string.error_loading_note);
                    finish(null);//from  w ww  .j  a va  2s.co m
                }
            } else
                contentsOnLoad = "";

            // Notify the user that a draft has been restored
            showToast(R.string.draft_restored);
        }

        // Clear draft preferences
        SharedPreferences.Editor editor = sharedPref.edit();
        editor.remove("draft-name");
        editor.remove("is-saved-note");
        editor.remove("draft-contents");
        editor.apply();
    }

    // Change window title
    String title;

    if (isSavedNote)
        try {
            title = listener.loadNoteTitle(filename);
        } catch (IOException e) {
            title = getResources().getString(R.string.edit_note);
        }
    else
        title = getResources().getString(R.string.action_new);

    getActivity().setTitle(title);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        Bitmap bitmap = ((BitmapDrawable) ContextCompat.getDrawable(getActivity(), R.drawable.ic_recents_logo))
                .getBitmap();

        ActivityManager.TaskDescription taskDescription = new ActivityManager.TaskDescription(title, bitmap,
                ContextCompat.getColor(getActivity(), R.color.primary));
        getActivity().setTaskDescription(taskDescription);
    }

    SharedPreferences pref = getActivity().getSharedPreferences(getActivity().getPackageName() + "_preferences",
            Context.MODE_PRIVATE);
    directEdit = pref.getBoolean("direct_edit", false);
}

From source file:com.afeng.xf.widget.snackbarlight.Light.java

/**
 * Make a customized {@link Snackbar} to display a message without any action.
 * Method {@link Light#make(View, CharSequence, Drawable, int, int, int)}
 *
 * @param view The view to find a parent from.
 * @param textRes The message to display. Formatted text is supported.
 *                String id required./*from w  ww .j a va2  s.  com*/
 * @param textIconRes The left icon of the message. Drawable resource id required.
 * @param backgroundColorRes The background color of the Snackbar. Color resource id required.
 * @param textColorRes The color of action message text. Color resource id required.
 * @param duration How long to show the message.
 *                 Either {@link Light#LENGTH_SHORT} or {@link Light#LENGTH_LONG}.
 * @return The customized Snackbar that will be displayed.
 */
public static Snackbar make(@NonNull View view, @StringRes int textRes, @DrawableRes int textIconRes,
        @ColorRes int backgroundColorRes, @ColorRes int textColorRes, int duration) {
    Context context = view.getContext();
    return make(view, context.getString(textRes),
            // DO NOT use the resource id directly.
            // It should be a resolved drawable or color.
            ContextCompat.getDrawable(context, textIconRes),
            // getResources().getColor() is deprecated.
            ContextCompat.getColor(context, backgroundColorRes), ContextCompat.getColor(context, textColorRes),
            duration);
}

From source file:com.github.andrewlord1990.materialandroid.component.list.ListItemView.java

/**
 * Set the avatar to display at the start (on the left).
 *
 * @param avatarRes The avatar to display.
 *///from   w w w  .  ja  v  a  2s  .c  o  m
public void setAvatar(@DrawableRes int avatarRes) {
    setAvatar(ContextCompat.getDrawable(getContext(), avatarRes));
}