Change image in viewholder image on click - android

The image changes as it should when the button is click,but I'm running into a probably where as new items are added,the old imageview gets reverted while the newer onces that weren't click get changed.
holder.favorite.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
((ImageView) v).setImageResource(R.drawable.ic_action_fave_on_default);
}
});

Related

How to handle touch event on a ImageView

I have three Images. When I first touch, first Image is shown, when I second touch, second Image is shown, and then when I third touch, third Image is shown. After all, when I fourth touch, I want to show first Image return and so on.
Can someone point me to show how handling or touching on a ImageView of android?
The below should do what you need:
public class MainActivity extends Activity {
ImageView image;
int i=1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
image = (ImageView) findViewById(R.id.imageViewName);
setButtonOnClickListeners();
}
}
image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (i==1){
image.setImageResource(R.drawable.image1);
i++;
}
else if(i==2){
image.setImageResource(R.drawable.image2);
i++;
}
else if(i==3){
image.setImageResource(R.drawable.image3);
i++;
}
else if(i==4){
image.setImageResource(R.drawable.image4);
i=1;
}
}
});
I've not tested but the idea is correct. When you click the image it will change the drawable depending on the value of i. When you get image 1 i will equal 1. Onclick will increment i until i==4 which it will reset to 1 as you requested.
A while loop might be tidier but this was the quickest soultion I thought of.
how to handle click on an ImageView
You can simply set a View.OnClickListener for your ImageView:
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// do stuff
}
});

How can I make previous item's button visible when click on current item's button in recyclerview?

What I want is when I click the "x" button of the second item, it will remove the second item and then make the "+" of the first item visible. Here is what I try but not work, the "+" button is not showed.
holder.ivDelete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
View view = recyclerView.getChildAt(position - 1);
holder.ivAdd = (ImageView) view.findViewById(R.id.img_add_items);
holder.ivAdd.setVisibility(View.VISIBLE);
list.remove(position);
notifyDataSetChanged();
}
});
Try this code,
holder.ivDelete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
View view = recyclerView.getChildItemId(position - 1);
holder.ivAdd = (ImageView) view.findViewById(R.id.img_add_items);
holder.ivAdd.setVisibility(View.VISIBLE);
list.remove(position);
notifyDataSetChanged();
}
});
I hope that will help you for solving your problem.

How do you apply multiple click event listeners to images in android

First time Android Studio user, been working on a mini program where 5 images are displayed on the screen. When the user clicks each image the user is redirected to another activity that displays more information. So far I've gotten one image to work my hardin_valley image.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView img = (ImageView) findViewById(R.id.imageViewHardin);
img.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, hardin_valley.class));
}
});
}
The question I have is I also have blonty_county, division_street and magnolia images I want to redirect to new activities when the user clicks. How would I go about creating individual listeners. Or, what would be the best approach, Thank You
The best approach if u have multiple Listeners in your activity is to just add the android:onClick="clickHandler" Tag in your XML to every Image View, then back at your java class you can do this:
public void clickHandler(View v)
{
if(v == R.id.imageViewHardin)
//do something
else if(v == R.id.imageViewBlueCounty)
//do something else
}
Use switch-case at onClick to which view is clicked
public class MainActivity extends Activity implements OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
//-your all image views to link layout-//
ImageView im = (ImageView) findViewById(R.id.image);
im.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
// -- find here id of your all 5 image view
case R.id.image:
break;
default:
break;
}
}

ImageView zooms on first tap

I am using a library to zoom in and out within a ImageView. I am using PhotoAttacher. When I click on the ImageView then first the ImageView loads in a fullscreen (within a dialog), and that is fine. But during loading in fullscreen mode the zoom function already takes place. How can I avoid zooming when I click first on ImageView. Here the code:
image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final Dialog nagDialog = new Dialog(DetailView.this,
android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
nagDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
nagDialog.setCancelable(false);
nagDialog.setContentView(R.layout.preview_image);
Button btnClose = (Button) nagDialog
.findViewById(R.id.btnIvClose);
ivPreview = (ImageView) nagDialog
.findViewById(R.id.iv_preview_image);
// Loading image from url in ImageView
Picasso.with(getApplicationContext()).load(path)
.placeholder(R.drawable.loading).into(ivPreview);
// Here definition of zoom function
PhotoViewAttacher mAttacher = new PhotoViewAttacher(ivPreview);
mAttacher.canZoom();
btnClose.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
nagDialog.dismiss();
}
});
nagDialog.show();
}
});
The weird thing is, when I close the preview and click again on the imageview then it does not zoom in first.
You could implement the PhotoViewAttacher after they image is loaded successfully.
You add a callback to know if the request is successfully done.
Sample code:
Picasso.with(getApplicationContext()).load(path)
.placeholder(R.drawable.loading).into(ivPreview, new Callback(){
#Override
public void onSuccess()
{
Log.d("Test picasso", "image loaded");
PhotoViewAttacher mAttacher = new PhotoViewAttacher(ivPreview);
mAttacher.canZoom();
}
#Override
public void onError()
{
Log.d("Test picasso", "image error");
}
});
Be aware, the Callback param is a strong reference and will prevent your Activity or Fragment from being garbage collected. So you should invoke an adjacent Picasso.cancelRequest(android.widget.ImageView) call to prevent temporary leaking.

OnclickListener for individual elements in a row from a ListActivity using SimpleCursorAdapter to Bind to DB not working properly

Please help.
As I have stated in the title I am trying to make that individual elements of a row of a List adapter launch different actions depending on what the user click.
It "kind of" works but it takes LONG for it to react to user clicks. What is it that I am doing wrong?
Thanks in advance,
So I tried the following code in
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
// Get the item that was clicked
Cursor c = (Cursor) this.getListAdapter().getItem(position);
// c.moveToNext();
prescription_id = c.getString(0);
TextView pName = (TextView) v.findViewById(R.id.text2);
TextView paName = (TextView) v.findViewById(R.id.text3);
TextView rDateLabel = (TextView) v.findViewById(R.id.textView1);
TextView rDate = (TextView) v.findViewById(R.id.text4);
TextView rLeftLabel = (TextView) v.findViewById(R.id.text5);
TextView rLeft = (TextView) v.findViewById(R.id.text6);
ImageView callPhone = (ImageView) v.findViewById(R.id.Call_Pharmacy);
pName.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
goToPDetails();
}
});
pa.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
goToPDetails();
}
});
rDateLabel.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
goToPDetails();
}
});
rDate.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
goToPDetails();
}
});
rLeftLabel.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
goToPDetails();
}
});
rLeft.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
goToPDetails();
}
});
callPhone.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//Some Code
}
});
}
All those onClick listeners (those on single sub-views of one ListView element) probably shouldn't be here in the onListItemClick method, but in the getView method of your Adapter instead (with proper use of the convertView argument).
The way you do it seems quite wrong, maybe your onListItemClick method isn't even needed if you correctly implement the various onClick listeners at the right place.
Using an xml based layout for your list item is key here. Set each individually clickable View with two attributes android:clickable="true" and android:onClick="<your click handler>" the method will need to be implemented with this signature: public void <your click handler> (View v) {...} in your Activity. A side note is that you'll have to make a design decision to implement a click handler to overlap handling (one click hanlder for more than one View) or a single view handler per View, the former is best for when click are substantially similar in function and the latter is when they are different.
The next step is to implement the click handler, the key here is to use ListView.getPositionForView(View v) so you can associate the row, the data, and the View clicked.
Don't forget to implement ListActivity.onListItemClick() as a catch-all for clicking on the root layout of the list item and as a catch-all for Views that don't have their own onClick handler set.
The above technique will have good performance and makes use of several Android API's to speed your development.
If you decide to implement the listeners in code, please study getView() closely (as darma mentioned) and for the sake of performance (if you have several items in your list) reuse the click listeners with the above discussion about how to associate the data and row.

Categories

Resources