how to use Imageview as Clickable? - android

How can I use imageview as a button(clickable) in my project? As of now i am using button object but i want to display an image and not a button.
Thanks in advance
android

ImageView do listen to click events i.e. OnClickListener. So if you plan to use them as clickable you do not need any additional set up for it.
Simply create an ImageView,
<ImageView
android:id="#+id/imgClickable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="your image"
/>
Then get its reference in your Java code,
ImageView clikableImage = (ImageView) findViewById(R.id.imgClickable);
clikableImage .setOnClickListener(this); //This make it clickable
And finally handle its onClick event by overriding
#override
public void onClick(View v){
//Your action
}

You can use android:clickable="true" attribute. And then you can use onClickListener to implement a listener.
But if you want to use an image instead of button you should consider ImageButton.
http://developer.android.com/reference/android/widget/ImageButton.html

It's pretty simple. Either in your layout when you define the ImageView you can add android:onClick="someFunction" and which calls someFunction whenever you click the image (make sure you define it in your code) or you can programmatically just add an onClickListener to the ImageView.

You can include the ImageView wherever you want on your layout and implement the OnClickListener interface to listen for click events. It would be something like:
ImageView image = (ImageView) findViewById(R.id.your_id);
imager.setOnClickListener(new View.OnClickListener() {
// Do something
});

Wll how about using ImageButton.
Uoc can find it when you open a layout xml file, and at the Grapichal layout - You would see ImageButton unter the Images&Media options menu.

Related

Why when I use setClickable(true) it's not show the action of click

Actually, I use a ListView and when I use setClickable(false) I have the animation as if I clicked on a button you see? The animation that shows you click. Which is not normal, I think, basic.
And when I use setClickable(true) I no longer have the animation, as well as if I use
convertView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
And i would like to use the OnClickListener but I think it would be better for the user to see that he can click, so to have the animation when you click.
So, I'd like to see when the user clicks on an item in the list, it does the action I want (I'll add that later) but let's imagine a Toast but it displays the effect as if you click on a button. The effect i got if i use setClickable(false) (the default setting).
That's the ripple effect !
In the row's layout of the ListView just add:
android:background="?android:attr/selectableItemBackground"
android:clickable="true"
android:focusable="true"
This will add the Ripple effect. If you want to show it on top of the other views, use the forground attribute:
android:foreground="?android:attr/selectableItemBackground"
android:clickable="true"
android:focusable="true"
add this foreground:?attr/selectableItemBackground to your view attribute, it should work

RadioButton with more than text

I'm trying to create a RadioGroup with RadioButtons that have more than text only.
The image below will make things easier to understand.
I want the RadioButton to be selected when the user clicks somewhere in the layout around it.
I've tried do do somthing lik this:
<RadioGroup>
<RelativeLayout>
<RadioButton>
<...other stuff>
</RelativeLayout>
</RadioGroup>
But that didn't work.
Does somebody know how to easily get the requested layout and behaviour? Thanks
EDIT:
I forgot to mention that I don't want to use a list. I want all items to be visible because there are maximum three. Also, the container is a ScrollView
As Der Golem said you can use ListView and set it to be in a single choice mode: list.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
But if you need only few layouts you can register Click listener to your layouts and call onClick events on RadioButtons:
bigLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
radioButton.performClick();
}
});
Hope it will help

How to bring an image to front?

I'm translating an image from somewhere to another, and where it goes there is a button. But i want it to be unclickable when the image's over it! It's not seen but still can be clicked! How can i bring the image to front, so the button will not be worked? Thanks in advance.
Implement setAnimationListener in your translate animation and in onAnimationStart(Animation animation) put your bringToFront() method. It does work for me.
Try this:
Button btn = (Button)findViewById(R.id.my_btn);
btn.setEnabled(false);
I think it could help if you make that the ImageView clickable.
This could be achieved by:
Setting the clickable attribute (http://developer.android.com/reference/android/view/View.html#attr_android:clickable) to true in the layout XML
Invoking setClickable(true) (http://developer.android.com/reference/android/view/View.html#setClickable%28boolean%29) on the ImageView object in your code
Change the order of your xml file. The Views that are listed first will be behind the Views that are listed after them.
<ImageView android:id="#+id/iv01"/>
<Button android:id="#+id/btn01"/>
In the code above the button will be placed on top of the ImageView.
<Button android:id="#+id/btn01"/>
<ImageView android:id="#+id/iv01"/>
In the code above the ImageView will be placed on top of the Button.
You can also hide the Button from the layout in your .java (Activity) file.
Button btn = findViewById(R.id.btn01);
btn.setVisibility(View.GONE);
Also you can set the Butt

Robotium: how to use clickOnButton(<name>) for custom buttons

I am new to Android robotium. I am having custom widgets(MyButton, MyTextView, MyCheckBox etc..,) which got inherited from native android widgets. How can i add a click event for my custom controls in a robotium script?
I tried using Solo.clickOnButton("Test Button") where the "Test Button" is an instance of MyButton, but i am not getting click event for the Button. Any suggestions would be really helpful.
Thanks,
-Ron..
I suposse you create the MyButton using extend Button etc etc
Well to asign click action you should use the normal form. For expample
main.xml:
<com.Ron.MyButton
android:id="#+id/custom_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
In your code you can acces to that button
Button myButton = (Button)findViewById(R.id.custom_button);
And then asign onClick action like you do with other normal button:
myButton.setOnclickListener(new onclickListener ....
Other method to asing onClickAction to all views is use int the xml :
<com.Ron.MyButton
android:id="#+id/custom_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="nameOfMethod"/>
And then in your code:
public void nameOfMethod (View v){
//code when click the view
}
(This works with all view, linearlayout, imageviews, bustom button .... all )

android image button

How can i create a button with no text and an image centered horizontally ?
I don't want to use an ImageButton because I want to define a different backgound image
You just use an ImageButton and make the background whatever you want and set the icon as the src.
<ImageButton
android:id="#+id/ImageButton01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/album_icon"
android:background="#drawable/round_button" />
just use a Button with android:drawableRight properties like this:
<Button android:id="#+id/btnNovaCompra" android:layout_width="wrap_content"
android:text="#string/btn_novaCompra"
android:gravity="center"
android:drawableRight="#drawable/shoppingcart"
android:layout_height="wrap_content"/>
You can just set the onClick of an ImageView and also set it to be clickable, Or set the drawableBottom property of a regular button.
ImageView iv = (ImageView)findViewById(R.id.ImageView01);
iv.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
You can use the button :
1 - make the text empty
2 - set the background for it
+3 - you can use the selector to more useful and nice button
About the imagebutton you can set the image source and the background the same picture and it must be (*.png) when you do it you can make any design for the button
and for more beauty button use the selector //just Google it ;)

Categories

Resources