Example usage for android.widget ImageView setImageDrawable

List of usage examples for android.widget ImageView setImageDrawable

Introduction

In this page you can find the example usage for android.widget ImageView setImageDrawable.

Prototype

public void setImageDrawable(@Nullable Drawable drawable) 

Source Link

Document

Sets a drawable as the content of this ImageView.

Usage

From source file:com.google.samples.apps.iosched.util.LPreviewUtilsImpl.java

@Override
public void setOrAnimatePlusCheckIcon(final ImageView imageView, boolean isCheck, boolean allowAnimate) {
    AnimatedStateListDrawable drawable = (AnimatedStateListDrawable) mActivity.getResources()
            .getDrawable(R.drawable.add_schedule_fab_icon_anim);
    imageView.setImageDrawable(drawable);
    if (allowAnimate) {
        // TODO: figure out if there's a way to always animate from current state
        imageView.setImageState(isCheck ? STATE_UNCHECKED : STATE_CHECKED, false);
        drawable.jumpToCurrentState();//from   ww w .  ja va 2  s  . co  m
        imageView.setImageState(isCheck ? STATE_CHECKED : STATE_UNCHECKED, false);
    } else {
        imageView.setImageState(isCheck ? STATE_CHECKED : STATE_UNCHECKED, false);
        drawable.jumpToCurrentState();
    }
}

From source file:com.cleverua.test.thumbs.ImageDownloader.java

/**
 * Same as download but the image is always downloaded and the cache is not used.
 * Kept private at the moment as its interest is not clear.
 *//*from w ww  .j a  v a2 s.c  om*/
private void forceDownload(String url, ImageView imageView, Drawable placeHolder) {
    // State sanity: url is guaranteed to never be null in DownloadedDrawable and cache keys.
    if (url == null) {
        imageView.setImageDrawable(null);
        return;
    }

    if (removePotentialDownload(url, imageView)) {
        BitmapDownloaderTask task = new BitmapDownloaderTask(url, imageView);
        imageView.setImageDrawable(placeHolder);
        imageView.setTag(new WeakReference<BitmapDownloaderTask>(task));

        imageView.setMinimumHeight(156);

        pendingTasks.offerLast(task);
        if (loaderTask == null || loaderTask.getStatus() == AsyncTask.Status.FINISHED) {
            loaderTask = pendingTasks.poll();
            loaderTask.execute();
        }
    }
}

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

private void addAnimation(final DecoView arcView, int series, float moveTo, int delay,
        final ImageView imageView, final int imageId, final int color) {
    DecoEvent.ExecuteEventListener listener = new DecoEvent.ExecuteEventListener() {
        @Override/*from   w ww. jav a2s.com*/
        public void onEventStart(DecoEvent event) {
            imageView.setImageDrawable(ContextCompat.getDrawable(getActivity(), imageId));

            showAvatar(true, imageView);

            arcView.addEvent(new DecoEvent.Builder(EventType.EVENT_COLOR_CHANGE, color).setIndex(mBack1Index)
                    .setDuration(2000).build());
        }

        @Override
        public void onEventEnd(DecoEvent event) {
            showAvatar(false, imageView);

            arcView.addEvent(new DecoEvent.Builder(EventType.EVENT_COLOR_CHANGE, COLOR_BACK)
                    .setIndex(mBack1Index).setDuration(2000).build());
        }

    };

    arcView.addEvent(new DecoEvent.Builder(moveTo).setIndex(series).setDelay(delay).setDuration(5000)
            .setListener(listener).build());
}

From source file:edu.stanford.mobisocial.dungbeetle.feed.objects.AppObj.java

@Override
public void render(final Context context, final ViewGroup frame, Obj obj, boolean allowInteractions) {
    PackageManager pm = context.getPackageManager();
    Drawable icon = null;// ww  w . j  a v  a  2 s .  c o  m
    String appName;
    if (obj.getJson() != null && obj.getJson().has(ANDROID_PACKAGE_NAME)) {
        appName = obj.getJson().optString(ANDROID_PACKAGE_NAME);
    } else {
        appName = "Unknown";
    }
    if (!(obj instanceof DbObj)) {
        if (appName.contains(".")) {
            appName = appName.substring(appName.lastIndexOf(".") + 1);
        }
        String text = "Preparing application " + appName + "...";
        // TODO: Show Market icon or app icon.
        TextView valueTV = new TextView(context);
        valueTV.setText(text);
        valueTV.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT));
        valueTV.setGravity(Gravity.TOP | Gravity.LEFT);
        frame.addView(valueTV);
        return;
    }

    DbObj dbParentObj = (DbObj) obj;
    boolean rendered = false;

    Intent launch = getLaunchIntent(context, dbParentObj);
    List<ResolveInfo> infos = pm.queryIntentActivities(launch, 0);
    if (infos.size() > 0) {
        ResolveInfo info = infos.get(0);
        if (info.activityInfo.labelRes != 0) {
            appName = info.activityInfo.loadLabel(pm).toString();
            icon = info.loadIcon(pm);
        } else {
            appName = info.activityInfo.name;
        }
    } else {
        appName = obj.getJson().optString(ANDROID_PACKAGE_NAME);
        if (appName.contains(".")) {
            appName = appName.substring(appName.lastIndexOf(".") + 1);
        }
    }
    // TODO: Safer reference to containing view
    if (icon != null) {
        View parentView = (View) frame.getParent().getParent();
        ImageView avatar = (ImageView) parentView.findViewById(R.id.icon);
        avatar.setImageDrawable(icon);

        TextView label = (TextView) parentView.findViewById(R.id.name_text);
        label.setText(appName);
    }

    // TODO: obj.getLatestChild().render();
    String selection = getRenderableClause();
    String[] selectionArgs = null;
    Cursor cursor = dbParentObj.getSubfeed().query(selection, selectionArgs);
    if (cursor.moveToFirst()) {
        DbObj dbObj = App.instance().getMusubi().objForCursor(cursor);
        DbObjects.getFeedRenderer(dbObj.getType()).render(context, frame, dbObj, allowInteractions);
        rendered = true;
    }

    if (!rendered) {
        String text;
        if (icon != null) {
            ImageView iv = new ImageView(context);
            iv.setImageDrawable(icon);
            iv.setAdjustViewBounds(true);
            iv.setMaxWidth(60);
            iv.setMaxHeight(60);
            iv.setLayoutParams(CommonLayouts.WRAPPED);
            frame.addView(iv);
            text = appName;
        } else {
            text = "New application: " + appName + ".";
        }
        // TODO: Show Market icon or app icon.
        TextView valueTV = new TextView(context);
        valueTV.setText(text);
        valueTV.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT));
        valueTV.setGravity(Gravity.TOP | Gravity.LEFT);
        frame.addView(valueTV);
    }
}

From source file:com.jgkj.bxxc.fragment.IndexFragment2.java

private void scrollView() {
    SharedPreferences sp = getActivity().getSharedPreferences("PicCount", Activity.MODE_PRIVATE);
    final int count = sp.getInt("Count", -1);
    if (count != -1) {
        final ImageView[] dots = new ImageView[count];
        for (int k = 0; k < count; k++) {
            ImageView image = new ImageView(getActivity());
            image.setImageDrawable(getResources().getDrawable(R.drawable.selector));
            image.setId(k);//from  w  w  w .j av a2 s  .co  m
            wrapParams = new LinearLayout.LayoutParams(ViewPager.LayoutParams.WRAP_CONTENT,
                    ViewPager.LayoutParams.WRAP_CONTENT);
            wrapParams.leftMargin = 5;
            image.setLayoutParams(wrapParams);
            linearlayout.addView(image);
            dots[k] = (ImageView) linearlayout.getChildAt(k);
            dots[k].setEnabled(true);
        }
        final Handler mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                if (currentItem < (count - 1)) {
                    currentItem++;
                    viewpager.setCurrentItem(currentItem);
                } else if (currentItem == (count - 1)) {
                    currentItem = 0;
                    viewpager.setCurrentItem(currentItem);
                }
                for (int j = 0; j < count; j++) {
                    dots[j].setEnabled(false);
                }
                dots[currentItem].setEnabled(true);
            }
        };
        TimerTask timerTask = new TimerTask() {
            @Override
            public void run() {
                mHandler.sendEmptyMessage(0);
            }
        };
        timer.schedule(timerTask, 1000, 3000);
    }
}

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

private void addAnimation(final DecoView arcView, int series, float moveTo, int delay,
        final ImageView imageView, final int imageId, final String format, final int color,
        final boolean restart) {

    DecoEvent.ExecuteEventListener listener = new DecoEvent.ExecuteEventListener() {
        @Override/* ww w  .j av a  2s  .  c  o  m*/
        public void onEventStart(DecoEvent event) {
            imageView.setImageDrawable(ContextCompat.getDrawable(getActivity(), imageId));
            showAvatar(true, imageView);
            mProgress = format;
        }

        @Override
        public void onEventEnd(DecoEvent event) {
            showAvatar(false, imageView);
            if (restart) {
                setupEvents();
            }
        }
    };

    arcView.addEvent(new DecoEvent.Builder(moveTo).setIndex(series).setDelay(delay).setDuration(5000)
            .setListener(listener).setColor(color).build());
}

From source file:com.starwood.anglerslong.LicenseActivity.java

/******************************************************************************************************************************************
 * Populates all of the tabs that have been added to this feature.
 * Sets an onClickListener to each one of the items
 * Keeps track of all the button ID's//w  w  w  .  j  av a 2 s .  com
 * Keeps track of the TOTAL number of items in the list.
 * *****************************************************************************************************************************************
 * @param json
 * @throws JSONException
 ******************************************************************************************************************************************/
private void populateLicense(String json, boolean isRestart) throws JSONException {

    //**** Used to get the correct relative layout ****//
    int[] rlIdArray = { R.id.rl1, R.id.rl2, R.id.rl3, R.id.rl4, R.id.rl5 };
    //**** Get the correct textview and populate it ****//
    int[][] textViewIdArray = { { R.id.name_rl, R.id.name_rl2, R.id.name_rl3, R.id.name_rl4, R.id.name_rl5 },
            { R.id.number_rl, R.id.number_rl2, R.id.number_rl3, R.id.number_rl4, R.id.number_rl5 },
            { R.id.type_rl, R.id.type_rl2, R.id.type_rl3, R.id.type_rl4, R.id.type_rl5 },
            { R.id.issuedate_rl, R.id.issuedate_rl2, R.id.issuedate_rl3, R.id.issuedate_rl4,
                    R.id.issuedate_rl5 },
            { R.id.expdate_rl, R.id.expdate_rl2, R.id.expdate_rl3, R.id.expdate_rl4, R.id.expdate_rl5 },
            { R.id.birthday_rl, R.id.birthday_rl2, R.id.birthday_rl3, R.id.birthday_rl4, R.id.birthday_rl5 },
            { R.id.huntered_rl, R.id.huntered_rl2, R.id.huntered_rl3, R.id.huntered_rl4, R.id.huntered_rl5 },
            { R.id.flag_rl, R.id.flag_rl2, R.id.flag_rl3, R.id.flag_rl4, R.id.flag_rl5 } };
    //**** Populate the state flag the user chose ****//
    int[] flagArray = { R.drawable.sc_flag, R.drawable.ga_flag, R.drawable.fl_flag, R.drawable.al_flag,
            R.drawable.ms_flag };
    //**** These are oll the options the user can enter ****//
    String[] jsonStrings = { "name", "number", "type", "issue_date", "exp_date", "birthday", "huntered",
            "state" };
    numItems = 0;
    JSONObject j = new JSONObject(json); // Whole JSON String
    JSONObject outer = j.getJSONObject("outermost"); // Outermost JSON
    JSONArray licenseArray = outer.getJSONArray("license");
    if (licenseArray.length() == 0 || licenseArray == null) {
        isLicensePopulated = true;
        isArrayEmpty = true;
    }

    for (int i = 0; i < licenseArray.length(); i++) {
        if (isRestart && !isEdit)
            i = licenseArray.length() - 1; // Execute this only if you just added a button (so you dont reload all buttons).
        else if (isEdit) {
            isEdit = false;
            break;
        }

        findViewById(rlIdArray[i]).setVisibility(View.VISIBLE);

        JSONObject temp = licenseArray.getJSONObject(i);

        TextView tv;
        for (int n = 0; n < jsonStrings.length; n++) {
            if (n == 7) { // Only go here if you're populating the FLAG (b/c it's not a textview)
                ImageView flag = (ImageView) findViewById(textViewIdArray[n][i]);
                flag.setImageDrawable(getResources()
                        .getDrawable(flagArray[Integer.parseInt(temp.getString(jsonStrings[n]))]));
                continue;
            }
            tv = (TextView) findViewById(textViewIdArray[n][i]);
            tv.setText(temp.getString(jsonStrings[n]));
        }

        ImageView licenseImage = (ImageView) findViewById(imageViewIdArray[i]);
        licenseImage.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                clickedItem(v);
            }
        });

        isArrayEmpty = false;
        isLicensePopulated = true;
        numItems++; // <<<<------- Lets you know how many items are populated on screen
    }
}

From source file:com.google.samples.apps.topeka.adapter.ScoreAdapter.java

private void setSolvedStateForQuiz(ImageView solvedState, int position) {
    final Context context = solvedState.getContext();
    final Drawable tintedImage;
    if (mCategory.isSolvedCorrectly(getItem(position))) {
        tintedImage = getSuccessIcon(context);
    } else {/*from  w w  w  . j  av  a  2  s.c  om*/
        tintedImage = getFailedIcon(context);
    }
    solvedState.setImageDrawable(tintedImage);
}

From source file:org.npr.android.news.NewsListAdapter.java

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        convertView = inflater.inflate(R.layout.news_item, parent, false);
    }//from  www .java 2 s  . co  m

    Story story = getItem(position);

    ImageView image = (ImageView) convertView.findViewById(R.id.NewsItemImage);
    TextView teaser = (TextView) convertView.findViewById(R.id.NewsItemTeaserText);
    TextView name = (TextView) convertView.findViewById(R.id.NewsItemNameText);

    if (story != null) {
        image.setImageDrawable(getContext().getResources()
                .getDrawable(isPlayable(story) ? R.drawable.icon_listen_main : R.drawable.bullet));
        image.setVisibility(View.VISIBLE);
        name.setTypeface(headlineTypeface, Typeface.NORMAL);
        name.setText(story.toString());
        String teaserText = story.getMiniTeaser();
        if (teaserText == null) {
            teaserText = story.getTeaser();
        }
        if (teaserText != null && teaserText.length() > 0) {
            // Disable for now.
            //        teaser.setText(story.getTeaser());
            //        teaser.setVisibility(View.VISIBLE);
        } else {
            teaser.setVisibility(View.INVISIBLE);
        }
        teaser.setVisibility(View.GONE);
    } else {
        // null marker means it's the end of the list.
        image.setVisibility(View.INVISIBLE);
        teaser.setVisibility(View.INVISIBLE);
        name.setTypeface(Typeface.SANS_SERIF, Typeface.ITALIC);
        name.setText(R.string.msg_load_more);
    }
    return convertView;
}

From source file:com.dm.wallpaper.board.fragments.CollectionFragment.java

private void initSearchBar() {
    Drawable drawable = ConfigurationHelper.getNavigationIcon(getActivity(),
            WallpaperBoardApplication.getConfig().getNavigationIcon());
    int color = ColorHelper.getAttributeColor(getActivity(), R.attr.search_bar_icon);
    if (drawable != null) {
        mNavigation.setImageDrawable(DrawableHelper.getTintedDrawable(drawable, color));
    }/*ww w  . ja v a  2 s .c o  m*/
    mNavigation.setOnClickListener(view -> {
        if (getActivity() instanceof WallpaperBoardActivity) {
            ((WallpaperBoardActivity) getActivity()).openDrawer();
        }
    });

    ImageView searchIcon = getActivity().findViewById(R.id.search);
    if (searchIcon != null) {
        searchIcon.setImageDrawable(
                DrawableHelper.getTintedDrawable(getActivity(), R.drawable.ic_toolbar_search, color));
    }

    TextView searchBarTitle = getActivity().findViewById(R.id.search_bar_title);
    if (searchBarTitle != null) {
        if (WallpaperBoardApplication.getConfig().getAppLogoColor() != -1) {
            searchBarTitle.setTextColor(WallpaperBoardApplication.getConfig().getAppLogoColor());
        } else {
            searchBarTitle.setTextColor(ColorHelper.setColorAlpha(color, 0.7f));
        }
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        if (mSearchBar.getLayoutParams() instanceof CoordinatorLayout.LayoutParams) {
            CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) mSearchBar
                    .getLayoutParams();
            params.setMargins(params.leftMargin,
                    params.topMargin + WindowHelper.getStatusBarHeight(getActivity()), params.leftMargin,
                    params.bottomMargin);
        }

        StateListAnimator stateListAnimator = AnimatorInflater.loadStateListAnimator(getActivity(),
                R.animator.card_lift);
        mSearchBar.setStateListAnimator(stateListAnimator);
    }

    mSearchBar.setOnClickListener(view -> {
        Intent intent = new Intent(getActivity(), WallpaperBoardBrowserActivity.class);
        intent.putExtra(Extras.EXTRA_FRAGMENT_ID, Extras.ID_WALLPAPER_SEARCH);
        intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        startActivity(intent);
        getActivity().overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
    });

    mMenuSort.setImageDrawable(
            DrawableHelper.getTintedDrawable(getActivity(), R.drawable.ic_toolbar_sort, color));
    mMenuSort.setOnClickListener(view -> {
        Popup.Builder(getActivity()).to(mMenuSort).list(PopupItem.getSortItems(getActivity(), true))
                .callback((popup, position) -> {
                    Preferences.get(getActivity()).setSortBy(popup.getItems().get(position).getType());

                    refreshWallpapers();
                    popup.dismiss();
                }).show();
    });
}