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.v7.preference.DialogPreference.java

/**
 * Sets the icon (resource ID) of the dialog. This will be shown on
 * subsequent dialogs./*w  ww.  ja  v a 2 s.  co m*/
 *
 * @param dialogIconRes The icon, as a resource ID.
 */
public void setDialogIcon(int dialogIconRes) {
    mDialogIcon = ContextCompat.getDrawable(getContext(), dialogIconRes);
}

From source file:com.hookedonplay.decoviewsample.SamplePeopleFragment.java

@Override
protected void setupEvents() {
    final DecoView arcView = getDecoView();
    final View view = getView();
    if (arcView == null || arcView.isEmpty() || view == null) {
        return;//  w w  w .  j  av  a2s .  c om
    }
    arcView.executeReset();

    final ImageView imgView = (ImageView) view.findViewById(R.id.imageViewAvatar);
    imgView.setImageDrawable(null);
    imgView.setVisibility(View.INVISIBLE);

    addAnimation(arcView, mSeries1Index, 19, 3000, imgView, R.drawable.ic_avatar_man, COLOR_BLUE);
    addAnimation(arcView, mSeries2Index, 45, 11000, imgView, R.drawable.ic_avatar_woman, COLOR_PINK);

    arcView.addEvent(
            new DecoEvent.Builder(64).setIndex(mSeries1Index).setDelay(11000).setDuration(5000).build());

    addAnimation(arcView, mSeries3Index, 36, 19000, imgView, R.drawable.ic_avatar_child, COLOR_YELLOW);

    arcView.addEvent(
            new DecoEvent.Builder(79).setIndex(mSeries2Index).setDelay(19000).setDuration(5000).build());

    arcView.addEvent(
            new DecoEvent.Builder(100).setIndex(mSeries1Index).setDelay(19000).setDuration(5000).build());

    arcView.addEvent(new DecoEvent.Builder(EventType.EVENT_COLOR_CHANGE, COLOR_BACK).setIndex(mBack1Index)
            .setDelay(27000).setDuration(2000).setListener(new DecoEvent.ExecuteEventListener() {
                @Override
                public void onEventStart(DecoEvent event) {
                    imgView.setImageDrawable(
                            ContextCompat.getDrawable(getActivity(), R.drawable.ic_avatar_group));
                    showAvatar(true, imgView);
                }

                @Override
                public void onEventEnd(DecoEvent event) {

                }
            }).build());

    addFinishAnimation(arcView, mSeries3Index, 1250, 30000, imgView);
    addFinishAnimation(arcView, mSeries2Index, 1500, 30000, null);
    addFinishAnimation(arcView, mSeries1Index, 1750, 30000, null);
}

From source file:com.artemchep.horario.ui.fragments.details.SubjectTaskDetailsFragment.java

private void setupDecorColor(int color) {
    color |= 0xFF000000; // ignore alpha bits
    final boolean isColorDark = ColorUtil.isColorDark(color);

    Drawable overflowIcon;/*from  w w  w .  ja va 2s  .  c o m*/
    if (isColorDark) {
        mToolbar.setTitleTextColor(Color.WHITE);
        mToolbar.setNavigationIcon(R.drawable.ic_arrow_left_white_24dp);
        overflowIcon = ContextCompat.getDrawable(getContext(), R.drawable.ic_dots_vertical_white_24dp);
        mSmartTabLayout.setDefaultTabTextColor(Color.WHITE);
        mSmartTabLayout.setSelectedIndicatorColors(Color.WHITE);
    } else {
        mToolbar.setTitleTextColor(Color.BLACK);
        mToolbar.setNavigationIcon(R.drawable.ic_arrow_left_black_24dp);
        overflowIcon = ContextCompat.getDrawable(getContext(), R.drawable.ic_dots_vertical_black_24dp);
        mSmartTabLayout.setDefaultTabTextColor(Color.BLACK);
        mSmartTabLayout.setSelectedIndicatorColors(Color.BLACK);
    }

    mAppBar.setBackgroundColor(color);
    mToolbar.setOverflowIcon(overflowIcon);

    mSmartTabLayout.setViewPager(mViewPager);
}

From source file:com.appdevper.mediaplayer.adater.MediaItemViewHolder.java

static View setupView(Activity activity, View convertView, ViewGroup parent, MediaMetadataCompat item,
        int state) {

    if (sColorStateNotPlaying == null || sColorStatePlaying == null) {
        initializeColorStateLists(activity);
    }//www.j a v a 2  s .c  o m

    MediaItemViewHolder holder;

    Integer cachedState = STATE_INVALID;

    if (convertView == null) {
        convertView = LayoutInflater.from(activity).inflate(R.layout.media_list_item, parent, false);
        holder = new MediaItemViewHolder();
        holder.mImageView = (ImageView) convertView.findViewById(R.id.play_eq);
        holder.mTitleView = (TextView) convertView.findViewById(R.id.title);
        holder.mDescriptionView = (TextView) convertView.findViewById(R.id.description);
        convertView.setTag(holder);
    } else {
        holder = (MediaItemViewHolder) convertView.getTag();
        cachedState = (Integer) convertView.getTag(R.id.tag_mediaitem_state_cache);
    }

    holder.mTitleView.setText(item.getString(MediaMetadataCompat.METADATA_KEY_TITLE));
    holder.mDescriptionView.setText(item.getString(MediaMetadataCompat.METADATA_KEY_ALBUM));

    // If the state of convertView is different, we need to adapt the view to the
    // new state.
    if (cachedState == null || cachedState != state) {
        switch (state) {
        case STATE_PLAYABLE:
            Drawable pauseDrawable = ContextCompat.getDrawable(activity, R.drawable.ic_play_arrow_black_36dp);
            DrawableCompat.setTintList(pauseDrawable, sColorStateNotPlaying);
            holder.mImageView.setImageDrawable(pauseDrawable);
            holder.mImageView.setVisibility(View.VISIBLE);
            break;
        case STATE_PLAYING:
            AnimationDrawable animation = (AnimationDrawable) ContextCompat.getDrawable(activity,
                    R.drawable.ic_equalizer_white_36dp);
            DrawableCompat.setTintList(animation, sColorStatePlaying);
            holder.mImageView.setImageDrawable(animation);
            holder.mImageView.setVisibility(View.VISIBLE);
            animation.start();
            break;
        case STATE_PAUSED:
            Drawable playDrawable = ContextCompat.getDrawable(activity, R.drawable.ic_equalizer1_white_36dp);
            DrawableCompat.setTintList(playDrawable, sColorStatePlaying);
            holder.mImageView.setImageDrawable(playDrawable);
            holder.mImageView.setVisibility(View.VISIBLE);
            break;
        default:
            holder.mImageView.setVisibility(View.GONE);
        }
        convertView.setTag(R.id.tag_mediaitem_state_cache, state);
    }

    return convertView;
}

From source file:cn.finalteam.galleryfinal.widget.FloatingActionButton.java

Drawable getIconDrawable() {
    if (mIcon != 0) {
        return ContextCompat.getDrawable(getContext(), mIcon);
    } else {/*from  w ww.  j a v  a2 s  .co m*/
        return new ColorDrawable(Color.TRANSPARENT);
    }
}

From source file:android.support.v7.internal.widget.TintManager.java

public Drawable getDrawable(int resId) {
    Drawable drawable = ContextCompat.getDrawable(mContext, resId);

    if (drawable != null) {
        drawable = drawable.mutate();//w  ww  . j  ava2 s . com

        if (arrayContains(TINT_COLOR_CONTROL_STATE_LIST, resId)) {
            ColorStateList colorStateList = getColorStateListForKnownDrawableId(resId);
            PorterDuff.Mode tintMode = DEFAULT_MODE;
            if (resId == R.drawable.abc_switch_thumb_material) {
                tintMode = PorterDuff.Mode.MULTIPLY;
            }

            if (colorStateList != null) {
                drawable = DrawableCompat.wrap(drawable);
                DrawableCompat.setTintList(drawable, colorStateList);
                DrawableCompat.setTintMode(drawable, tintMode);
            }
        } else if (arrayContains(CONTAINERS_WITH_TINT_CHILDREN, resId)) {
            drawable = mResources.getDrawable(resId);
        } else {
            tintDrawable(resId, drawable);
        }
    }
    return drawable;
}

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

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // if permissions are not already granted, request permission from the user
    if (!(ContextCompat.checkSelfPermission(MainActivity.this,
            reqPermissions[0]) == PackageManager.PERMISSION_GRANTED
            && ContextCompat.checkSelfPermission(MainActivity.this,
                    reqPermissions[1]) == PackageManager.PERMISSION_GRANTED)) {
        int requestCode = 2;
        ActivityCompat.requestPermissions(MainActivity.this, reqPermissions, requestCode);
    }// ww  w. ja v a  2  s.  c  o  m

    // setup the two SearchViews and show text hint
    mPoiSearchView = (SearchView) findViewById(R.id.poi_searchView);
    mPoiSearchView.setIconified(false);
    mPoiSearchView.setFocusable(false);
    mPoiSearchView.setQueryHint(getResources().getString(R.string.search_hint));
    mProximitySearchView = (SearchView) findViewById(R.id.proximity_searchView);
    mProximitySearchView.setIconified(false);
    mProximitySearchView.setFocusable(false);
    mProximitySearchView.setQueryHint(getResources().getString(R.string.proximity_search_hint));
    // setup redo search button
    Button redoSearchButton = (Button) findViewById(R.id.redo_search_button);
    // on redo button click call redoSearchInThisArea
    redoSearchButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            redoSearchInThisArea();
        }
    });

    // define pin drawable
    BitmapDrawable pinDrawable = (BitmapDrawable) ContextCompat.getDrawable(this, R.drawable.pin);
    try {
        mPinSourceSymbol = PictureMarkerSymbol.createAsync(pinDrawable).get();
    } catch (InterruptedException | ExecutionException e) {
        String error = "Error creating PictureMarkerSymbol: " + e.getMessage();
        Log.e(TAG, error);
        Toast.makeText(MainActivity.this, error, Toast.LENGTH_LONG).show();
    }
    // set pin to half of native size
    mPinSourceSymbol.setWidth(19f);
    mPinSourceSymbol.setHeight(72f);

    // instantiate flag proximity search view flag
    mProximitySearchViewEmpty = true;

    // create a LocatorTask from an online service
    mLocatorTask = new LocatorTask(getString(R.string.world_geocode_service));

    // inflate MapView from layout
    mMapView = (MapView) findViewById(R.id.mapView);
    // disable map wraparound
    mMapView.setWrapAroundMode(WrapAroundMode.DISABLED);
    // create a map with the BasemapType topographic
    final ArcGISMap map = new ArcGISMap(Basemap.createTopographic());
    // set the map to be displayed in this view
    mMapView.setMap(map);
    // add listener to update extent when viewpoint has changed
    mMapView.addViewpointChangedListener(new ViewpointChangedListener() {
        @Override
        public void viewpointChanged(ViewpointChangedEvent viewpointChangedEvent) {
            // get the current map extent
            mCurrentExtentGeometry = mMapView.getCurrentViewpoint(Viewpoint.Type.BOUNDING_GEOMETRY)
                    .getTargetGeometry();
        }
    });
    // add listener to handle callouts
    mMapView.setOnTouchListener(new DefaultMapViewOnTouchListener(this, mMapView) {
        @Override
        public boolean onSingleTapConfirmed(MotionEvent motionEvent) {
            showCallout(motionEvent);
            return true;
        }
    });
    // setup and start location display
    mLocationDisplay = mMapView.getLocationDisplay();
    mLocationDisplay.setAutoPanMode(LocationDisplay.AutoPanMode.RECENTER);
    mLocationDisplay.startAsync();
    // initially use device location to focus POI search
    final Point[] currentLocation = new Point[1];
    mLocationDisplay.addLocationChangedListener(new LocationDisplay.LocationChangedListener() {
        @Override
        public void onLocationChanged(LocationDisplay.LocationChangedEvent locationChangedEvent) {
            currentLocation[0] = mLocationDisplay.getMapLocation();
            // only update preferredSearchLocation if device has moved
            if (!currentLocation[0].equals(mLocationDisplay.getMapLocation(), 100)
                    || mPreferredSearchProximity == null) {
                mPreferredSearchProximity = mLocationDisplay.getMapLocation();
            }
        }
    });
    // define the graphics overlay
    mGraphicsOverlay = new GraphicsOverlay();

    setupPoi();
    setupProximity();
}

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

private Drawable getIcon(@DrawableRes int drawable) {
    return ContextCompat.getDrawable(getContext(), drawable).mutate();
}

From source file:com.cicada.yuanxiaobao.common.BaseAdapterHelper.java

/**
 * Will set background of a view.//w  ww. j a  v  a2s. co m
 *
 * @param viewId
 *            The view id.
 * @param drawableId
 *            A drawable to use as a background.
 * @return The BaseAdapterHelper for chaining.
 */
public BaseAdapterHelper setBackgroundDrawable(int viewId, int drawableId) {
    View view = retrieveView(viewId);
    view.setBackground(ContextCompat.getDrawable(context, drawableId));
    return this;
}

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

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

    // Before we do anything else, check for a saved draft; if one exists, load it
    SharedPreferences prefMain = getActivity().getPreferences(Context.MODE_PRIVATE);
    if (getId() == R.id.noteViewEdit && prefMain.getLong("draft-name", 0) != 0) {
        Bundle bundle = new Bundle();
        bundle.putString("filename", "draft");

        Fragment fragment = new NoteEditFragment();
        fragment.setArguments(bundle);/*from w  ww  .  j  ava2 s.  co  m*/

        // Add NoteEditFragment
        getFragmentManager().beginTransaction().replace(R.id.noteViewEdit, fragment, "NoteEditFragment")
                .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_CLOSE).commit();
    } else {
        if (getId() == R.id.noteViewEdit) {
            // Change window title
            String title = getResources().getString(R.string.app_name);

            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);
            }

            // Don't show the Up button in the action bar, and disable the button
            ((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(false);
            ((AppCompatActivity) getActivity()).getSupportActionBar().setHomeButtonEnabled(false);
        }

        // Read preferences
        SharedPreferences pref = getActivity()
                .getSharedPreferences(getActivity().getPackageName() + "_preferences", Context.MODE_PRIVATE);
        theme = pref.getString("theme", "light-sans");
        sortBy = pref.getString("sort_by", "date");
        showDate = pref.getBoolean("show_date", false);
        directEdit = pref.getBoolean("direct_edit", false);

        // Apply theme
        LinearLayout noteViewEdit = getActivity().findViewById(R.id.noteViewEdit);
        LinearLayout noteList = getActivity().findViewById(R.id.noteList);

        if (theme.contains("light")) {
            noteViewEdit.setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.window_background));
            noteList.setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.window_background));
        }

        if (theme.contains("dark")) {
            noteViewEdit
                    .setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.window_background_dark));
            noteList.setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.window_background_dark));
        }

        // Declare ListView
        listView = getActivity().findViewById(R.id.listView1);

        // Refresh list of notes onResume (instead of onCreate) to reflect additions/deletions and preference changes
        listNotes();
    }
}