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
}
});
Related
I am making an application on Android which generates one of the two available images on clicking a button and after a duration of 1 second, that image is supposed to fade away, giving the user a choice to click the button again.
The problem is that the animation works smoothly on the first button press (i.e. generates an image and then fades away), however on the second button press, the image just sits there and nothing happens. I can't figure out why.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ImageView firstimage = (ImageView)findViewById(R.id.imageView2);
final ImageView secondimage = (ImageView)findViewById(R.id.imageView1);
final Button clickMe = (Button)findViewById(R.id.button);
final TextView image_description = (TextView)findViewById(R.id.textView);
image_description.setText("");
final Animation fadeout = new AlphaAnimation(1,0);
fadeout.setStartOffset(1000);
fadeout.setDuration(1000);
secondimage.setAnimation(fadeout);
firstimage.setAnimation(fadeout);
image_description.setAnimation(fadeout);
secondimage.setVisibility(View.GONE);
firstimage.setVisibility(View.GONE);
image_description.setVisibility(View.GONE);
clickMe.setVisibility(View.VISIBLE);
fadeout.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) { }
#Override
public void onAnimationEnd(Animation animation) {
System.out.println("Animation block");
secondimage.setVisibility(View.GONE);
firstimage.setVisibility(View.GONE);
image_description.setVisibility(View.GONE);
clickMe.setVisibility(View.VISIBLE);
}
#Override
public void onAnimationRepeat(Animation animation) { }
});
clickMe.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
System.out.println("Click block");
Random r = new Random();
int i = r.nextInt(2);
clickMe.setVisibility(View.GONE);
if(i == 0) {
secondimage.setVisibility(View.VISIBLE);
image_description.setText("LOOK it's a CAT");
image_description.setVisibility(View.VISIBLE);
secondimage.setAnimation(fadeout);
image_description.setAnimation(fadeout);
} else {
firstimage.setVisibility(View.VISIBLE);
image_description.setText("LOOK it's a DOG");
image_description.setVisibility(View.VISIBLE);
firstimage.setAnimation(fadeout);
image_description.setAnimation(fadeout);
}
}
});
}
The Logs look something like this:
Click Block
Animation Block
Click Block
Click Block
Click Block
Click Block
Any idea why the code is not reaching the Animation block on 2nd click onwards?
Alright. I was able to solve my own query.
I replaced
secondimage.setAnimation(fadeout);
with
secondimage.startAnimation(fadeout);
On doing so, the code was able to reach the onAnimationEnd function.
The easiest thing to do is create/inflate a new instance of the Animation type object for each view that needs animation. As you have it now, it's trying to reuse the same object.
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;
}
}
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.
this application requires:
first click will change image1 to image2
second click will change back to old image (image2 to image1)
image1 = (ImageView)findViewById(R.id.imageView1);
image1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
image1.setImageResource(R.drawable.a3_01);
image1.setTag(70);
}
});
this image will set a new tag for the server knows that the picture have been change.
*the code i used is only for the first click and it works. ive just have no idea to make a second click event. can anyone gives me idea of it? much appreciate. thanks.
You could use a boolean to act as a switch for you to flop back and forth with an if statement.
boolean showingFirst = true;
image1 = (ImageView)findViewById(R.id.imageView1);
image1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(showingFirst == true){
image1.setImageResource(R.drawable.a3_02);
showingFirst = false;
}else{
image1.setImageResource(R.drawable.a3_01);
image1.setTag(70);
showingFirst = true;
}
}
});
Put both images into an ImageSwitcher and use the Button clicks to call its showNext() method.
You can use if case in it like
public void onClick(View v) {
if (i == 0) {
Toast.makeText(getApplicationContext(), "First Click", 1000).show();
i++;
} else if (i == 1) {
Toast.makeText(getApplicationContext(), "Second Click", 1000).show();
i = 0;
}
}
I have an application where i want to add onclicklistener to my individual items in pager.
Below is my screen shot . please some one tell me how to add onclicklistener to individual image in this current page .
Thanks
below is my code for pager view.
here when 1st time run the app initialize the every view page, when i try to click on image it return only last image info. But i want the current image info.
scroller = ((Pager)findViewById(R.id.scrollView));
indicator = ((PageIndicator)findViewById(R.id.indicator));
indicator.setPager(scroller);
// Pager pager = new Pager(this, attrs)
//indicator.getActivePage();
LayoutInflater layoutInflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View pageView = null;
for (int i = 0; i < NUM_PAGES; i++) {
pageView = layoutInflater.inflate(R.layout.page, null);
// ((TextView) pageView.findViewById(R.id.pageText)).setText("Page " + (i+1));
c=4*i;
imageView1 = (ImageView) pageView.findViewById(R.id.imageView1);
imageView1.setImageResource(icons[c]);
imageView2 = (ImageView) pageView.findViewById(R.id.imageView2);
imageView2.setImageResource(icons[c+1]);
imageView3 = (ImageView) pageView.findViewById(R.id.imageView3);
imageView3.setImageResource(icons[c+2]);
imageView4 = (ImageView) pageView.findViewById(R.id.imageView4);
imageView4.setImageResource(icons[c+3]);
// pageView.setBackgroundColor(COLORS[i % COLORS.length]);
imageView1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
System.out.println("helo i clicked==>"+c);
}
});
imageView2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
System.out.println("helo i clicked==>"+c);
}
});
imageView3.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
System.out.println("helo i clicked==>"+c);
}
});
imageView4.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
System.out.println("helo i clicked==>"+c);
}
});
scroller.addPage(pageView);
}
It seem to be you didn't read information available at android developers.
Use below code to add onClickListener for images. I don't know whether you are using ImageView or ImageButton. I took ImageView.
ImageView iv = new ImageView(/**Context you have to pass**/);
iv.setOnClickListener(new View.OnClickListener) {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
// do whatever you want
}
});
I hope it may help you.
set this on your images..or button
in the xml such that
<Button [...]
android:onClick: "onClick" [...]
/>
"onClick": name this whatever you want your method name that handles clicks to be
This can be done by assigning onClickListeners to Image Buttons. You can use Image buttons to display these images and set onClickListeners on them. Perhaps something like this can help?
ImageButton button = (ImageButton) findViewById(R.id.imageButton1);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// Do your stuff here
}
});
If you use ImageView then set the property OnClick to true.