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.example.georg.theupub.ProfileActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_profile);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);/*w w  w .  j  a v  a  2s. c  o m*/

    if (!getPoints()) {
        Context context = getApplicationContext();
        CharSequence text = "Can't get points. Can't reach database!";
        int duration = Toast.LENGTH_LONG;
        Toast toast = Toast.makeText(context, text, duration);
        toast.show();
    }
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar,
            R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

    QRCodeWriter writer = new QRCodeWriter();

    try {
        // BitMatrix matrix = writer.encode(
        //  "Giorgos Demosthenous 953157", BarcodeFormat.QR_CODE, 400, 400);
        BitMatrix matrix = writer.encode(sample_user, BarcodeFormat.QR_CODE, 400, 400);
        Bitmap qrcode_bmp = toBitmap(matrix);
        ImageView iv = (ImageView) findViewById(R.id.grImage_id);
        iv.setImageResource(R.drawable.qr);
        iv.setImageBitmap(qrcode_bmp);

    } catch (WriterException e) {
        e.printStackTrace();
    }

    // Shows the points of the user
    pointText = (TextView) findViewById(R.id.Points);
    pointText.setText(userPoints + " Points");
}

From source file:com.example.alexs.tourguide.dcAdapter.java

/**
 * Provides a view for an AdapterView (ListView, GridView, etc.)
 *
 * @param position The position in the list of data that should be displayed in the
 *                 list item view.//from   w w w.  ja  v a  2  s .  com
 * @param convertView The recycled view to populate.
 * @param parent The parent ViewGroup that is used for inflation.
 * @return The View for the position in the AdapterView.
 */
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // Check if the existing view is being reused, otherwise inflate the view
    View listItemView = convertView;
    if (listItemView == null) {
        listItemView = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent, false);
    }

    // Find the TextView for the list text
    LinearLayout backColorView = (LinearLayout) listItemView.findViewById(R.id.listText);
    backColorView.setBackgroundColor(ContextCompat.getColor(getContext(), mColorID));

    // Get the {@link dcAttractions} object located at this position in the list
    dcAttractions currentAttraction = getItem(position);

    // Find the TextView in the list_item.xml layout with the ID version_name
    TextView nameTextView = (TextView) listItemView.findViewById(R.id.attraction_name);
    // Get the version name from the current dcAttractions object and
    // set this text on the name TextView
    nameTextView.setText(currentAttraction.getAttractionName());

    // Find the TextView in the list_item.xml layout with the ID version_number
    TextView summaryTextView = (TextView) listItemView.findViewById(R.id.attraction_summary);
    // Get the version number from the current dcAttractions object and
    // set this text on the number TextView
    summaryTextView.setText(currentAttraction.getAttractionSummary());

    // Find the TextView in the list_item.xml layout with the ID version_number
    TextView addressTextView = (TextView) listItemView.findViewById(R.id.attraction_address);
    // Get the version number from the current dcAttractions object and
    // set this text on the number TextView
    addressTextView.setText(currentAttraction.getAttractionAddress());

    // Find the ImageView in the list_item.xml layout with the ID list_item_icon
    ImageView iconView = (ImageView) listItemView.findViewById(R.id.list_item_icon);
    // Get the image resource ID from the current dcAttractions object and
    // set the image to iconView
    if (currentAttraction.hasImage()) {
        iconView.setImageResource(currentAttraction.getImageResourceId());
    } else {
        iconView.setVisibility(View.GONE);
    }

    // Return the whole list item layout (containing 2 TextViews and an ImageView)
    // so that it can be shown in the ListView
    return listItemView;
}

From source file:com.example.android.gft.WordAdapter.java

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // Check if an existing view is being reused, otherwise inflate the view
    View listItemView = convertView;
    if (listItemView == null) {
        listItemView = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent, false);
    }/*from   w  ww . ja  v a 2 s  . co m*/

    // Get the {@link Word} object located at this position in the list
    Word currentWord = getItem(position);

    // Find the TextView in the list_item.xml layout with the ID miwok_text_view.
    TextView miwokTextView = (TextView) listItemView.findViewById(R.id.miwok_text_view);
    // Get the Miwok translation from the currentWord object and set this text on
    // the Miwok TextView.
    miwokTextView.setText(currentWord.getMiwokTranslationId());

    // Find the TextView in the list_item.xml layout with the ID default_text_view.
    TextView defaultTextView = (TextView) listItemView.findViewById(R.id.default_text_view);
    // Get the default translation from the currentWord object and set this text on
    // the default TextView.
    defaultTextView.setText(currentWord.getDefaultTranslationId());

    // Find the ImageView in the list_item.xml layout with the ID image.
    ImageView imageView = (ImageView) listItemView.findViewById(R.id.image);
    // Check if an image is provided for this word or not
    if (currentWord.hasImage()) {
        // If an image is available, display the provided image based on the resource ID
        imageView.setImageResource(currentWord.getImageResourceId());
        // Make sure the view is visible
        imageView.setVisibility(View.VISIBLE);
    } else {
        // Otherwise hide the ImageView (set visibility to GONE)
        imageView.setVisibility(View.GONE);
    }

    // Set the theme color for the list item
    View textContainer = listItemView.findViewById(R.id.text_container);
    // Find the color that the resource ID maps to
    int color = ContextCompat.getColor(getContext(), mColorResourceId);
    // Set the background color of the text container View
    textContainer.setBackgroundColor(color);

    // Return the whole list item layout (containing 2 TextViews) so that it can be shown in
    // the ListView.
    return listItemView;
}

From source file:com.example.android.Spanish.WordAdapter.java

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // Check if an existing view is being reused, otherwise inflate the view
    View listItemView = convertView;
    if (listItemView == null) {
        listItemView = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent, false);
    }//from   w w  w  .  j  a v a  2 s .c  om

    // Get the {@link Word} object located at this position in the list
    Word currentWord = getItem(position);

    // Find the TextView in the list_item.xml layout with the ID miwok_text_view.
    TextView miwokTextView = (TextView) listItemView.findViewById(R.id.miwok_text_view);
    // Get the Miwok translation from the currentWord object and set this text on
    // the Miwok TextView.
    miwokTextView.setText(currentWord.getSpanishTranslation());

    // Find the TextView in the list_item.xml layout with the ID default_text_view.
    TextView defaultTextView = (TextView) listItemView.findViewById(R.id.default_text_view);
    // Get the default translation from the currentWord object and set this text on
    // the default TextView.
    defaultTextView.setText(currentWord.getDefaultTranslation());

    // Find the ImageView in the list_item.xml layout with the ID image.
    ImageView imageView = (ImageView) listItemView.findViewById(R.id.image);
    // Check if an image is provided for this word or not
    if (currentWord.hasImage()) {
        // If an image is available, display the provided image based on the resource ID
        imageView.setImageResource(currentWord.getImageResourceId());
        // Make sure the view is visible
        imageView.setVisibility(View.VISIBLE);
    } else {
        // Otherwise hide the ImageView (set visibility to GONE)
        imageView.setVisibility(View.GONE);
    }

    // Set the theme color for the list item
    View textContainer = listItemView.findViewById(R.id.text_container);
    // Find the color that the resource ID maps to
    int color = ContextCompat.getColor(getContext(), mColorResourceId);
    // Set the background color of the text container View
    textContainer.setBackgroundColor(color);

    // Return the whole list item layout (containing 2 TextViews) so that it can be shown in
    // the ListView.
    return listItemView;
}

From source file:com.example.android.tourguide.WordAdapter.java

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // Check if an existing view is being reused, otherwise inflate the view
    View listItemView = convertView;
    if (listItemView == null) {
        listItemView = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent, false);
    }//from w  w w . j a v  a 2  s.  co  m

    // Get the {@link Word} object located at this position in the list
    Word currentWord = getItem(position);

    // Find the TextView in the list_item.xml layout with the ID miwok_text_view.
    TextView miwokTextView = (TextView) listItemView.findViewById(R.id.tourguide_text_view);
    // Get the Miwok translation from the currentWord object and set this text on
    // the Miwok TextView.
    miwokTextView.setText(currentWord.getMiwokTranslation());

    // Find the TextView in the list_item.xml layout with the ID default_text_view.
    TextView defaultTextView = (TextView) listItemView.findViewById(R.id.default_text_view);
    // Get the default translation from the currentWord object and set this text on
    // the default TextView.
    defaultTextView.setText(currentWord.getDefaultTranslation());

    // Find the ImageView in the list_item.xml layout with the ID image.
    ImageView imageView = (ImageView) listItemView.findViewById(R.id.image);
    // Check if an image is provided for this word or not
    if (currentWord.hasImage()) {
        // If an image is available, display the provided image based on the resource ID
        imageView.setImageResource(currentWord.getImageResourceId());
        // Make sure the view is visible
        imageView.setVisibility(View.VISIBLE);
    } else {
        // Otherwise hide the ImageView (set visibility to GONE)
        imageView.setVisibility(View.GONE);
    }

    // Set the theme color for the list item
    View textContainer = listItemView.findViewById(R.id.text_container);
    // Find the color that the resource ID maps to
    int color = ContextCompat.getColor(getContext(), mColorResourceId);
    // Set the background color of the text container View
    textContainer.setBackgroundColor(color);

    // Return the whole list item layout (containing 2 TextViews) so that it can be shown in
    // the ListView.
    return listItemView;
}

From source file:com.example.shubh.miwok.WordAdapter.java

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // Check if an existing view is being reused, otherwise inflate the view
    View listItemView = convertView;
    if (listItemView == null) {
        listItemView = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent, false);
    }//from  w  ww  .  j a va 2s  .c om

    // Get the {@link Word} object located at this position in the list
    Word currentWord = getItem(position);

    // Find the TextView in the list_item.xml layout with the ID miwok_text_view.
    TextView miwokTextView = (TextView) listItemView.findViewById(R.id.miwok_text_view);
    // Get the Miwok translation from the currentWord object and set this text on
    // the Miwok TextView.
    miwokTextView.setText(currentWord.getMiwokTranslation());

    // Find the TextView in the list_item.xml layout with the ID default_text_view.
    TextView defaultTextView = (TextView) listItemView.findViewById(R.id.default_text_view);
    // Get the default translation from the currentWord object and set this text on
    // the default TextView.
    defaultTextView.setText(currentWord.getDefaultTranslation());

    // Find the ImageView in the list_item.xml layout with the ID image.
    ImageView imageView = (ImageView) listItemView.findViewById(R.id.image);
    // Check if an image is provided for this word or not
    if (currentWord.hasImage()) {
        // If an image is available, display the provided image based on the resource ID
        imageView.setImageResource(currentWord.getImageResourceId());
        // Make sure the view is visible
        imageView.setVisibility(View.VISIBLE);
    } else {
        // Otherwise hide the ImageView (set visibility to GONE)
        imageView.setVisibility(View.GONE);
    }

    // Set the theme color for the list item
    View textContainer = listItemView.findViewById(R.id.text_container);
    // Find the color that the resource ID maps to
    int color = ContextCompat.getColor(getContext(), mColorResourceId);
    // Set the background color of the text container View
    textContainer.setBackgroundColor(color);

    // Return the whole list item layout (containing 2 TextViews) so that it can be shown in
    // the ListView.
    return listItemView;
}

From source file:com.fugueweb.pub.animation.ZoomActivity.java

/**
 * "Zooms" in a thumbnail view by assigning the high resolution image to a hidden "zoomed-in"
 * image view and animating its bounds to fit the entire activity content area. More
 * specifically:/*from ww  w . ja va2  s .  co  m*/
 *
 * <ol>
 *   <li>Assign the high-res image to the hidden "zoomed-in" (expanded) image view.</li>
 *   <li>Calculate the starting and ending bounds for the expanded view.</li>
 *   <li>Animate each of four positioning/sizing properties (X, Y, SCALE_X, SCALE_Y)
 *       simultaneously, from the starting bounds to the ending bounds.</li>
 *   <li>Zoom back out by running the reverse animation on click.</li>
 * </ol>
 *
 * @param thumbView  The thumbnail view to zoom in.
 * @param imageResId The high-resolution version of the image represented by the thumbnail.
 */
private void zoomImageFromThumb(final View thumbView, int imageResId) {
    // If there's an animation in progress, cancel it immediately and proceed with this one.
    if (mCurrentAnimator != null) {
        mCurrentAnimator.cancel();
    }

    // Load the high-resolution "zoomed-in" image.
    final ImageView expandedImageView = (ImageView) findViewById(R.id.expanded_image);
    expandedImageView.setImageResource(imageResId);

    // Calculate the starting and ending bounds for the zoomed-in image. This step
    // involves lots of math. Math.
    final Rect startBounds = new Rect();
    final Rect finalBounds = new Rect();
    final Point globalOffset = new Point();

    // The start bounds are the global visible rectangle of the thumbnail, and the
    // final bounds are the global visible rectangle of the container view. Also
    // set the container view's offset as the origin for the bounds, since that's
    // the origin for the positioning animation properties (X, Y).
    thumbView.getGlobalVisibleRect(startBounds);
    findViewById(R.id.container).getGlobalVisibleRect(finalBounds, globalOffset);
    startBounds.offset(-globalOffset.x, -globalOffset.y);
    finalBounds.offset(-globalOffset.x, -globalOffset.y);

    // Adjust the start bounds to be the same aspect ratio as the final bounds using the
    // "center crop" technique. This prevents undesirable stretching during the animation.
    // Also calculate the start scaling factor (the end scaling factor is always 1.0).
    float startScale;
    if ((float) finalBounds.width() / finalBounds.height() > (float) startBounds.width()
            / startBounds.height()) {
        // Extend start bounds horizontally
        startScale = (float) startBounds.height() / finalBounds.height();
        float startWidth = startScale * finalBounds.width();
        float deltaWidth = (startWidth - startBounds.width()) / 2;
        startBounds.left -= deltaWidth;
        startBounds.right += deltaWidth;
    } else {
        // Extend start bounds vertically
        startScale = (float) startBounds.width() / finalBounds.width();
        float startHeight = startScale * finalBounds.height();
        float deltaHeight = (startHeight - startBounds.height()) / 2;
        startBounds.top -= deltaHeight;
        startBounds.bottom += deltaHeight;
    }

    // Hide the thumbnail and show the zoomed-in view. When the animation begins,
    // it will position the zoomed-in view in the place of the thumbnail.
    thumbView.setAlpha(0f);
    expandedImageView.setVisibility(View.VISIBLE);

    // Set the pivot point for SCALE_X and SCALE_Y transformations to the top-left corner of
    // the zoomed-in view (the default is the center of the view).
    expandedImageView.setPivotX(0f);
    expandedImageView.setPivotY(0f);

    // Construct and run the parallel animation of the four translation and scale properties
    // (X, Y, SCALE_X, and SCALE_Y).
    AnimatorSet set = new AnimatorSet();
    set.play(ObjectAnimator.ofFloat(expandedImageView, View.X, startBounds.left, finalBounds.left))
            .with(ObjectAnimator.ofFloat(expandedImageView, View.Y, startBounds.top, finalBounds.top))
            .with(ObjectAnimator.ofFloat(expandedImageView, View.SCALE_X, startScale, 1f))
            .with(ObjectAnimator.ofFloat(expandedImageView, View.SCALE_Y, startScale, 1f));
    set.setDuration(mShortAnimationDuration);
    set.setInterpolator(new DecelerateInterpolator());
    set.addListener(new AnimatorListenerAdapter() {
        @Override
        public void onAnimationEnd(Animator animation) {
            mCurrentAnimator = null;
        }

        @Override
        public void onAnimationCancel(Animator animation) {
            mCurrentAnimator = null;
        }
    });
    set.start();
    mCurrentAnimator = set;

    // Upon clicking the zoomed-in image, it should zoom back down to the original bounds
    // and show the thumbnail instead of the expanded image.
    final float startScaleFinal = startScale;
    expandedImageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (mCurrentAnimator != null) {
                mCurrentAnimator.cancel();
            }

            // Animate the four positioning/sizing properties in parallel, back to their
            // original values.
            AnimatorSet set = new AnimatorSet();
            set.play(ObjectAnimator.ofFloat(expandedImageView, View.X, startBounds.left))
                    .with(ObjectAnimator.ofFloat(expandedImageView, View.Y, startBounds.top))
                    .with(ObjectAnimator.ofFloat(expandedImageView, View.SCALE_X, startScaleFinal))
                    .with(ObjectAnimator.ofFloat(expandedImageView, View.SCALE_Y, startScaleFinal));
            set.setDuration(mShortAnimationDuration);
            set.setInterpolator(new DecelerateInterpolator());
            set.addListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    thumbView.setAlpha(1f);
                    expandedImageView.setVisibility(View.GONE);
                    mCurrentAnimator = null;
                }

                @Override
                public void onAnimationCancel(Animator animation) {
                    thumbView.setAlpha(1f);
                    expandedImageView.setVisibility(View.GONE);
                    mCurrentAnimator = null;
                }
            });
            set.start();
            mCurrentAnimator = set;
        }
    });
}

From source file:com.maxwen.wallpaper.board.fragments.WallpaperSearchFragment.java

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_wallpaper_search, menu);
    MenuItem search = menu.findItem(R.id.menu_search);
    int color = ColorHelper.getAttributeColor(getActivity(), R.attr.toolbar_icon);
    search.setIcon(DrawableHelper.getTintedDrawable(getActivity(), R.drawable.ic_toolbar_search, color));

    mSearchView = (SearchView) MenuItemCompat.getActionView(search);
    mSearchView.setImeOptions(EditorInfo.IME_FLAG_NO_EXTRACT_UI | EditorInfo.IME_ACTION_SEARCH);
    mSearchView.setQueryHint(getActivity().getResources().getString(R.string.menu_search));
    mSearchView.setMaxWidth(Integer.MAX_VALUE);

    MenuItemCompat.expandActionView(search);
    mSearchView.setIconifiedByDefault(false);
    mSearchView.clearFocus();/*from w w w  . j  a  va  2  s .c o  m*/

    ViewHelper.changeSearchViewTextColor(mSearchView, color, ColorHelper.setColorAlpha(color, 0.6f));
    View view = mSearchView.findViewById(android.support.v7.appcompat.R.id.search_plate);
    if (view != null)
        view.setBackgroundColor(Color.TRANSPARENT);

    ImageView closeIcon = (ImageView) mSearchView
            .findViewById(android.support.v7.appcompat.R.id.search_close_btn);
    if (closeIcon != null)
        closeIcon.setImageResource(R.drawable.ic_toolbar_close);

    ImageView searchIcon = (ImageView) mSearchView
            .findViewById(android.support.v7.appcompat.R.id.search_mag_icon);
    ViewHelper.removeSearchViewSearchIcon(searchIcon);

    mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {

        @Override
        public boolean onQueryTextChange(String string) {
            filterSearch(string);
            return true;
        }

        @Override
        public boolean onQueryTextSubmit(String string) {
            mSearchView.clearFocus();
            return true;
        }
    });
}

From source file:com.cleanwiz.applock.ui.activity.StepActivity.java

private void initPoints(int pagers) {

    layout_points = (LinearLayout) findViewById(R.id.layout_point);
    layout_points.removeAllViews();//  ww w .j a  v a2s  .co m
    points = new ArrayList<ImageView>();
    int width = ScreenUtil.dip2px(mContext, 24);
    int height = ScreenUtil.dip2px(mContext, 16);
    for (int i = 0; i < pagers; i++) {
        ImageView iv = new ImageView(mContext);
        iv.setScaleType(ScaleType.CENTER_INSIDE);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(width, height);
        if (i == 0) {
            iv.setImageResource(R.drawable.pager_point_white);
        } else {
            iv.setImageResource(R.drawable.pager_point_green);
        }
        layout_points.addView(iv, lp);
        points.add(iv);
    }

}

From source file:com.akop.bach.activity.RibbonedMultiPane.java

protected void updateRibbon() {
    View view = findViewById(R.id.title_icon);
    if (view instanceof ImageButton) {
        ImageButton button = (ImageButton) view;
        button.setOnClickListener(new View.OnClickListener() {
            @Override// w  w  w  .j  av  a2 s  .  c  om
            public void onClick(View v) {
                if (mAccount != null)
                    mAccount.open(RibbonedMultiPane.this);
            }
        });
    }

    Bitmap bmp = null;
    String iconUrl = (mAccount != null) ? mAccount.getIconUrl() : null;

    if (iconUrl != null) {
        ImageCache ic = ImageCache.getInstance();
        if ((bmp = ic.getCachedBitmap(iconUrl)) != null)
            mHandler.updateAvatar(bmp);

        if (ic.isExpired(iconUrl, sCp))
            ic.requestImage(iconUrl, mRibbonImageListener, 0, null, sCp);
    } else {
        ImageView iv = (ImageView) findViewById(R.id.title_icon);
        if (iv != null)
            iv.setImageResource(R.drawable.icon);
    }

    String title = getBachTitle();
    String subtitle = getSubtitle();

    TextView tv;

    if ((tv = (TextView) findViewById(R.id.title_gamertag)) != null)
        tv.setText(title);

    if ((tv = (TextView) findViewById(R.id.ribbon_line_1)) != null)
        tv.setText(subtitle);

    setTitle(title);
    if (android.os.Build.VERSION.SDK_INT >= 11) {
        new ActionBarHelper().setSubtitle(subtitle);
    }
}