Placing one image over another image in android - android

I have a imageButton in android which user clicks. Then after his click I want t0 show a tick or a cross image on the toip of it.
How is it possible?
<ImageButton android:layout_width="wrap_content"
android:text="Button" android:id="#+id/button1"
android:layout_height="wrap_content" android:layout_gravity="center_horizontal"
android:background="#drawable/rhino" android:layout_marginRight="20dp"></ImageButton>

put your ImageButton and the tick image in a FrameLayout and make the visbility of the Tick image "Invisible" . So when you click on the ImageButton then change the state of Tick Image to Visible.

Get a reference to your ImageButton and then use one of its setImage methods, such as setImageResource.

You can achieve the same using ImageView. Use ImageView
testimage = (ImageView) findViewById(R.id.imageview);
testimage.setOnClickListener(listener);
write the logic to set both type of image to imageview in onclick event
public OnClickListener listener=new OnClickListener(){
#Override
public void onClick(View arg0) {
System.out.println("..set image button..");
Drawable[] layers = new Drawable[2];
layers[0] = getResources().getDrawable(R.drawable.btn_call);
layers[1] = getResources().getDrawable(R.drawable.blue_unfocus);
System.out.println(layers[1]+"...Drawable..."+layers[0]);
LayerDrawable layerDrawable = new LayerDrawable(layers);
testimage.setImageDrawable(layerDrawable);
}
};
Thanks
Deepak

Related

ImageSwitcher image won't display onCreate()

I can't figure out why my ImageSwitcher won't display the image when the Activity first loads. I've set the image resource in the onCreate method but the image seems to be invisible until I start the animation. The animation should slide one image out and a second image in. When I click the button to start the animation, I can see the original image slide out from where it should be but I can't see it at the start. I've tried setting the image within the ViewFactory and just outside but it doesn't seem to work
Here's my imageSwitcher code:
mImageSwitcher = (ImageSwitcher)findViewById(R.id.imageSwitcher);
mImageSwitcher.setOnClickListener(mImageSwitcherClickListener);
mImageSwitcher.setFactory(new ViewFactory() {
#Override
public View makeView() {
ImageView view = new ImageView(getApplicationContext());
view.setScaleType(ImageView.ScaleType.CENTER);
view.setLayoutParams(new
ImageSwitcher.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
view.setImageResource(R.mipmap.image);
return view;
}
});
mImageSwitcher.setImageResource(R.mipmap.image);
Here is the layout for the Activity:
<ImageSwitcher
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/imageSwitcher"
android:layout_centerHorizontal="true"
android:layout_gravity="bottom|center"
android:alpha=".5"
android:scaleX="4"
android:scaleY="4"
android:layout_marginBottom="150dp"
android:animateFirstView="true"/>
In tutorials it shows you have to use animation
try this
Animation in = AnimationUtils.loadAnimation(this,android.R.anim.slide_in_left);
imageSwitcher.setInAnimation(in);
imageSwitcher.setOutAnimation(out);
reference from this link

how to add zoom effect in imageview

I want to show a image in a activity . I successfully made it with imageview.But it has no zoom effect ie. I can't zoom the pic. now how can I add this zoom efect.
my .xml code is :
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:src="#drawable/cat" />
use the lib https://github.com/chrisbanes/PhotoView
In code
ImageView mImageView;
PhotoViewAttacher mAttacher;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Any implementation of ImageView can be used!
mImageView = (ImageView) findViewById(R.id.iv_photo);
// Set the Drawable displayed
Drawable bitmap = getResources().getDrawable(R.drawable.wallpaper);
mImageView.setImageDrawable(bitmap);
// Attach a PhotoViewAttacher, which takes care of all of the zooming functionality.
mAttacher = new PhotoViewAttacher(mImageView);
}
// If you later call mImageView.setImageDrawable/setImageBitmap/setImageResource/etc then you just need to call
attacher.update();

Clickable image layers with transparent background

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.

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);
}

Change ImageView

I have set up an ImageView in main.xml, if I want to access it from my View class and make it visible = false, how can I do this programatically?
Thank you
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
theView = new GameView(this);
theView.setBackgroundResource(R.layout.main);
setContentView(theView);
For example, in your layout xml file, there is a imageview1
<ImageView
android:id="#+id/imageview1"
android:layout_gravity="center"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
in your java src,
ImageView img=(ImageView)findViewById(R.id.imageview1);
img.setVisibility(View.GONE);
img.setVisibility(View.VISIBLE);
img.setVisibility(View.INVISIBLE);
you can google the difference between View.Gone and View.VISIBLE
First you should retrieve a link to this view object. Than set visibility property of this object to View.INVISIBLE
ImageView imageView = (ImageView)findViewById(R.id.image_view);
imageView.setVisibility(View.INVISIBLE);
Use this property in the xml of that Imageview
android:visibility="visible"
and change the visibility programatically like this on some particular event:
image.setVisibility(ImageView.GONE)
where image is the instance of that imageview received through findViewById()
ImageView myView = (ImageView) findViewById(R.id.myView);
myView.setVisibility (android.View.INVISIBLE);
http://developer.android.com/reference/android/view/View.html#INVISIBLE
http://developer.android.com/reference/android/view/View.html#findViewById%28int%29
Suppose you have a ImageView called imageView.
Now access the imageView like
imageView=(ImageView) findViewById(R.id.your_image_view);
Now when you are trying to hide the imageView just use imageView.setVisibility(View.INVISIBLE);
Hope this will help you

Categories

Resources