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 :)
Related
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);
I have two images with transparent background.
I added both to the layout view.
I need to assign an onClickListener to each image.
The problem is that only the topmost image is firing the click event (is like if the top most image cover the rest and don't care about its transparent background). Both images are .png with transparent background
Here is the code:
ImageView img1, img2;
RelativeLayout l = (RelativeLayout)findViewById(R.id.layout1);
bm1 = BitmapFactory.decodeResource(getResources(), R.drawable.image1);
bm2 = BitmapFactory.decodeResource(getResources(), R.drawable.image2);
img1 = new ImageView(this);
img1.setImageBitmap(bm1);
l.addView(img1);
img2 = new ImageView(this);
img2.setImageBitmap(bm2);
l.addView(img1);
img1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
//code to process when img1 is clicked
}
});
img2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
//code to process when img2 is clicked
}
});
In this example, only img2 is firing the event.
I need that both img2 and img1 can fire their events.
Thanks in advance for your help.
If the transparent background of one image covers the second, then the first will respond as being clicked, because the transparent background is counted as part of the image.
Perhaps the simplest answer would be a little bit of photo-editing to remove some of the transparent background. Crop the images a bit.
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
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);
}
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.