How to set more than 2 images on ImageView in Android - android

I'm working on assignment, and have a problem. I need to have 2 clicks on ImageView control,meaning I have 3 photos total. When I set imageview control to show one photo, and on click, it switches to second photo, it all works good. Problem is that I can't switch to 3rd photo. Anyone can help? How can I add one more "click" to switch from 2nd to 3rd photo?

You can use a counter, in each click update the imageView based on the counter value ( if 0 it's first image, else if it's 2 second image and the same for the third image), and increment the counter by 1 on each click and make sure value of counter doesn't exceed 3.
int counter = 0;
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
updateImageView(counter);
counter++;
counter %= 3;
}
});
void updateImageView(int counter) {
if(counter == 0)
set first Image
else if(counter == 1)
set second Image
else
set third Image
}

Related

How to change an Gif ImageView multiple times using the same button Android Studio

I'm trying to make something that's probably easy to do but I couldn't find any ways to do it. Basically, when I click on a button I want it to change one Imageview with the second Imageview, but then again, when I click on the same button, I want that second Imageview to be changed to the third Imageview, is that possible? So far, I've only done the first part, I can only change one Imageview with another, but can't do it to the third one. Here's my code
changeButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
GifImageView gifImageView = findViewById(R.id.greengif);
gifImageView.setImageResource(R.drawable.yellow);
}
});
You need to count the click event.
int count = 0;
changeButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
GifImageView gifImageView = findViewById(R.id.greengif);
if (count == 0) {
// show the second image
gifImageView.setImageResource(R.drawable.yellow);
} else if (count == 1) {
// show the third image
gifImageView.setImageResource(R.drawable.yellow);
}
count++;
}
}

Changing the background image of Text View whenever the button is pressed

Whenever the user presses the button, the text of the TextView, as well as the background image of TextView, is changed. I have created an int[] array to store the id of drawable to use in TextView.setBackgroundResource(array[index]) . But on incrementing the index the background is not changed. I even tried hardcoded index for array[] but it still sets first element image.
//j and drawable array are global variable.
int j=1;
int[] drawablearray=new int[]{R.drawable.girl,R.drawable.beach,R.drawable.flower};
nextButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(j<drawablearray.length-1){
j++;
quoteTextView.setBackgroundResource(drawablearray[j]);
quoteTextView.getBackground().setAlpha(150);
}else{
j=0;
quoteTextView.setBackgroundResource(drawablearray[j]);
quoteTextView.getBackground().setAlpha(150);
}
});
It seems everthing OK in your code.
Did you initialised nextButton properly?
Probably your click is not working.
UPDATE
I have updated your code:
nextButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(j<drawablearray.length){
quoteTextView.setBackgroundResource(drawablearray[j]);
quoteTextView.getBackground().setAlpha(150);
j++;
}else{
quoteTextView.setBackgroundResource(R.drawable.girl);
quoteTextView.getBackground().setAlpha(150);
}
}
});
For the first 3 clicks it will show three different images then sets to default image
Your j variable must be 'global'. It means that it must a member of your Activity or Fragment but not a variable inside your method
Instead of the "if" condition I suggest you to use:
j = (j+1) % drawablearray.length;
Then the variable j must be a field of your java class (declare it outside every method)

How to reset a click counter?

I have an implementation where, on every click of a button, a counter is increased and the counter is shown in a TextView. There's a reset button that is supposed to set the counter back to 0, so that the count can start again from zero when the btn_take_photo button is pressed. Here's my code:
private int counter = 0;
btn_take_photo.setOnClickListener(new FloatingActionButton.OnClickListener() {
#Override
public void onClick(View view) {
counter++;
count.setText(String.valueOf(counter));
}
});
This is the method called by the button (btn_approve) that is supposed to reset the counter:
public void btn_aprove (View view)
{ count.setText("0");
}
When I click btn_approve, the TextView shows 0. But, when I press btn_take_photo again, the counter starts from the last set value instead of 1. For instance, if I reset the counter when count is 6, the TextView reads 0. Then if I press btn_take_photo, the counter shows 6 again.
You also have to set the variable counter back to 0.
So your "Reset"-function should look like this:
public void btn_aprove (View view)
{
count.setText("0");
counter = 0;
}
You must also reset counter = 0;

changing images on button click

I have 3 images in my app. The images are numbers. The maximum number displayed on all the 3 images will be 9. At first the image in the unit's place will be 1 and the rest images will be 0. On Clicking on the "next" button, the next number is being displayed i.e. 2. Proceeding in this way till the last number 9,then on the next click the unit's place becomes 0 and the ten's place becomes 1 and so on will proceed till 999. There is a previous button too which will reverse the condition of next button and will decrement the numbers by one digit. I am able to achieve the next button condition by using if else conditions but unable to do the same for previous button. please help.
int image1 = 2;
int image10 = 0;
int image100 = 0;
int max = 10;
final int [] images = {
R.drawable.num00,R.drawable.num_0, R.drawable.num1, R.drawable.num2, R.drawable.num3, R.drawable.num4,
R.drawable.num5, R.drawable.num6, R.drawable.num7, R.drawable.num8, R.drawable.num9
};
ImageButton next = (ImageButton)findViewById(R.id.imageButton1);
next.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if((image1==max)&&(image10<max)) //for value with 9, 19... to increment
{
image1=1;
if (image10==0)
{
image10=2;
}
else
{
image10++;
}
img1.setImageResource(images[image1]);
img2.setImageResource(images[image10]);
}
else if((image1==max)&&(image10==max)&&(image100>=0)&&(image100<=10)) //for value with 9 in 1st block to increment
{
image1=1;
image10=1;
if (image100==0)
{
image100=2;
}
else
{
image100++;
}
img1.setImageResource(images[image1]);
img2.setImageResource(images[image10]);
img3.setImageResource(images[image100]);
}
else if (image100<=10) //for value below 9 in 1st block to increment
{
image1++;
img1.setImageResource(images[image1]);
}
}
});
Here, image1 is the position of image in the unit's place, image10 in the ten's place and image100 in the hundred's place.
The implementation can be optimized a lot.
Why cant you go for a simple number like below.
int number = 0;
for next button:
if(number <1000){
number++
updateImages()
}
for previous button:
if(number > 0){
number--
updateImages()
}
After doing the above line for those prev/next buttons, simply write a method like below.
updateImages(){
unitsPlace = number%10;
tensPlace = ((number/10)%10);
hundredsPlace = ((number/100)%10);
img1.setImageResource(images[hundredsPlace]);
img2.setImageResource(images[tensPlace]);
img3.setImageResource(images[unitsPlace]);
}
Thats it.

How to check if a button is clicked or not?

I have a Activity in android that has 4 buttons.
The first 3 buttons fetches a json data from a weather API for 1 day, next 5 days and next 10 days respectively.
I have a 4th button placed at the bottom of the screen, which takes user to second activity.
I want to restrict the entry of user to second Activity if no button from top 3 is clicked.
If the data is fetched, I mean any one of the top 3 buttons have been clicked, allow him to go to second activity on 4th button click else show a message.
How can i check on click of 4th button if any of the top 3 buttons have been clicked before?
Thanks
Put a boolean field in your activity, name it clicked and set it to false on the onCreate method of your first activity, then in the onClick method of your 3 buttons, set it to true,
and in the onClick method of your 4th button check it, if it's true go startActivity, else launch a Toast
You can make the 4th button look disable in "OnCreate" with the function "setEnabled"(may be wrong),
and then just set "setOnClickListener" for the 4th button when you click any of the others.
ps.
Can provide code example if needed.
Why don't you use if statement? You can keep the clicked count data under the first three buttons. Like this;
import java.util.stream.*;
int[] btnMemory = new int[4];
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
btnMemory[0] = 1;
// your code
}
});
after, you can check it with if statement under 4th button;
button4.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
int sum = IntStream.of(btnMemory).sum();
if(sum >= 3)
// your code
}
});

Categories

Resources