Android: How to detect touch events on dynamically created ImageView - android

I have the following design :
<LinearLayout>
<ScrollLayout>
<LinearLayout> .... </LinearLayout> ----> TextView and ImageView dynamically created in this LinearLayout
</ScrollLayout>
</LinearLayout>
I need to detect touch events on the dynamically created ImageView(s). I don't know how many ImageView will be created dynamically. How can I detect which ImageView was touched by the user ?
Thanks for your help.

You'll want to create one [maybe more] View.OnClickListeners in your java code. Then when each ImageView is added, you can assign that OnClickListener to it using View.setOnClickListener. To determine which was clicked, you can set a "Tag" using View.setTag() and then when onClick is called, use View.getTag() to determine which was clicked. The tag should be uniquely identifiable.
Here's some rough code (untested):
private OnClickListener imageViewListener extends View.OnClickListener{
public void onClick(View v){
Intent i = new Intent(v.getContext(), NewActivity.class);
i.putExtra("ImageURI", v.getTag().toString());
startActivity(i);
}
}
Then as you add images, you just set the tag and listener:
for(String uri: someList){
ImageView iv = new ImageView(this);
iv.setTag(uri);
iv.setImageUri(uri);
iv.setOnClickListener(imageViewListener);
}

You should use View.OnTouchLisetener class. So, after initializing your ImageView, add this call to it:
imageView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent e) {
//Your code here
}
});

Related

Add ImageView programatically with own onTouchListener

I have a button that I want to create a ImageView everytime I am pressing it, this ImageView can have his own onTouchListener right ?
I have a button create new and everytime I press it a ball will appear on the screen that I will be able to move it, when I finnish it moving when I press New again another ball will appear on the screen with the same properties and ability to drag and drop.I am asking because I have problem creating a ImageView programatically for example:
save = (Button) findViewById(R.id.savebtn);
save.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
ImageView asds = new ImageView(this);
asds.setBackgroundResource(R.drawable.element_wall);
asds.setOnTouchListener(new OnTouchListener()
{
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
return false;
}
});
}
});
For example, this is not working how it should.Thank you for the time acording reading this.
Firstly use setOnTouchListener outside setOnClickListener.
Secondly assign each imageview a unique tag using setTag() method and using image.getTag() you can get the tag and once you get the image with unique tag you can set OnTouchListener on the particular imageview
Two things.
You need to add the view to one of the ViewGroups on screen. For instance, if you have a FrameLayout containing all your views, you would do something like:
ImageView imageView = new ImageView(this);
FrameLayout frameLayout = (FrameLayout) findViewByid(R.id.container);
frameLayout.addView(imageView);
Since you are creating the ImageView inside of an anonymous inner class, the this keyword refers to the anonymous inner class, not the Activity. You need to qualify the this keyword with your Activity's name, e.g.
ImageView imageView = new ImageView(MyActivity.this);

Set Onclick on many views Android

I have a serie of ImageViews on my Activity. And I wanna execute a method when one of these is touched. I have the next:
public void onClick(View v) {
switch(v.getId()){
case R.id.image1:
mymethod(1,1,movimientos,(ImageView)v);
break;
case R.id.image2:
ponerficha(1,2,movimientos,(ImageView)v);
break;
case R.id.image3:
...
But the method isn't executed, The problem not is the method, because any code in the cases not work. Any idea?
First thing what you need to check if you already register onClickListener for your Images
image.setOnClickListener(this);
(This you have to use if your class implements OnClickListener interface)
Then how you declare and initialize your ImageViews, whether have own ids.
image = (ImageView) findViewById(R.id.someId)
anotherImage = (ImageView) findViewById(R.id.anotherId)
...
You can work with onClickListeners like with anonyme classes like
image.setOnClickListener(
new View.OnClickListener() {
public void onClick(View v) {
// some actions
}
});
but more complex and better in the case you have many widgets to set implement OnClickListener.
public class ClassName extends Activity implements View.OnClickListener {}
First, your activity has to implement View.OnClickListener like so
public class myActivity implements View.OnClickListener {
Then you need to set your on click listener for your ImageViews. If all your ImageViews are in a linearlayout then the code would look like this
LinearLayout llImageViewHolder = (LinearLayout) findViewById(R.id.llImageViewHolder);
for (int i = 0; i < llImageViewHolder.getChildCount(); i++ {
ImageView iv = (ImageView) llImageViewHolder.getChildAt(i);
iv.setOnClickListener(this);
}
Cheers.
You need to add an onClick handler to each view you are interested in processing clicks for.
How you do it depends on how you want to process the click events. You can either use hawaii.five-0's approach and have one event handler for everything, or you can have one event handler per view item which you could add in the onCreate method of your activity:
ImageView imageView = (ImageView) findViewById(R.id.image_view);
imageView.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
// Do something
}
}

Perform Click ImageView Android

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..

How to set onclick function to dynamic textview in android?

I set the onClick() function, but when I click the text it works two times that mean I have two dynamic text view. How to resolve it?
My code:
TextView tView[] = new TextView [Array.length];
for(int i =1; i<Array.length; i++)
{
tview[i] = new TextView(this);
tview[i].setId(i);
tview[i].setText(Array[i]);
tview[i].setOnTouchListener(new OnTouchListener()
{
Public boolean onTouch(View v ,MotionEvent event)
{
Toast.makeText(getApplicationcontext,"MapVal",Toast.LengthShort).show();
}
});
}
Problem is in using OnTouchListener. Event onTouch() is calling not one time on every tap action, but minimum two: on touch down and on touch up. Use OnClickListener and setOnClickListener() instead.
if u want to set an onclick listener then use
urtextvw_name.setOnClicklistener()

Android OnLongClickListener issue

I have a custom view, DisView(Context, bitmap), which I want to add a LongCLickListener to.
The view is displayed once something else is clicked.
public void onClick(View view) {
...
RelativeLayout toplayout = new RelativeLayout(this);
setContentView(toplayout);
Bitmap bmp2 = BitmapFactory.decodeResource(getResources(), R.drawable.tag3);
tag3 = new DisView(this,bmp2);
tag3.setOnLongClickListener(this);
I should add that originally the activity's contentview is set to a linearlayout, but on a button being clicked, setContentLayout() makes a relativelayout the new layout.
Next I did the onLongClick method ( a method of the activity, implementing onlongclicklistener):
#Override
public boolean onLongClick(View view) {
moveTag(view);
return true;
}
moveTag() is a very simple TranslateAnimation. I have no idea why it doesn't work. I have a feeling it may be because I changed the layout.
did you think about ViewSwitcher to change your displayed layout?
Jonathan
if toplayout is a LinearLayout and you write
RelativeLayout toplayout = new RelativeLayout(this);
the software will usually crash on startup(or when this view is activated), if what you meet now is the software crashes once it started(or when you start this activity), it is highly possible that the problem is caused by this statement.
In your example you did this:
tag3.setOnLongClickListener(this);
Why did you do that?
setOnLongClickListener takes a parameter of type OnLongClickLIstener. Something like:
tag3.setOnLongClickListener((OnLongClickListener) keyHdlr);
where keyHdlr looks something like this:
OnLongClickListener keyHdlr = new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
Log.d("long", "backspace phone land long clicked!!!!!!");
return false;
}
};

Categories

Resources