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:com.grarak.kerneladiutor.views.recyclerview.downloads.DownloadKernelView.java

@Override
public void onCreateView(View view) {
    super.onCreateView(view);

    if (mDownloadDrawable == null) {
        mDownloadDrawable = ContextCompat.getDrawable(view.getContext(), R.drawable.ic_download);
        DrawableCompat.setTint(mDownloadDrawable, Color.WHITE);
    }/*from  w  ww.j  a  v a  2 s .com*/
    if (mCancelDrawable == null) {
        mCancelDrawable = ContextCompat.getDrawable(view.getContext(), R.drawable.ic_cancel);
        DrawableCompat.setTint(mCancelDrawable, Color.WHITE);
    }

    final TextView title = (TextView) view.findViewById(R.id.title);
    TextView summary = (TextView) view.findViewById(R.id.summary);
    TextView changelog = (TextView) view.findViewById(R.id.changelog);
    mDownloadSection = view.findViewById(R.id.downloadSection);
    mProgressParent = view.findViewById(R.id.progressParent);
    mProgressText = (TextView) view.findViewById(R.id.progressText);
    mProgressBar = (ProgressBar) view.findViewById(R.id.progressbar);
    mFabButton = (FloatingActionButton) view.findViewById(R.id.fab_button);
    mCheckMD5 = view.findViewById(R.id.checkmd5);
    mMismatchMD5 = view.findViewById(R.id.md5_mismatch);
    mInstallButton = view.findViewById(R.id.install);

    final CharSequence titleText = Utils.htmlFrom(mDownload.getName());
    title.setText(titleText);
    title.setMovementMethod(LinkMovementMethod.getInstance());

    String description = mDownload.getDescription();
    if (description != null) {
        summary.setText(Utils.htmlFrom(description));
        summary.setMovementMethod(LinkMovementMethod.getInstance());
    } else {
        summary.setVisibility(View.GONE);
    }

    List<String> changelogs = mDownload.getChangelogs();
    if (changelogs.size() > 0) {
        StringBuilder stringBuilder = new StringBuilder();
        for (String change : changelogs) {
            if (stringBuilder.length() == 0) {
                stringBuilder.append("\u2022").append(" ").append(change);
            } else {
                stringBuilder.append("<br>").append("\u2022").append(" ").append(change);
            }
        }
        changelog.setText(Utils.htmlFrom(stringBuilder.toString()));
        changelog.setMovementMethod(LinkMovementMethod.getInstance());
    } else {
        changelog.setVisibility(View.GONE);
    }

    mMismatchMD5.setVisibility(View.GONE);
    mInstallButton.setVisibility(View.GONE);
    if (mCheckMD5Task == null) {
        mDownloadSection.setVisibility(View.VISIBLE);
        mCheckMD5.setVisibility(View.GONE);
    }
    mProgressParent.setVisibility(mDownloadTask == null ? View.INVISIBLE : View.VISIBLE);
    mFabButton.setImageDrawable(mDownloadTask == null ? mDownloadDrawable : mCancelDrawable);
    mFabButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(final View view) {
            if (mDownloadTask == null) {
                mFabButton.setImageDrawable(mCancelDrawable);
                mProgressParent.setVisibility(View.VISIBLE);
                mMismatchMD5.setVisibility(View.GONE);
                mInstallButton.setVisibility(View.GONE);
                mProgressBar.setProgress(0);
                mProgressText.setText("");

                mProgressBar.setIndeterminate(true);
                mDownloadTask = new DownloadTask(mActvity, new DownloadTask.OnDownloadListener() {

                    private int mPercentage;

                    @Override
                    public void onUpdate(int currentSize, int totalSize) {
                        int percentage = Math.round(currentSize * 100f / totalSize);
                        if (mPercentage != percentage) {
                            mPercentage = percentage;
                            mProgressBar.setIndeterminate(false);
                            double current = Utils.roundTo2Decimals(currentSize / 1024D / 1024D);
                            double total = Utils.roundTo2Decimals(totalSize / 1024D / 1024D);
                            mProgressBar.setProgress(mPercentage);
                            mProgressText.setText(view.getContext().getString(R.string.downloading_counting,
                                    String.valueOf(current), String.valueOf(total))
                                    + view.getContext().getString(R.string.mb));
                        }
                    }

                    @Override
                    public void onSuccess(String path) {
                        mFabButton.setImageDrawable(mDownloadDrawable);
                        mProgressParent.setVisibility(View.INVISIBLE);
                        mDownloadTask = null;
                        checkMD5(mDownload.getMD5sum(), path, mDownload.getInstallMethod());
                    }

                    @Override
                    public void onCancel() {
                        mFabButton.setImageDrawable(mDownloadDrawable);
                        mProgressParent.setVisibility(View.INVISIBLE);
                        mMismatchMD5.setVisibility(View.GONE);
                        mDownloadTask = null;
                    }

                    @Override
                    public void onFailure(String error) {
                        Utils.toast(view.getContext().getString(R.string.download_error, titleText),
                                view.getContext());
                        mFabButton.setImageDrawable(mDownloadDrawable);
                        mProgressParent.setVisibility(View.INVISIBLE);
                        mDownloadTask = null;
                    }
                }, Utils.getInternalDataStorage() + "/downloads/" + titleText.toString() + ".zip");
                mDownloadTask.execute(mDownload.getUrl());
            } else {
                mFabButton.setImageDrawable(mDownloadDrawable);
                mProgressParent.setVisibility(View.INVISIBLE);
                mDownloadTask.cancel();
                mDownloadTask = null;
            }
        }
    });
}

From source file:com.jun.elephant.ui.widget.VoteDialog.java

private void setDrawableTop(TextView textView, int resId) {
    Drawable drawable = ContextCompat.getDrawable(mContext, resId);
    drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
    textView.setCompoundDrawables(null, drawable, null, null);
}

From source file:com.appeaser.sublimenavigationviewlibrary.SublimeSwitchItemView.java

public void setCheckableItemTintList(ColorStateList checkableItemTintList) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        // Covers android M (23)
        Drawable dTrack = getResources().getDrawable(R.drawable.snv_switch_track_material,
                getContext().getTheme());
        Drawable dThumb = getResources().getDrawable(R.drawable.snv_switch_thumb_material_anim,
                getContext().getTheme());

        if (dTrack != null && dThumb != null) {
            DrawableCompat.setTintList(dTrack, checkableItemTintList);
            DrawableCompat.setTintList(dThumb, checkableItemTintList);
            mSwitch.setTrackDrawable(dTrack);
            mSwitch.setThumbDrawable(dThumb);
        }/*from   w  w  w .j  av  a 2s.c om*/
    } else {
        Drawable dTrack = ContextCompat.getDrawable(getContext(), R.drawable.snv_switch_track);
        Drawable dThumb = ContextCompat.getDrawable(getContext(), R.drawable.switch_thumb_pre_lollipop);

        if (dTrack != null && dThumb != null) {
            dTrack = DrawableCompat.wrap(dTrack);
            DrawableCompat.setTintList(dTrack, checkableItemTintList);
            dTrack.setAlpha(85 /* 0.3f */);

            dThumb = DrawableCompat.wrap(dThumb);
            DrawableCompat.setTintList(dThumb, checkableItemTintList);

            mSwitch.setTrackDrawable(dTrack);
            mSwitch.setThumbDrawable(dThumb);
        }
    }
}

From source file:com.h6ah4i.android.example.advrecyclerview.demo_d_grid.DraggableGridExampleFragment.java

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    //noinspection ConstantConditions
    mRecyclerView = (RecyclerView) getView().findViewById(R.id.recycler_view);
    mLayoutManager = new GridLayoutManager(getContext(), 3, GridLayoutManager.VERTICAL, false);

    // drag & drop manager
    mRecyclerViewDragDropManager = new RecyclerViewDragDropManager();
    mRecyclerViewDragDropManager.setDraggingItemShadowDrawable(
            (NinePatchDrawable) ContextCompat.getDrawable(getContext(), R.drawable.material_shadow_z3));
    // Start dragging after long press
    mRecyclerViewDragDropManager.setInitiateOnLongPress(true);
    mRecyclerViewDragDropManager.setInitiateOnMove(false);
    mRecyclerViewDragDropManager.setLongPressTimeout(750);

    //adapter/*from   ww  w.  java  2s .  co  m*/
    final DraggableGridExampleAdapter myItemAdapter = new DraggableGridExampleAdapter(getDataProvider());
    mAdapter = myItemAdapter;

    mWrappedAdapter = mRecyclerViewDragDropManager.createWrappedAdapter(myItemAdapter); // wrap for dragging

    final GeneralItemAnimator animator = new RefactoredDefaultItemAnimator();

    mRecyclerView.setLayoutManager(mLayoutManager);
    mRecyclerView.setAdapter(mWrappedAdapter); // requires *wrapped* adapter
    mRecyclerView.setItemAnimator(animator);

    // additional decorations
    //noinspection StatementWithEmptyBody
    if (supportsViewElevation()) {
        // Lollipop or later has native drop shadow feature. ItemShadowDecorator is not required.
    } else {
        mRecyclerView.addItemDecoration(new ItemShadowDecorator(
                (NinePatchDrawable) ContextCompat.getDrawable(getContext(), R.drawable.material_shadow_z1)));
    }

    mRecyclerViewDragDropManager.attachRecyclerView(mRecyclerView);

    // for debugging
    //        animator.setDebug(true);
    //        animator.setMoveDuration(2000);
}

From source file:cl.smartcities.isci.transportinspector.adapters.dialogAdapters.IncomingBusesAdapter.java

public IncomingBusesAdapter(Context pContext, ArrayList<Service> pServices, String busStop) {
    super(pContext, R.layout.bus_list_row);
    this.busStop = busStop;
    originalServices = pServices;/* w ww . j  a v a  2  s. com*/
    loadViewState();
    this.typeface = Typeface.createFromAsset(pContext.getAssets(), pContext.getString(R.string.icon_font));
    this.mInflater = (LayoutInflater) pContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    this.context = pContext;
    this.next = ContextCompat.getDrawable(this.context, R.drawable.ic_navigate_next_black_36dp);
    this.prev = ContextCompat.getDrawable(this.context, R.drawable.ic_navigate_before_black_36dp);
    this.scale = getContext().getResources().getDisplayMetrics().density;
    this.onIcon = ContextCompat.getDrawable(getContext(), R.drawable.ic_location_on_28dp);
    this.onOffIcon = ContextCompat.getDrawable(getContext(), R.drawable.ic_location_on_28dp);
    this.offIcon = ContextCompat.getDrawable(getContext(), R.drawable.ic_location_off_28dp);
    this.onIconColor = ContextCompat.getColor(getContext(), R.color.green_button);
    this.offIconColor = ContextCompat.getColor(getContext(), R.color.color_grey_off);
    this.onOffIconColor = ContextCompat.getColor(getContext(), R.color.background_white);
}

From source file:com.amaze.carbonfilemanager.activities.DbViewer.java

@Override
public void onCreate(Bundle savedInstanceState) {
    this.checkStorage = false;
    super.onCreate(savedInstanceState);

    if (getAppTheme().equals(AppTheme.DARK)) {
        setTheme(R.style.appCompatDark);
        getWindow().getDecorView().setBackgroundColor(Utils.getColor(this, R.color.holo_dark_background));
    }//from  w w w .  j  a  va 2 s . c  o m
    setContentView(R.layout.activity_db_viewer);
    toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    if (SDK_INT >= 21) {
        ActivityManager.TaskDescription taskDescription = new ActivityManager.TaskDescription("Amaze",
                ((BitmapDrawable) ContextCompat.getDrawable(this, R.mipmap.ic_launcher)).getBitmap(),
                getColorPreference().getColor(ColorUsage.getPrimary(MainActivity.currentTab)));
        setTaskDescription(taskDescription);
    }
    getSupportActionBar().setBackgroundDrawable(
            getColorPreference().getDrawable(ColorUsage.getPrimary(MainActivity.currentTab)));
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    if (SDK_INT == 20 || SDK_INT == 19) {
        SystemBarTintManager tintManager = new SystemBarTintManager(this);
        tintManager.setStatusBarTintEnabled(true);
        tintManager.setStatusBarTintColor(
                getColorPreference().getColor(ColorUsage.getPrimary(MainActivity.currentTab)));
        FrameLayout.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) findViewById(R.id.parentdb)
                .getLayoutParams();
        SystemBarTintManager.SystemBarConfig config = tintManager.getConfig();
        p.setMargins(0, config.getStatusBarHeight(), 0, 0);
    } else if (SDK_INT >= 21) {
        boolean colourednavigation = sharedPref.getBoolean("colorednavigation", true);
        Window window = getWindow();
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        window.setStatusBarColor(PreferenceUtils.getStatusColor(
                getColorPreference().getColorAsString(ColorUsage.getPrimary(MainActivity.currentTab))));
        if (colourednavigation)
            window.setNavigationBarColor(PreferenceUtils.getStatusColor(
                    getColorPreference().getColorAsString(ColorUsage.getPrimary(MainActivity.currentTab))));

    }

    path = getIntent().getStringExtra("path");
    pathFile = new File(path);
    listView = (ListView) findViewById(R.id.listView);

    load(pathFile);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
            DbViewerFragment fragment = new DbViewerFragment();
            Bundle bundle = new Bundle();
            bundle.putString("table", arrayList.get(position));
            fragment.setArguments(bundle);
            fragmentTransaction.add(R.id.content_frame, fragment);
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();
        }
    });

}

From source file:com.h6ah4i.android.example.advrecyclerview.demo_composition_all.CompositionAllExampleActivity.java

private ComposedAdapter createComposedAdapter(RecyclerView rv, OnListItemClickMessageListener clickListener) {
    RecyclerViewDragDropManager dragMgr = new RecyclerViewDragDropManager();
    RecyclerViewDragDropManager dragMgr2 = new RecyclerViewDragDropManager();
    RecyclerViewSwipeManager swipeMgr = new RecyclerViewSwipeManager();
    RecyclerViewSwipeManager swipeMgr2 = new RecyclerViewSwipeManager();
    RecyclerViewExpandableItemManager expMgr = new RecyclerViewExpandableItemManager(null);
    RecyclerViewExpandableItemManager expMgr2 = new RecyclerViewExpandableItemManager(null);

    dragMgr.setDraggingItemShadowDrawable(
            (NinePatchDrawable) ContextCompat.getDrawable(this, R.drawable.material_shadow_z3));
    dragMgr2.setDraggingItemShadowDrawable(
            (NinePatchDrawable) ContextCompat.getDrawable(this, R.drawable.material_shadow_z3));

    ComposedAdapter composedAdapter = new ComposedAdapter();

    composedAdapter.addAdapter(new MySectionHeaderAdapter("Draggable - 1"));
    composedAdapter.addAdapter(dragMgr.createWrappedAdapter(new MyDraggableAdapter(clickListener)));
    composedAdapter.addAdapter(new MySectionHeaderAdapter("Draggable - 2"));
    composedAdapter.addAdapter(dragMgr2.createWrappedAdapter(new MyDraggableAdapter(clickListener)));

    composedAdapter.addAdapter(new MySectionHeaderAdapter("Swipeable - 1"));
    composedAdapter.addAdapter(swipeMgr.createWrappedAdapter(new MySwipeableAdapter(clickListener)));
    composedAdapter.addAdapter(new MySectionHeaderAdapter("Swipeable - 2"));
    composedAdapter.addAdapter(swipeMgr2.createWrappedAdapter(new MySwipeableAdapter(clickListener)));

    composedAdapter.addAdapter(new MySectionHeaderAdapter("Expandable - 1"));
    composedAdapter.addAdapter(expMgr.createWrappedAdapter(new MyExpandableAdapter(expMgr, clickListener)));
    composedAdapter.addAdapter(new MySectionHeaderAdapter("Expandable - 2"));
    composedAdapter.addAdapter(expMgr2.createWrappedAdapter(new MyExpandableAdapter(expMgr2, clickListener)));

    dragMgr.attachRecyclerView(rv);/*from www .  ja  v a2 s . c  o m*/
    dragMgr2.attachRecyclerView(rv);
    swipeMgr.attachRecyclerView(rv);
    swipeMgr2.attachRecyclerView(rv);
    expMgr.attachRecyclerView(rv);
    expMgr2.attachRecyclerView(rv);

    return composedAdapter;
}

From source file:com.classiqo.nativeandroid_32bitz.ui.MediaItemViewHolder.java

public static Drawable getDrawableByState(Context context, int state) {
    if (sColorStateNotPlaying == null || sColorStatePlaying == null) {
        initializeColorStateLists(context);
    }/*  w w  w .  jav a2 s.c o m*/

    switch (state) {
    case STATE_PLAYABLE:
        Drawable pauseDrawable = ContextCompat.getDrawable(context, R.drawable.ic_play_arrow_black_36dp);
        DrawableCompat.setTintList(pauseDrawable, sColorStateNotPlaying);

        return pauseDrawable;
    case STATE_PLAYING:
        AnimationDrawable animation = (AnimationDrawable) ContextCompat.getDrawable(context,
                R.drawable.ic_equalizer_white_36dp);
        DrawableCompat.setTintList(animation, sColorStatePlaying);
        animation.start();

        return animation;
    case STATE_PAUSED:
        Drawable playDrawable = ContextCompat.getDrawable(context, R.drawable.ic_equalizer1_white_36dp);
        DrawableCompat.setTintList(playDrawable, sColorStatePlaying);

        return playDrawable;
    default:
        return null;
    }
}

From source file:com.bilibili.magicasakura.widgets.AppCompatBackgroundHelper.java

public void setBackgroundResId(int resId) {
    if (mBackgroundResId != resId) {
        resetTintResource(resId);//  ww w  . j  a  v  a  2 s . c  o m

        if (resId != 0) {
            Drawable drawable = mTintManager.getDrawable(resId);
            setBackgroundDrawable(
                    drawable != null ? drawable : ContextCompat.getDrawable(mView.getContext(), resId));
        }
    }
}

From source file:com.dolbik.pavel.translater.fragments.translate.TranslateFragment.java

@Nullable
@Override/*www. j a va  2  s  . co  m*/
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
        @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_translate, container, false);

    Toolbar toolbar = (Toolbar) view.findViewById(R.id.toolbar);
    ((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
    progressBar = (ProgressBar) toolbar.findViewById(R.id.progressBar);
    toolbarView = (LinearLayout) toolbar.findViewById(R.id.toolbar_view);
    fromDirection = (TextView) toolbar.findViewById(R.id.from);
    toDirection = (TextView) toolbar.findViewById(R.id.to);
    ImageView swap = (ImageView) toolbar.findViewById(R.id.swap);

    fromDirection.setOnClickListener(this);
    toDirection.setOnClickListener(this);
    swap.setOnClickListener(this);

    coordinatorLayout = (CoordinatorLayout) view.findViewById(R.id.coordinatorLayout);
    translateContainer = (RelativeLayout) view.findViewById(R.id.translate_container);
    translate = (EditText) view.findViewById(R.id.translate);

    translate.setOnFocusChangeListener((v, hasFocus) -> {
        if (hasFocus) {
            translateContainer.setBackground(
                    ContextCompat.getDrawable(getContext(), R.drawable.translate_container_focus_shape));
        } else {
            translateContainer.setBackground(
                    ContextCompat.getDrawable(getContext(), R.drawable.translate_container_shape));
        }
    });

    translateSbs = RxTextView.textChanges(translate).debounce(1000, TimeUnit.MILLISECONDS)
            .map(charSequence -> charSequence.toString().replaceAll("[\r\n]+", "\r").trim())
            .observeOn(AndroidSchedulers.mainThread()).subscribe(s -> presenter.translateText(s, false));

    license = (TextView) view.findViewById(R.id.license);
    clear = (ImageView) view.findViewById(R.id.clear);
    clear.setOnClickListener(this);

    resultPrg = (ProgressBar) view.findViewById(R.id.resultPrg);
    resultTranslate = (TextView) view.findViewById(R.id.resultTranslate);
    resultError = (TextView) view.findViewById(R.id.resultError);
    favorite = (ImageView) view.findViewById(R.id.favorite);
    favorite.setOnClickListener(this);

    return view;
}