Show incremented number on click of a button in an EditText. - android

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

Related

Condition On Button click

When the button is clicked it should show me some text. This is working. Whenever the limit on the button click is exceeded, it must show some user defined text. After clicking the button 3 times, it is showing me some text not the user defined one. Here is my code for the OnClickListener:
final Button btnca =(Button) findViewById(R.id.btnca);
btnca.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int c=1;
if(c <= 3) //if button click for first three times
{
new FancyShowCaseView.Builder(Playing.this)
.title(questionPlay.get(index).getCorrectAnswer())
.build()
.show();
score -= 10;
txtScore.setText(String.format("%d", score));
c++;
}
if(c>3) //if button click for after three times
{
new FancyShowCaseView.Builder(Playing.this)
.title("Your Limit Exceed")
.build()
.show();
}}
});
The problem is that c is local to the onClick method so it is starting at 1 for every click. Try moving it out to the class level
final Button btnca =(Button) findViewById(R.id.btnca);
btnca.setOnClickListener(new View.OnClickListener() {
int c=1; //initialize here so it's re-used in each onClick
#Override
public void onClick(View v) {
if(c <= 3) //if button click for first three times
{
new FancyShowCaseView.Builder(Playing.this)
.title(questionPlay.get(index).getCorrectAnswer())
.build()
.show();
score -= 10;
txtScore.setText(String.format("%d", score));
c++;
}
if(c>3) //if button click for after three times
{
new FancyShowCaseView.Builder(Playing.this)
.title("Your Limit Exceed")
.build()
.show();
}}
});
Edit: I should mention this isn't a complete solution. I'll assume this code is in Activity(or Fragment).onCreate(). The counter will reset on configuration change when your lifecycle component is re-created, but I'll leave that solution as an exercise for the reader :)
You should initialize counter variable c outside of onClick() method. Of-course you should initialize it as c = 0 instead of c = 1 to get the toast after 4th click.
Try this:
final Button btnca =(Button) findViewById(R.id.btnca);
btnca.setOnClickListener(new View.OnClickListener() {
int c = 0;
#Override
public void onClick(View v) {
if(c <= 3) //if button click for first three times
{
new FancyShowCaseView.Builder(Playing.this)
.title(questionPlay.get(index).getCorrectAnswer())
.build()
.show();
score -= 10;
txtScore.setText(String.format("%d", score));
c++;
}
if(c>3) //if button click for after three times
{
new FancyShowCaseView.Builder(Playing.this)
.title("Your Limit Exceed")
.build()
.show();
// Reset if required
//c = 0;
}}
});
FYI, if you want to reset variable c, then reset(c = 0) it inside your condition if(c>3).
Hope this will help~

how do u add multiple fixed int from four buttons to an array

edit.
I'm trying to make a game where there is 4 buttons each button has a set value.
buttone is 1 buttontwo is 2 and so on.
and on each click of the button it takes the value from what ever button is pressed and add it to an array, the number of buttons that you have to press is set by the intent from the main activity and the buttons that have to be press is set by the randomNums array.
eg.
if the randomNums give you 1,2,1,1,2.3,3,4.
green button must be pressed 3 times
blue button must be pressed 2 times
yellow button must be pressed once.
buttonOne must be pressed 3 times.
buttonTwo must be pressed 2 times.
buttonThree must be pressed 2 times.
buttonFour must be pressed once times.
I have the randomNums which gives you the up to 12 number from 1-4.
I have the buttons set up so that if the randomNums gives you 1,1,1,2,3,3.
ButtonOne will be green buttonTwo will be yellow and buttonThree will be blue
for 3 seconds then all the buttons turn grey.
but where I'm having trouble is how do u set a button to have a set value.
eg.
if I press buttonOne 3 times it will enter into an array like so 1,1,1.
so the question is how do set a fixed value to a button so that every time it is press it send a fix value to an array.
sry if I was not clear the 1st times around if u have any more question or if you need me to explain it again or want to see the code that I have to set the color of the button play let me know . thanks
hi I'm very new to android studio,
I have 4 buttons and I want each to have a fixed value.
ButtonOne =1;
ButtonTwo =2;
ButtonThree =3;
ButtonFour =4;
and when a button is pressed I want to add that fixed button value to an array.
what I have is sending an ++ number back and not sending a fixed value.
so my question is how do you send a multiple fixed int from four buttons OnClickListener to an array?
public int counterOne = 0;
final Button buttons[] = {
(Button) findViewById(R.id.ButtonOne),
(Button) findViewById(R.id.ButtonTwo),
(Button) findViewById(R.id.ButtonThree),
(Button) findViewById(R.id.ButtonFour)
};
buttons[0].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
counterOne = counterOne+1;
CheckSET.setText(""+counterOne);
}
});
final int pressed[] =new int [12];
end.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String stringlevel = ""+CheckSET.getText();
String b = "";
final int intLevel = Integer.parseInt(stringlevel);
for (int i = 0; i < 12;i++){
//b = ""+ b +""+ pressed[i];
//pressed[i] = pressed[i]+1;
//if(intLevel==1){
// pressed[i] = pressed[i]+1;
//}else if (intLevel ==2){
// pressed[i] = pressed[i]+2;
//}
pressed[0] = counterOne;
b = ""+ b +""+ pressed[i];
diff.setText(b);
}
}
});
the end button is to add all the button clicks to the array.
but if there is a better way then doing an end button to run the button array that would be great. could you do a for() and have all the buttons inside the for and each time they are pressed it adds it to the array?
I have a random array set up all ready that picks 12 numbers from 1-4 and does not let any number repeat more then 3 times.
private int[] pickNums() {
Random rand = new Random();
int randomNums[] = new int[12];
int one = 0;
int two = 0;
int three = 0;
int four = 0;
for (int i = 0; i < 12; i++) {
randomNums[i] = rand.nextInt(4)+1;
if (randomNums[i] == 1) {
one = one + 1;
if (one>3){
i=i-1;
}
} else if (randomNums[i] == 2) {
two = two + 1;
if (two>3){
i=i-1;
}
} else if (randomNums[i] == 3) {
three = three + 1;
if (three>3){
i=i-1;
}
} else if (randomNums[i] == 4) {
four = four + 1;
if (four>3){
i=i-1;
}
}
}
return randomNums;
}
my plan is to do a bubble sort on the randomNums and on the buttonPress array to see if the buttons that are pressed all ==.
but I'm having a lot of trouble with the buttonpress array
I am not sure what you asking but may be this will help you
(Instead of Array you should use list)
private List<Integer> pressedNums = new ArrayList<>();
Button button1,button2,button3,button4;
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
pressedNums.add(1)
}
});
similarly for button2,3,4 add 2,3,4
at last you can use loop
int total = 0;
for(Integer i : pressedNums){
total += i;
}
textField.setText(Integer.toString(total));

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;

Why are my TextViews acting strangely?

I'm making a rather pointless android app that has two buttons, and two textviews.
It has a counter int set to 0.
The fist button named add, adds 1 to the counter, the second button named sub, subtracts one from the counter.
If the counter is greater than 1, the totalPlus textview is shown, with the current counter value.
If the coutner is equal to 0 or less, the totalMinus textview is shown, with the current counter value.
In both cases, the non-relevant textview is hidden.
The problem I'm having is that, say I'm on +5, the top counter will shown, and it will be seen incrementing when I press the button. But if I press the subtract button, the current textview hides, until I get to 0 or less in which the minusTextView displays. It works both ways.
[code]
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
counter = 0;
add = (Button) findViewById(R.id.bAdd);
sub = (Button) findViewById(R.id.bSub);
totalPlus = (TextView) findViewById(R.id.totalPlus);
totalMinus = (TextView) findViewById(R.id.totalMinus);
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
counter++;
if (counter > 1)
totalPlus.setVisibility(View.VISIBLE);
totalMinus.setVisibility(View.INVISIBLE);
totalPlus.setText("Your Total is " + counter);
}
});
sub.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
counter--;
if (counter <0)
totalMinus.setVisibility(View.VISIBLE);
totalPlus.setVisibility(View.INVISIBLE);
totalMinus.setText("Your Total is " + counter);
}
Java ignores indentation levels. You need to use braces around all three statements which you want to be executed if the if statement passes.
if (counter <0) { // <-- this
totalMinus.setVisibility(View.VISIBLE);
totalPlus.setVisibility(View.INVISIBLE);
totalMinus.setText("Your Total is " + counter);
} // <-- and this
If you do not include these braces, Java only considers the first statement to be part of the if construct; the other two statements are outside that construct and will execute always.

using two buttons out of twelve at once

I have 12 buttons in my activity..i want to use them in the following way:
Two buttons should be allowed to click at once and when those two are clicked then some action to be performed..if this action is successful, these two buttons must be "invisible" and if this action is unsuccessful, again there must be option to click any of the two buttons out of all twelve..
i have set the layout of this activity and all the twelve buttons as well.I have also set the onClick method for all of the buttons.
[ADDITION]
i mean only two out of twelve buttons be allowed to press at once..any two of them..and after that the output of both the buttons be compared..if they are equal then the buttons be invisible else they are still there and once again the user gets a chance to click two buttons..
[CODE]
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
RotateAnimation rotate = new RotateAnimation(0,90);
rotate.setFillAfter(true);
button1.startAnimation(rotate);
Random r = new Random();
int next = r.nextInt(5) + 1;
imgV1.setImageResource(images[next]); //imageView1 is given a random image
AlphaAnimation alpha = new AlphaAnimation(0,1);
alpha.setFillAfter(true);
imgV1.startAnimation(alpha);
arg0.clearAnimation();
}});
imgV1.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
AlphaAnimation alpha = new AlphaAnimation(1,0);
alpha.setFillAfter(true);
imgV1.startAnimation(alpha);
RotateAnimation rotate = new RotateAnimation(90,0);
rotate.setFillAfter(true);
button1.startAnimation(rotate);
arg0.clearAnimation();
}});
button click gives a random image..image click gives the button back..now i want that when two buttons are clicked and if they have the same image, then they both go invisible..else they both turn back to the buttons and user can again click on any of the two buttons..
Each button has an imageView behind it in the layout..
K.. Now I got it. So, there will be 6 images in your Drawable. Here we go..
Make an Integer array of size 12 to store id's of 6 images. say, int[] images={R.drawable.img1,...};
Also Button firstClick;Drawable back; to know the first clicked button.
Now, our onClick will be as,
findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(count<2){// means first click
firstClick=(Button)v;
back=v.getBackground();
action=images[0];// button1 pressed so index 0
v.setBackgroundResource(action);
v.setEnabled(false);
count++;
}
else{//second click
count=0;
if(action==images[0]){
v.setBackgroundResource(action);
v.setEnabled(false);
}else{
v.setBackgroundDrawable(back); //roll back to old background
firstClick.setBackgroundDrawable(back);
}
}
}
});
You can use setVisibility() method of view(Button) to set it's visibility on or off.
Button b = ( Button )findViewById( R.id.button1 );
b.setVisibility( b.INVISIBLE );
b.setVisibility( b.VISIBLE );
The logic that I thought is like,
You should have two variables in hand globally.
1 for counting button clicks and 2nd for storing first click action(Based upon your app).
I'm taking int action=0,count=0; as global variables.
findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(count<2){// means first click
action=1;// button1 pressed
count++;
}
else{//second click
count=0;
//Here perform your action and based upon it, set visibility. Previous click is available in 'action'
}
}
});
Repeat this for all button clicks. Thats it. I'll prefer your own method to be called for perform actions and set visibility.

Categories

Resources