Example usage for android.widget ImageView setImageResource

List of usage examples for android.widget ImageView setImageResource

Introduction

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

Prototype

@android.view.RemotableViewMethod(asyncImpl = "setImageResourceAsync")
public void setImageResource(@DrawableRes int resId) 

Source Link

Document

Sets a drawable as the content of this ImageView.

Usage

From source file:com.android.settings.Settings.java

private void setupMore() {
    final Tab tab = mActionBar.newTab();
    LayoutInflater inflater = getLayoutInflater();
    View view = inflater.inflate(R.layout.setting_tab_new_ui, null);
    ImageView dialView = (ImageView) view.findViewById(R.id.tab_icon);
    if (dialView != null) {
        dialView.setImageResource(R.drawable.tab_more);
    }/*from ww  w  . ja v  a  2 s . c  om*/
    TextView dialText = (TextView) view.findViewById(R.id.tab_text);
    if (dialText != null) {
        dialText.setText(R.string.more_settings_tab);
    }
    tab.setCustomView(view);
    tab.setTag(R.xml.settings_headers_more);
    tab.setTabListener(mTabListener);
    mActionBar.addTab(tab);
}

From source file:com.eleybourn.bookcatalogue.utils.Utils.java

/**
 * Shrinks the passed image file spec into the specificed dimensions, and returns the bitmap. If the view 
 * is non-null, the image is also placed in the view.
 * //from  w  ww  . ja v a  2 s  .c om
 * @param destView
 * @param filename
 * @param maxWidth
 * @param maxHeight
 * @param exact
 * 
 * @return
 */
private static Bitmap shrinkFileIntoImageView(ImageView destView, String filename, int maxWidth, int maxHeight,
        boolean exact) {
    Bitmap bm = null;

    // Read the file to get file size
    BitmapFactory.Options opt = new BitmapFactory.Options();
    opt.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(filename, opt);

    // If no size info, or a single pixel, assume file bad and set the 'alert' icon
    if (opt.outHeight <= 0 || opt.outWidth <= 0 || (opt.outHeight == 1 && opt.outWidth == 1)) {
        if (destView != null)
            destView.setImageResource(android.R.drawable.ic_dialog_alert);
        return null;
    }

    // Next time we don't just want the bounds, we want the file
    opt.inJustDecodeBounds = false;

    // Work out how to scale the file to fit in required bbox
    float widthRatio = (float) maxWidth / opt.outWidth;
    float heightRatio = (float) maxHeight / opt.outHeight;

    // Work out scale so that it fit exactly
    float ratio = widthRatio < heightRatio ? widthRatio : heightRatio;

    // Note that inSampleSize seems to ALWAYS be forced to a power of 2, no matter what we
    // specify, so we just work with powers of 2.
    int idealSampleSize = (int) android.util.FloatMath.ceil(1 / ratio); // This is the sample size we want to use
    // Get the nearest *bigger* power of 2.
    int samplePow2 = (int) Math.pow(2, Math.ceil(Math.log(idealSampleSize) / Math.log(2)));

    try {
        if (exact) {
            // Create one bigger than needed and scale it; this is an attempt to improve quality.
            opt.inSampleSize = samplePow2 / 2;
            if (opt.inSampleSize < 1)
                opt.inSampleSize = 1;
            Bitmap tmpBm = BitmapFactory.decodeFile(filename, opt);
            if (tmpBm == null) {
                // We ran out of memory, most likely
                // TODO: Need a way to try loading images after GC(), or something. Otherwise, covers in cover browser wil stay blank.
                Logger.logError(
                        new RuntimeException("Unexpectedly failed to decode bitmap; memory exhausted?"));
                return null;
            }
            android.graphics.Matrix matrix = new android.graphics.Matrix();
            // Fixup ratio based on new sample size and scale it.
            ratio = ratio / (1.0f / opt.inSampleSize);
            matrix.postScale(ratio, ratio);
            bm = Bitmap.createBitmap(tmpBm, 0, 0, opt.outWidth, opt.outHeight, matrix, true);
            // Recycle if original was not returned
            if (bm != tmpBm) {
                tmpBm.recycle();
                tmpBm = null;
            }
        } else {
            // Use a scale that will make image *no larger than* the desired size
            if (ratio < 1.0f)
                opt.inSampleSize = samplePow2;
            bm = BitmapFactory.decodeFile(filename, opt);
        }
    } catch (OutOfMemoryError e) {
        return null;
    }

    // Set ImageView and return bitmap
    if (destView != null)
        destView.setImageBitmap(bm);

    return bm;
}

From source file:co.taqat.call.CallActivity.java

private void setContactInformation(TextView contactName, ImageView contactPicture, LinphoneAddress lAddress) {
    LinphoneContact lContact = ContactsManager.getInstance().findContactFromAddress(lAddress);
    if (lContact == null) {
        contactName.setText(LinphoneUtils.getAddressDisplayName(lAddress));
        contactPicture.setImageResource(R.drawable.avatar);
    } else {//from   www.  j a v a 2  s  . c o m
        contactName.setText(lContact.getFullName());
        LinphoneUtils.setImagePictureFromUri(contactPicture.getContext(), contactPicture,
                lContact.getPhotoUri(), lContact.getThumbnailUri());
    }
}

From source file:com.mobicage.rogerthat.plugins.messaging.ServiceMessageDetailActivity.java

private void setStatusIcon(ImageView imageView, final MemberStatusTO ms) {
    int resource;
    if ((ms.status & MessagingPlugin.STATUS_ACKED) == MessagingPlugin.STATUS_ACKED)
        resource = R.drawable.status_blue;
    else if ((ms.status & MessagingPlugin.STATUS_RECEIVED) == MessagingPlugin.STATUS_RECEIVED)
        resource = R.drawable.status_green;
    else//from ww  w  .ja  va2  s .c  o m
        resource = R.drawable.status_yellow;
    imageView.setImageResource(resource);
}

From source file:cm.aptoide.pt.ApkInfo.java

public void loadMalware(final MalwareStatus malwareStatus) {
    runOnUiThread(new Runnable() {

        @Override//from  ww  w.j  ava2  s . c o m
        public void run() {
            try {
                EnumApkMalware apkStatus = EnumApkMalware
                        .valueOf(malwareStatus.getStatus().toUpperCase(Locale.ENGLISH));
                Log.d("ApkInfoMalware-malwareStatus", malwareStatus.getStatus());
                Log.d("ApkInfoMalware-malwareReason", malwareStatus.getReason());

                switch (apkStatus) {
                case SCANNED:
                    ((TextView) findViewById(R.id.app_badge_text)).setText(getString(R.string.trusted));
                    ((ImageView) findViewById(R.id.app_badge)).setImageResource(R.drawable.badge_scanned);
                    ((LinearLayout) findViewById(R.id.badge_layout)).setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            View trustedView = LayoutInflater.from(ApkInfo.this)
                                    .inflate(R.layout.dialog_anti_malware, null);
                            AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(ApkInfo.this)
                                    .setView(trustedView);
                            final AlertDialog trustedDialog = dialogBuilder.create();
                            trustedDialog.setIcon(R.drawable.badge_scanned);
                            trustedDialog.setTitle(getString(R.string.app_trusted, viewApk.getName()));
                            trustedDialog.setCancelable(true);

                            TextView tvSignatureValidation = (TextView) trustedView
                                    .findViewById(R.id.tv_signature_validation);
                            tvSignatureValidation.setText(getString(R.string.signature_verified));
                            ImageView check_signature = (ImageView) trustedView
                                    .findViewById(R.id.check_signature);
                            check_signature.setImageResource(R.drawable.ic_yes);

                            trustedDialog.setButton(Dialog.BUTTON_NEUTRAL, "Ok", new Dialog.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface arg0, int arg1) {
                                    trustedDialog.dismiss();
                                }
                            });
                            trustedDialog.show();
                        }
                    });
                    break;
                //             case UNKNOWN:
                //                ((TextView) findViewById(R.id.app_badge_text)).setText(getString(R.string.unknown));
                //                ((ImageView) findViewById(R.id.app_badge)).setImageResource(R.drawable.badge_unknown);
                //                break;
                case WARN:
                    ((TextView) findViewById(R.id.app_badge_text)).setText(getString(R.string.warning));
                    ((ImageView) findViewById(R.id.app_badge)).setImageResource(R.drawable.badge_warn);
                    ((LinearLayout) findViewById(R.id.badge_layout)).setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            View warnView = LayoutInflater.from(ApkInfo.this)
                                    .inflate(R.layout.dialog_anti_malware, null);
                            AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(ApkInfo.this)
                                    .setView(warnView);
                            final AlertDialog warnDialog = dialogBuilder.create();
                            warnDialog.setIcon(R.drawable.badge_warn);
                            warnDialog.setTitle(getString(R.string.app_warning, viewApk.getName()));
                            warnDialog.setCancelable(true);

                            TextView tvSignatureValidation = (TextView) warnView
                                    .findViewById(R.id.tv_signature_validation);
                            tvSignatureValidation.setText(getString(R.string.signature_not_verified));
                            ImageView check_signature = (ImageView) warnView.findViewById(R.id.check_signature);
                            check_signature.setImageResource(R.drawable.ic_failed);

                            warnDialog.setButton(Dialog.BUTTON_NEUTRAL, "Ok", new Dialog.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface arg0, int arg1) {
                                    warnDialog.dismiss();
                                }
                            });
                            warnDialog.show();
                        }
                    });
                    break;
                //             case CRITICAL:
                //                ((TextView) findViewById(R.id.app_badge_text)).setText(getString(R.string.critical));
                //                ((ImageView) findViewById(R.id.app_badge)).setImageResource(R.drawable.badge_critical);
                //                break;
                default:
                    break;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    });

}

From source file:co.taqat.call.CallActivity.java

private boolean displayCallStatusIconAndReturnCallPaused(LinearLayout callView, LinphoneCall call) {
    boolean isCallPaused, isInConference;
    ImageView callState = (ImageView) callView.findViewById(R.id.call_pause);
    callState.setTag(call);/*  ww  w. j  a v a 2 s.  c  o m*/
    callState.setOnClickListener(this);

    if (call.getState() == State.Paused || call.getState() == State.PausedByRemote
            || call.getState() == State.Pausing) {
        callState.setImageResource(R.drawable.pause);
        isCallPaused = true;
        isInConference = false;
    } else if (call.getState() == State.OutgoingInit || call.getState() == State.OutgoingProgress
            || call.getState() == State.OutgoingRinging) {
        isCallPaused = false;
        isInConference = false;
    } else {
        isInConference = isConferenceRunning && call.isInConference();
        isCallPaused = false;
    }

    return isCallPaused || isInConference;
}

From source file:com.android.tv.settings.dialog.DialogFragment.java

private void setContentView(View content) {
    TextView titleView = (TextView) content.findViewById(R.id.title);
    TextView breadcrumbView = (TextView) content.findViewById(R.id.breadcrumb);
    TextView descriptionView = (TextView) content.findViewById(R.id.description);
    titleView.setText(mTitle);/*  www .  j a  v a  2s.  c o  m*/
    breadcrumbView.setText(mBreadcrumb);
    descriptionView.setText(mDescription);
    final ImageView iconImageView = (ImageView) content.findViewById(R.id.icon);
    if (mIconBackgroundColor != Color.TRANSPARENT) {
        iconImageView.setBackgroundColor(mIconBackgroundColor);
    }

    if (AccessibilityHelper.forceFocusableViews(getActivity())) {
        titleView.setFocusable(true);
        titleView.setFocusableInTouchMode(true);
        descriptionView.setFocusable(true);
        descriptionView.setFocusableInTouchMode(true);
        breadcrumbView.setFocusable(true);
        breadcrumbView.setFocusableInTouchMode(true);
    }

    if (mIconResourceId != 0) {
        iconImageView.setImageResource(mIconResourceId);
        updateViewSize(iconImageView);
    } else {
        if (mIconBitmap != null) {
            iconImageView.setImageBitmap(mIconBitmap);
            updateViewSize(iconImageView);
        } else {
            if (mIconUri != null) {
                iconImageView.setVisibility(View.INVISIBLE);

                DrawableDownloader bitmapDownloader = DrawableDownloader.getInstance(content.getContext());
                mBitmapCallBack = new BitmapCallback() {
                    @Override
                    public void onBitmapRetrieved(Drawable bitmap) {
                        if (bitmap != null) {
                            mIconBitmap = (bitmap instanceof BitmapDrawable)
                                    ? ((BitmapDrawable) bitmap).getBitmap()
                                    : null;
                            iconImageView.setVisibility(View.VISIBLE);
                            iconImageView.setImageDrawable(bitmap);
                            updateViewSize(iconImageView);
                        }
                    }
                };

                bitmapDownloader.getBitmap(new BitmapWorkerOptions.Builder(content.getContext())
                        .resource(mIconUri).width(iconImageView.getLayoutParams().width).build(),
                        mBitmapCallBack);
            } else {
                iconImageView.setVisibility(View.GONE);
            }
        }
    }

    content.setTag(R.id.title, titleView);
    content.setTag(R.id.breadcrumb, breadcrumbView);
    content.setTag(R.id.description, descriptionView);
    content.setTag(R.id.icon, iconImageView);
}

From source file:com.akop.bach.fragment.xboxlive.AchievementsFragment.java

private void loadGameDetails() {
    View view = getView();//www . j a  v a 2 s  .c  o m
    if (view == null)
        return;

    if (mTitleId < 0) {
        view.findViewById(R.id.unselected).setVisibility(View.VISIBLE);
        view.findViewById(R.id.achievement_contents).setVisibility(View.GONE);
    } else {
        view.findViewById(R.id.unselected).setVisibility(View.GONE);
        view.findViewById(R.id.achievement_contents).setVisibility(View.VISIBLE);
    }

    if (!mShowGameTotals)
        return;

    Context context = getActivity();
    ContentResolver cr = context.getContentResolver();
    Cursor c = cr.query(Games.CONTENT_URI, GamesFragment.PROJ, Games._ID + "=" + mTitleId, null, null);

    if (c != null) {
        try {
            if (c.moveToFirst()) {
                TextView tv;
                ImageView iv;

                XboxLiveGameListItem gameListItem = (XboxLiveGameListItem) view.findViewById(R.id.gameListItem);
                gameListItem.mBeaconSet = c.getInt(GamesFragment.GAME_BEACON_SET) != 0;
                gameListItem.mItemId = mTitleId;

                if ((tv = (TextView) view.findViewById(R.id.game_title)) != null)
                    tv.setText(c.getString(GamesFragment.GAME_TITLE));

                if ((tv = (TextView) view.findViewById(R.id.game_last_played)) != null)
                    tv.setText(Games.getLastPlayedText(context, c.getLong(GamesFragment.GAME_LAST_PLAYED)));

                if ((tv = (TextView) view.findViewById(R.id.game_achievements_unlocked)) != null)
                    tv.setText(Games.getAchievementTotalText(context, c.getInt(GamesFragment.GAME_ACH_EARNED),
                            c.getInt(GamesFragment.GAME_ACH_TOTAL)));

                if ((tv = (TextView) view.findViewById(R.id.game_gp_earned)) != null)
                    tv.setText(Games.getGamerscoreTotalText(context, c.getInt(GamesFragment.GAME_GP_EARNED),
                            c.getInt(GamesFragment.GAME_GP_TOTAL)));

                iv = (ImageView) view.findViewById(R.id.game_beacon);
                iv.setImageResource(c.getInt(GamesFragment.GAME_BEACON_SET) != 0 ? R.drawable.beacon_on
                        : R.drawable.beacon_off);

                ImageCache ic = ImageCache.getInstance();
                String iconUrl = c.getString(GamesFragment.GAME_ICON_URL);
                Bitmap bmp;

                if ((bmp = ic.getCachedBitmap(iconUrl)) != null) {
                    iv = (ImageView) view.findViewById(R.id.game_icon);
                    iv.setImageBitmap(bmp);
                } else {
                    if (iconUrl != null) {
                        ic.requestImage(iconUrl, new OnImageReadyListener() {
                            @Override
                            public void onImageReady(long id, Object param, Bitmap bmp) {
                                mHandler.post(new Runnable() {
                                    @Override
                                    public void run() {
                                        loadGameDetails();
                                    }
                                });
                            }
                        }, 0, null, true, null);
                    }
                }
            }
        } finally {
            c.close();
        }
    }
}

From source file:com.google.samples.apps.sergio.ui.BaseActivity.java

private View makeNavDrawerItem(final int itemId, ViewGroup container) {
    boolean selected = getSelfNavDrawerItem() == itemId;
    int layoutToInflate = 0;
    if (itemId == NAVDRAWER_ITEM_SEPARATOR) {
        layoutToInflate = R.layout.navdrawer_separator;
    } else if (itemId == NAVDRAWER_ITEM_SEPARATOR_SPECIAL) {
        layoutToInflate = R.layout.navdrawer_separator;
    } else {/*w  w w .  java2 s.  c  om*/
        layoutToInflate = R.layout.navdrawer_item;
    }
    View view = getLayoutInflater().inflate(layoutToInflate, container, false);

    if (isSeparator(itemId)) {
        // we are done
        UIUtils.setAccessibilityIgnore(view);
        return view;
    }

    ImageView iconView = (ImageView) view.findViewById(R.id.icon);
    TextView titleView = (TextView) view.findViewById(R.id.title);
    int iconId = itemId >= 0 && itemId < NAVDRAWER_ICON_RES_ID.length ? NAVDRAWER_ICON_RES_ID[itemId] : 0;
    int titleId = itemId >= 0 && itemId < NAVDRAWER_TITLE_RES_ID.length ? NAVDRAWER_TITLE_RES_ID[itemId] : 0;

    // set icon and text
    iconView.setVisibility(iconId > 0 ? View.VISIBLE : View.GONE);
    if (iconId > 0) {
        iconView.setImageResource(iconId);
    }
    titleView.setText(getString(titleId));

    formatNavDrawerItem(view, itemId, selected);

    view.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            onNavDrawerItemClicked(itemId);
        }
    });

    return view;
}

From source file:com.androprogrammer.tutorials.customviews.SlidingTabLayout.java

private void populateTabStrip() {
    final PagerAdapter adapter = mViewPager.getAdapter();
    final View.OnClickListener tabClickListener = new TabClickListener();

    for (int i = 0; i < adapter.getCount(); i++) {
        View tabView = null;// www  . j a va  2 s.  com
        TextView tabTitleView = null;
        ImageView tabIconView = null;

        if (mTabViewLayoutId != 0) {
            // If there is a custom tab view layout id set, try and inflate it
            tabView = LayoutInflater.from(getContext()).inflate(mTabViewLayoutId, mTabStrip, false);
            tabTitleView = (TextView) tabView.findViewById(mTabViewTextViewId);
            tabIconView = (ImageView) tabView.findViewById(mTabViewImageViewId);
        }

        if (tabView == null) {
            if (tabType.equals(TabType.ICON)) {
                tabView = createDefaultIconTabView(getContext());
            } else if (tabType.equals(TabType.TEXT)) {

                tabView = createDefaultTextTabView(getContext());
            }
        }

        if (tabTitleView == null && TextView.class.isInstance(tabView)) {
            tabTitleView = (TextView) tabView;
        }

        // setting title to tabView or its' child
        if (tabTitleView != null) {
            // if tabTitleView could be found in tabView, then set title to it
            tabTitleView.setText(adapter.getPageTitle(i));

        } else if (TextView.class.isInstance(tabView)) {
            // if generated tabView is a text view, get page title and set it to the text view
            ((TextView) tabView).setText(adapter.getPageTitle(i));

        }

        // setting icon to tabView or its' child
        if (tabIconView != null) {
            // if tabIconView could be found in tabView, then set icon to it
            tabIconView.setImageResource(getResourceIdByName("ic_" + adapter.getPageTitle(i).toString()));

        } else if (ImageView.class.isInstance(tabView)) {
            // if generated tabView is an image view, find the resource with the page
            // title as resource name suffix. ex: ic_PAGE_TITLE
            ((ImageView) tabView)
                    .setImageResource(getResourceIdByName("ic_" + adapter.getPageTitle(i).toString()));
        }

        tabView.setOnClickListener(tabClickListener);

        mTabStrip.addView(tabView);
    }
}