Android - ImageView On Click - android

Im trying to trace a click when my image view is clicked, but I cannot seem to pick up the touch, any ideas?
imgView.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
Log.v(TAG, " click");
}
});

Images normally aren't clickable, therefore I would suggest that you put the definition
android:clickable="true"
android:focusable="true"
in your layout.xml to the imageView.
If this is not helping, please edit+update your question and show us the part of your layout, where you define the image to have a look at it.
EDIT:
Tried it, and your code worked with me too - even with the upper case id name.
Can you please have a close look at your LogCat?
Mine sometimes doesn't really update, as long as I don't choose the device again.
To do so in Eclipse, go to the View "Devices" (or show it first via "Window") and click once on your device / virtual device.
If you still don't find your log entry in the LogCat-View, try to create a filter (via the green plus and giving it the String you defined with TAG as "by Log Tag").
Have a look at android developers > Using DDMS at the section "Using LogCat"

You forgot to override the onClick method. This works for me and should do for you as well :-)
imgView.setOnClickListener(new View.OnClickListener() {
//#Override
public void onClick(View v) {
Log.v(TAG, " click");
}
});
Here's to get the X and Y coordinates:
imgView.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// X = event.getX()
// Y = event.getY()
return false;
}
});

1) First make your image view clickable by adding-> android:clickable="true" .
2) Then go to your java file and add declare a variable
for example-> private static ImageView imgview;
3) Then add this functionality:
public void OnclickButtonListener() {
imgview = findViewById(R.id.imageView3);
imgview.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent=new Intent(timetable.this,MapsActivity.class);
startActivity(intent);
}
});
}
and call this function in the constructor.
Hopefully this would work.

Related

how to check which image view i have added in xml programmatically?

I added some imageviews in xml layout. Now programmatically i need to check which imageView i have added from the xml file and if same image then on click of that image i need to replace new image and visa versa.
Is the way i implemented it good or can I find a better solution for this?
This is my code so far:
holder.bankTickImg.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (holder.bankTickImg.getDrawable().getConstantState().equals(context.getResources().getDrawable(R.drawable.black_untick))) {
holder.bankTickImg.setImageResource(R.drawable.blue_tick);
}else{
holder.bankTickImg.setImageResource(R.drawable.black_untick);
}
}
});
Imageview Code
<ImageView
android:id="#+id/defaultBankTick"
android:layout_width="#dimen/TwentyFour"
android:layout_height="#dimen/TwentyFour"
android:layout_alignParentRight="true"
android:layout_marginBottom="#dimen/FiftyFive"
android:layout_marginRight="#dimen/Thirty"
android:layout_marginTop="#dimen/FiftyFive"
app:srcCompat="#drawable/black_untick" />
If i got your question.
Why don't you use a integer/boolean variable to save the state. eg.
lets say, you start with
int clicked = 0;
then in on click check if its value it 0 then treat it as fresh and change the value to 1.
and if the value is already 1 then treat it as it clicked 2nd time and change it to 0 .
There are many ways to check imageView which you added
holder.bankTickImg.setImageResource(R.drawable.black_untick); //default image , you can also set in xml code
holder.bankTickImg.setId(0);//you also do with string setTag();
holder.bankTickImg.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
int istick=holder.bankTickImg.getId();
if (istick==1)
{
holder.bankTickImg.setImageResource(R.drawable.blue_tick);
}
else
{
holder.bankTickImg.setImageResource(R.drawable.black_untick);
}
}
});

GIF Listener in android

i'm using this How to animate .gif images in an android? solution (first answer of Shobhit Puri) to implements in my app some Gif. now i want to add some Listener on them (click listner).
every single procedure i tried didn't worked.
the idea is:
the gif is made by 3 image, when you click the first image the gif will change in another one, and so on.
Thanks for your attention.
EDIT:
actualy i'm trying this way
findViewById(R.id.ivAnimation).setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
Log.i(TAG, "onClickGif PrincipalActivity");
}
});
on onCreate() of the mainActivity. the Log is well generated but i can't go over this
I'm not sure about gifs, but if you have separate files, you could do something like this.
Warning: Untested :)
final int[] imgs = new int[] {R.drawable.img1, R.drawable.img2, R.drawable.img3};
int position = 0;
final ImageView gifView = (ImageView) findViewById(R.id.ivAnimation);
gifView.setImageResource(imgs[position]);
gifView.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
Log.i(TAG, "onClickGif PrincipalActivity");
position = (position + 1) % imgs.length; // wrap around to "restart" gif
gifView.setImageResource(imgs[position]);
}
});

Android ImageButton's image to be replaced with another image

I have an ImageButton and I want that onClick would replace it with another image (flip back and forth) and on a long press, would replace it to another image.
How can I do that?
I don't feel like reading long documentaries for this.
Set onClickListeners for your button then change the drawable. Since you don't have any code, the following is based on a dynamic ImageButton that only outlines how to perform the action you want. I suggest you define your ImageButton in your XML layout first and then use
iBtn = (ImageButton) findViewById(R.id.btnID);
ImageButton iBtn = new ImageButton(this);
iBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
iBtn.setImageDrawable(getResources().getDrawable(R.drawable.img1);
}
});
iBtn.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
iBtn.setImageDrawable(getResources().getDrawable(R.drawable.img2);
return true;
}
});
If you're going to learn Android (or any language or platform really), you should really get comfortable reading the documentation provided, as it will give you answers to many basic questions, such as how to use various methods and classes.
That aside, you need to set both an OnClickListener and an OnLongClickListener for your button. Then inside those listeners, you'll need to set the image using the setImageResource() method. That method requires a drawable image, which you should have saved in your drawable folder (if not, put it there!)
You didn't post your existing code, so here's a generic example.
ImageButton button = (ImageButton) findViewById(R.id.img_button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
button.setImageResource(R.drawable.pic1);
}
});
button.setOnLongClickListener(new OnLongClickListener() {
public boolean onLongClick(View v) {
button.setImageResource(R.drawable.pic2);
return true; // <-- This must be true.
}
});
You could read further about how to use any buttons in the button guide, you'll just be swapping for ImageButton where appropriate.
Add ImageButton to your layout :
<ImageButton
android:id="#+id/img_btn1"
android:src="#drawable/imgc"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
and then add this code to your Activity Oncreate() method
ImageButton imageButton;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageButton = (ImageButton) findViewById(R.id.img_btn1);
imageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
imageButton.setImageResource(R.drawable.imga);
}
});
imageButton.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
imageButton.setImageResource(R.drawable.imgb);
return true;
}
});
}
change imga , imgb, imgc names according to your images taht are placed in drawable folder

how to use zoom button in a view android

I've always wanted to create an Android activity which uses zoombutton not the zoomcontroller in a view. But I don't know how to get started. I appreciate any help you would give me. If you could give me some piece of code to get started. Thanks
I don't really understand what you actually want to zoom, just want to give you the idea of Zooming a text for a TextView here the code for it.
ImageView increaseTextBtn = (ImageView)findViewById(R.id.increaseTextBtn);
final TextView txtTitle = (TextView) findViewById(R.id.txt_desc);
increaseTextBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
txtTitle.setTextSize(TypedValue.COMPLEX_UNIT_PX,(txtTitle.getTextSize()+1f));
}
});
ImageView decreaseTextBtn = (ImageView)findViewById(R.id.decreaseTextBtn);
decreaseTextBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
txtTitle.setTextSize(TypedValue.COMPLEX_UNIT_PX,(txtTitle.getTextSize()-1f));
}
});
in this code there are two buttons (increaseTextBtn , decreaseTextBtn ) to adjust the zoom level of TextView
Update
For image view you can find an examle here

android view id is not correct

I create a whole layout setup in XML then attempt to attach listeners to the buttons using findViewById(). The problem I am having now is that the View parameter I receive in the method does not contain the ID of the view I clicked: 830009633920 vs 2131099657.
Button btnNext = (Button)findViewById(R.id.btnNext);
btnNext.setOnClickListener(this);
#Override
public void onClick(View view) {
if (view.getId() == R.id.btnNext) {
...
}
}
How about this:
Button btnNext = (Button)findViewById(R.id.btnNext);
btnNext.setOnClickListener(this);
#Override
public void onClick(View view)
{
switch (view.getId())
{
case R.id.btnNext:
...
break;
case R.id.foo:
...
break;
}
}
That should be working, so my only guess without seeing the rest of the code or layout is that you have set click handlers on two items that overlap.
If you plan on making an Android Library project switch like this will not work due to a recent change in the tool chain.
http://tools.android.com/tips/non-constant-fields
In general I find it is much simpler to use an anonymous class the handle clicks rather than a large single handler.
btnNext.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View view)
{
...
}
});
You might try this approach just to debug that the item you expect to get the click is what you are pressing, to make sure you don't have layout issues.
Try skipping the IDs, and just compare the actually view object. so something like this:
#Override
public void onClick(View view) {
if (view.equals(btnNext)) {
...
}
}

Categories

Resources