How set an image in Android ImageView - android

I need to show a image, when i press a button on my application.
I'm trying with:
ImageView imageView = (ImageView) findViewById(R.id.imageView2);
imageView.setImageDrawable(R.drawable.loading);
but it shows an error.
Anyone can help me? Thanks.

import package_name.R.drawable;
ImageView imageView = (ImageView) findViewById(R.id.imageView2);
imageView.setImageResource(drawable.loading);
OR
you can set the image from the start, set it to invisible and when you want to show it just change the visibility property.

for future visitors:
sometime setImageDrawable(Drawable) method show an error like:
The method setImageDrawable(Drawable) in the type ImageView is not applicable for the arguments (int)
can try following codes:
ImageView imageView = (ImageView) findViewById(R.id.myImageView);
Drawable image = getResources().getDrawable(R.drawable.your_image);
imageView.setImageDrawable(image);
--------- OR can try this ------------------
imageView.setImageDrawable(ContextCompat.getDrawable(YourActivity.this, R.drawable.your_image));
hope it work for you.

Related

How to convert a textview to Imageview

I am trying to get a Imageview from textview. I tried many methods but not able to set that textview on Imageview as image.
I tried following method:
TextView tv = (TextView)findViewById(R.id.textview);
tv.buildDrawingCache();
ImageView img = (ImageView)findViewById(R.id.imageview);
img.setImageBitmap(tv.getDrawingCache());
but i am not still able to show that textview on Imageview. Can someone help me that how can i set textview as image on that imageview?
Try this, if it maya help you
yourTextView.setDrawingCacheEnabled(true);
yourTextView.buildDrawingCache();
yourImageview.setImageBitmap(tv1.getDrawingCache());
call
tv.buildDrawingCache(true);
Bitmap b = tv.getDrawingCache(true).copy(Bitmap.Config.ARGB_8888, false);
tv.destroyDrawingCache();
img.setImageBitmap(b);

ReplaceAll the ImageView in FrameLayout

There are a FrameLayout and a Button,I want to add 6 ImageView onto the FrameLayout,and replaceall the 6 ImageView with other 6 ImageView when I click the Button.Thus the FrameLayout may display 12 ImageView together.(I use the Universal_ImageLoader to load Image to ImageView).How to resolve this problem?
you can set the images programmatically (not in xml),you dont need to have 12 image views.
for example set an on click listener for your button,so when you click it, you set a new image to the ImageView:
image1 = (ImageView) findViewById(R.id.imageView1);
image2 = (ImageView) findViewById(R.id.imageView2);
image3 = (ImageView) findViewById(R.id.imageView3);
image4 = (ImageView) findViewById(R.id.imageView4);
image5 = (ImageView) findViewById(R.id.imageView5);
image6 = (ImageView) findViewById(R.id.imageView6);
final Button button = (Button) findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
image1.setImageResource(R.drawable.myPic1);
image2.setImageResource(R.drawable.myPic2);
image3.setImageResource(R.drawable.myPic3);
image4.setImageResource(R.drawable.myPic4);
image5.setImageResource(R.drawable.myPic5);
image6.setImageResource(R.drawable.myPic6);
}
});
the setImageResource method sets a new picture on the ImageView its given (the imageviews here are image1, image2, image3, image4, image5, image6 ).
now we have put this method inside onClick so whenever you click your button all 6 of your images will change to the image you chose (in my example the images i chose are myPic1, myPic2, myPic3, myPic4, myPic5, myPic6 )
this way you would have replaced all the 6 ImageViews with other 6 ImageViews with a click of a Button like you wanted
I hope this helps :)

Android - Load and resize images before App start?

In "onCreate" I have this code here:
imageView1 = (ImageView) findViewById(R.id.imageView1);
imageView2 = (ImageView) findViewById(R.id.imageView2);
imageView3 = (ImageView) findViewById(R.id.imageView3);
// ...
imageView1.getLayoutParams().width = imgViewWidth;
imageView2.getLayoutParams().width = imgViewWidth;
imageView3.getLayoutParams().width = imgViewWidth;
Now,
The app starts
I see the background (solid color)
Then, the images appear
What can I do, that the resized images appear before the app starts?
You can resize it in onCreate. See this http://developer.android.com/training/basics/activity-lifecycle/index.html

How to get image from an ImageButton?

I'm not sure about the code, but I tried doing this:
ImageButton stuff = (ImageButton) createView.findViewById(R.id.stuff);
int stuff2 = stuff.getId();
champs.setImageResource(stuff2);
But that didn't work, the Imagebutton was shrinked for some reason, there was no image, just a gray rectangle.
try this
Bitmap bitmap = ((BitmapDrawable)stuff.getDrawable()).getBitmap();
stuff.getId();
This will return the id of the View not the image resource associated with it. So you won't be having a resource related to this that is why you are not seeing the Image.
Set a valid drawable to setImageResource Some thing like R.drawable.drawable_id
Try this
ImageButton stuff = (ImageButton) createView.findViewById(R.id.stuff);
Drawable d = stuff .getBackground();
champs.setBackgroundDrawable(d);
This will help get the drawable object and set the drawable object.You can also get the Bitmap object for image drawable This will set the exact drawable associated with the imagebutton
ImageButton mButton = new ImageButton(this);
Drawable drawable = mButton.getBackground();
champs.setImageDrawable(drawable)
You need to set the ID of drawable resource but you are setting ID of the ImageButton as image resource.
here stuff.getId() returns R.id.stuff.
set the valid drawable resource like
ImageButton stuff = (ImageButton) createView.findViewById(R.id.stuff);
stuff.setImageResource(R.drawable.id_of_image);

How to retrieve an image from ImageView when Click on that image?

i have a ImageView in the layout, when i click on the image i want to get that image into a variable and replace with another image in this ImageView. please help me..
The onClick Listener will give you a View, that's the ImageView that was clicked. Cast it to an ImageView and do whatever you want with it.
in this example i have take previous image in Drawable and replace i with new image. if you set any imageview to image which stay in drawable variable(d) then use :: setBackgroundDrawable(d); is useful
public void onClick(View v){
ImageView i;
i = (ImageView) findViewById(R.id.img);
Drawable d = i.getBackground();
i.setBackgroundResource(R.id.secondImage);
}

Categories

Resources