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;
Related
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)
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
}
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.
I am using this code to create InfiniteViewPager:
pAdapter = new InfinitePagerAdapter(new MyPagerAdapter());
InfinitivePAdapter = (InfiniteViewPager) findViewById(R.id.pager);
InfinitivePAdapter.setAdapter(pAdapter);
There seems to be something wrong with setCurrentItem() function.
The program freezes when I try call setCurrentItem() function, but not everytime. The program freezes only when:
The setCurrentItem()'s item is lower and it is called from different method(in my case from onClickListener).
InfinitivePAdapter.setCurrentItem(7);//item is set on 7
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
InfinitivePAdapter.setCurrentItem(5);//when button is pressed it is set to 5
}
});
However program seems to be working normal when:
Item is bigger then previous one:
InfinitivePAdapter.setCurrentItem(3);//item is set on 3
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
InfinitivePAdapter.setCurrentItem(12);//when button is pressed it is set to 12
}
});
or the setCurrentItem(3) is called one after another even though the item is smaller than previous one:
InfinitivePAdapter.setCurrentItem(7);
InfinitivePAdapter.setCurrentItem(5);
More precisely program freezes in InfiniteViewPager class on this line:
#Override
public void setCurrentItem(int item) {
// offset the current item to ensure there is space to scroll
item = getOffsetAmount() + (item % getAdapter().getCount());
super.setCurrentItem(item);//<---HERE
}
I got my InfiniteViewPager from https://github.com/antonyt/InfiniteViewPager
You can download full code here:https://drive.google.com/folderview?id=0B3LJrVAKNLR1OG8yeERyQ2ZySU0&usp=sharing
Thanks!
I figured it out. In the InfinitePagerAdapter class I just changed the return value of getCount() method.
#Override
public int getCount() {
return Integer.MAX_VALUE;
}
Integer.MAX_VALUE was too big of a value so scale it down to 3000:
return 3000;//Or something similar
The problem was in the ViewPager class where on line 1029 the for loop would never finished:
for (int pos = mCurItem + 1; pos < N; pos++)
//N is the return Integer.MAX_VALUE and the mCurItem is the item set in setCurrentItem() method
when i click on add button it count numbers from 0 and show it one after one but when i click
delete button it clean edit text but when start to click again in add button it count from the last number it stop on it so i want when i click on it in every single time it beginning to count from( 0 ).
final Button add = (Button) findViewById(R.id.button1);
final Button delet = (Button) findViewById(R.id.button2);
final EditText edit = (EditText) findViewById(R.id.editText1);
add.setOnClickListener(new View.OnClickListener() {
private int number = 1;
public void onClick(View v) {
// TODO Auto-generated method stub
// random = random();
edit.setText("" + number++); // i want when i click on it in every single time it beginning to count from( 0 )
//because when i delete it and start to click again on add button it count from the last number that stop on it
}
});
delet.setOnClickListener(new View.OnClickListener() {
private int numberr;
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
edit.setText("0"); // should i change this clean code to make it count from the beginning in every single click ?
}
});
}
You will have to make the number varialbe global to make the code functional.
It will always be initialized to 1 as soon as you click on buttons.
add.setOnClickListener(new View.OnClickListener() {
// make it global or store in intent to retain its value after incrementing and intialize it globally and not here else again will be same case.
// number = 1;
public void onClick(View v) {
// TODO Auto-generated method stub
// random = random();
edit.setText("" + number++); // i want when i click on it in every single time it beginning to count from( 0 )
//because when i delete it and start to click again on add button it count from the last number that stop on it
}
});