Edittext with multiple images and clicklistener - android

I have a scenario in which I have to to add text and multiple images, something like below
"fooo bar lorem ipsum somtext.
and all these images have clicklisteners so I can know which image is being clicked.
Till now I am able to place images but unable to implement click listeners on that

Associate a Tag with each imageview and
Set a common onclick Listner for both imageview in onclick even find the imageview by tag associated with View in onClick() method Like
Let two imageview are iv1 and iv2
iv1.setTag("TAG1");
iv2.setTag("TAG2');
OnClickListener oc=new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
String tag=(String) v.getTag();
if(tag.equals("TAG1"))
{
// Imageview is iv1
}else{
// Imageview is iv2
}
}
};
Try it and let me know if problem exist

Related

android change image onclick imageviev

How can I change Image of an Imageview?
I want to get the image associated; if it is img1 I want set the image to img2, if it is img2 I want to set the image to img2.
first set the tag of imageview in xml to 1
final ImageView imageview = (ImageView) findViewById(R.id.imageView1);
imageview.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (Integer.parseInt(imageview.getTag().toString()) == 1) {
imageview.setBackgroundResource(R.drawable.image2);
imageview.setTag(2);
} else {
imageview.setBackgroundResource(R.drawable.image1);
imageview.setTag(1);
}
}
});
I think you are looking for ImageView.setTag() and ImageView.getTag().It checks if the image is associated with the Image view as #ρяσѕρєя K mentioned in comment.
There are two version of setTag one which takes object as an argument and other takes key and object as an argument

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

How to load image from one xml file into another xml file?

I'm creating an camera app here. I've managed to click an image and display in an ImageView. I have a ImageButton on whose click takes you to another page. On the other page I want to display that image with a set of scrollable images like on Instagram. But I cant load that image on the other xml file.
Help Please.
Code:
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.image_select);
// Image clicked...
ImageView ivPic = (ImageView) findViewById (R.id.ivPic);
// Image button to go to next page...
ImageButton ib = (ImageButton) findViewById(R.id.ibNext);
// Not sure if this is right.....
Drawable iv = ivPic.getDrawable();
ib.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent("com.rating.bar.EFFECTS"));
}
});
}}
In the other class I created this classes variable but I cant use iv(which holds the drawable of the image view). It only accepts integers I think. What should I do?
Read the URI/Path of the image in your imagebutton and add to the extras of the intent calling the next activity and read the extras in the next activity.

Is there a way to get image name in place of image position in setOnClickListener onClick method under grid view?

I am using grid view example. What i want to know is that is there a way to get the image name when a user clicks on a particular image? Right now we are able to get the position of the image clicked.
Please help me on this.
First you can used custom gridview like listview..
imageviewname.setTag("yourimagename");
imageviewname.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
String imgname = v.getTag();
// toast message
}
});

Replace the previous selected image with original image if one clicks on a new image

I am using grid view with ImageAdapter to display images.
I have two set of images that is mThumbIds containing original images and cThumbIds containing the selected images.
Right now when i click on an image i change the normal image with the selected image. The code is as below:
final ImageView iv = (ImageView)v.findViewById(R.id.icon_image);
iv.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
//iv.setColorFilter(Color.LTGRAY);
iv.setImageResource(cThumbIds[position]);
//iv.bringToFront();
index= position;
}
});
iv.setImageResource(mThumbIds[position]);
But the problem arises when i click on the another image the other image also shows as selected. The correct way would be to show the new image as selected and remove the older one as selected .In other words the older one should revert back to original one.
Please help me on this
Thanks,
Pankaj
You need to create a variable and keep the clicked image's id in that. When the user clicks some other image, first reset the other image as per the id in the variable and then replace the variable value with the id of the currently clicked image.
I'm assuming you are using a modified copy of the ImageAdapter class in this tutorial and that the code you have posted is in the getView(int,View,ViewGroup) method of that class.
You save the index of the image that is selected but you don't save the view itself. You need to save both in order to revert the old image, something like this:
private int selectedPosition = -1;
private ImageView selectedView = null;
...
public View getView(int position, View convertView, ViewGroup parent) {
// I don't understand what this line is about??
ImageView iv = (ImageView) v.findViewById(R.id.icon_image);
// Why not something like this??
// ImageView iv = new ImageView(mContext);
iv.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
// Set the selected image for the ImageView just pressed.
iv.setImageResource(cThumbIds[position]);
// Revert to the original image for the ImageView previously
// pressed.
if (selectedPosition != -1) {
selectedView.setImageResource(mThumbIds[selectedPosition]);
}
// Save the position and ImageView just pressed so it can be
// reverted next time an ImageView is pressed
selectedPosition = position;
selectedView = (ImageView) view;
}
});
iv.setImageResource(mThumbIds[position]);
return (iv);
}
I'm a bit confused about the line ImageView iv = (ImageView) v.findViewById(R.id.icon_image); though (as I mention in my code example).

Categories

Resources