For an ordinary button I can do this:
myButton.performClick();
and the system understands, that the button was clicked.
Now I have an ImageView. What's the alternative of this function for ImageViews?
Thanks
You can still assign an onClickListener to an image view, since the listener assignment method is a View based method. Now once the listener is added to the ImageView, you may call the onClick(ImageView) method in the listener when ever. Besides that, ImageView also has access to the performClick method that the ButtonView does. You can use the same code across views so long as you have a Listener.
Do you mean something like..
ImageView img = (ImageView) findViewById(R.id.myImageId);
img.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// your code here
}
});
Or
In xml:
<ImageView
android:clickable="true"
android:onClick="imageClick"
android:src="#drawable/myImage">
</ImageView>
In code
public class Test extends Activity {
........
........
public void imageClick(View view) {
//Implement image click function
}
You can setOnClickListener() to an ImageView as well. You have to make sure you programmatically imageView.setClickable(true) first, or in XML define android:clickable="true" to your ImageVIew..
Related
I want to create a button animation with two image on an ImageView programmiticaly only and by using onClickListener. I have two image that i can set with setImageResource() fonction but i don't know how to play the button animation before the button action launches.
Please, could you explain a bit more what you are wanting? Do you want to replace the ImageButton resource when you click on it? You can achieve it setting the OnClickListener
new View.OnClickListener() {
public void onClick(View v) {
imageButtonView.setImageResource(idOfTheNewResource);
}
};
Is this what you are wanting?
I have a custom class that inherits from RelativeLayout. I have an ImageButton called control_btn that's defined and added to the layout in an init() method. On the onDraw, I have the following code (among other things):
control_btn.setImageResource(R.drawable.play_new);
control_btn.getLayoutParams().width = getPixels(20);
control_btn.getLayoutParams().height = getPixels(20);
control_btn.setX(getPixels(0));
control_btn.setY(getPixels(10));
control_btn.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
control_btn.setAdjustViewBounds(true);
control_btn.setPadding(0, 0, 0, 0);
control_btn.setBackgroundColor(Color.TRANSPARENT);
control_btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
control_btn.setImageResource(R.drawable.pause);
Log.d("WTH", "clicked");
}
});
However, the image never changes, despite getting the log entry.
Further, I have another ImageButton with exactly the same code whose image DOES change (also inside the custom class).
Any ideas on how to fix this?
put setImageResource into init() method, then problem will be fixed.
I have a layout which contains lots of images. What I have to do is when an image is clicked, I have to show its details. But I don't want to have onClickListeners for all the images. How can I achieve this?
You don't have to have different handlers for all the images. Instead use one handler for all the images. This would make your code cleaner, manageable and solve your problem too.
public void onCreate(Bundle bundle) {
//...
OnClickListener mHandler = new OnClickListener() {
#Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.img1:
//..
break;
case R.id.img2:
//....
break;
}
}
};
ImageButton btn1 = (ImageButton)findViewById(R.id.img1);
ImageButton btn2 = (ImageButton)findViewById(R.id.img2);
//...
btn1.SetOnClickListener(mHandler);
btn2.SetOnClickListener(mHandler);
//...
}
One Listener to rule them all.
Implement onClick() on an object, register it as listener
In onClick(), examine the View object passed as parameter to determine which of the images was clicked. You can do anything from getId() to casting it to (ImageView) and getting the actual image out.
Once you know which image was clicked, do what you will with it.
If you're looking to implement custom behavior for an ImageView (or whatever), and then have multiple instances of that type of view, you should subclass the ImageView and put your listener in there. Then you've got an encapsulated View that implements the custom behavior you want, and if you decide later that you want more or less or them, or to put them in another place, it's easy to move the View and its behavior without ripping apart your Activity.
I would like to create a linear layout which would behave similarly to ImageButton.
<LinearLayout
android:id="#+id/container"
style="?WidgetHomeIconContainer">
<ImageView
android:id="#+id/icon"
style="?WidgetHomeIcon" />
<TextView
android:id="#+id/title"
style="?WidgetHomeLabel"
android:text="#string/title"
android:textAppearance="?attr/TextHomeLabel" />
</LinearLayout>
In styles of ImageView, TextView and LinearLayout, I set a selectors for all states.
Now:
when I click on ImageView (I tried it also with ImageButton) - it behaves correctly and the image is changed according the selector xml.
when I click on LinearLayout - the linear layout is clicked, but the the ImageView and TextView don't change it's drawable/appearance
So I would like to do the following. When I click on parent LinearLayout, I need to change all it's childs to pressed state.
I tried to add following code to LinearLayout onClickListener to propagate the click:
#Override
public void onClick(View v)
{
LinearLayout l = (LinearLayout) v;
for(int i = 0; i < l.getChildCount(); i++)
{
l.getChildAt(i).setClickable(true);
l.getChildAt(i).performClick();
}
}
But it still reamins the same. Thank you very much for any help.
Put
android:duplicateParentState="true"
in your ImageView and TextView..then the views get its drawable state (focused, pressed, etc.) from its direct parent rather than from itself.
Not only make for every child:
android:duplicateParentState="true"
But also additionally:
android:clickable="false"
This will prevent unexpected behaviour (or solution simply not working) if clickable child views are used.
SO Source
After having the same problem some months later, I found this solution:
private void setOnClickListeners() {
super.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
onClick(v);
}
});
for (int index = 0; index < super.getChildCount(); index++) {
View view = super.getChildAt(index);
view.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
onClick(v);
}
});
}
}
protected void onClick(View v) {
// something to do here...
}
In my case, no one of the other solutions works!
I finally had to use OnTouchListener as explained here, capturing the event when the user clicks in the parent view, and removing all childs OnClickListener.
So the idea is, delegate the click behavior to the parent, and notify the child that is really clicked, if you want to propagate the event. ¡¡That's what we are looking for!!
Then, we need to check which child has been clicked. You can find a reference here to know how it´s done. But the idea is basiclly getting the area of the child, and asking for who contains the clicked coordinates, to perform his action (or not).
At first, my child view failed to get click from parent. After investigating, what I need to do to make it work are:
remove click listener on child view
adding click listener on parent view
So, I don't need to add these on every children.
android:duplicateParentState="true"
android:clickable="false"
I only add duplicateParentState to one of my child view.
My child view is now listening to parent click event.
I want to change the image of a button in my code. I found this can be done in xml:
please check this link
but the image will not stay on after I released the button. I want to click the button
and the button change to a new image. Can this be done?
in onClick method you need to change the Button's image, this way..
public void onClick(View v) {
if(v==buttonName){
buttonName.setBackgroundResource(R.drawable.imageName);
}
}
Assuming an ImageButton...
You can change the background image in an onClick listener, similar to the following:
myButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
//set button image
myButton.setImageBitmap(myBitmapFile)
}
});