Reset Button for a 'Click Counter' Android App - java

I am creating a small 'Click Counter' app, which basically has a button function, which when pressed, a textview field displays the number of clicks made.
I am trying to setup a 'Reset' button, which changes the value of the textview back to 0. This is the code I have so far:
wreset = (Button)findViewById(R.id.wreset);
wreset.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
txtCount.setText(String.valueOf(0));
}
This changes the textview value to 0, however when the button is clicked again it starts back from the count which it was on when the reset button was clicked.
For example:
a. the current count = 10
b. reset button is selected
c. current count = 0
d. clicker button pressed
e. current count = 11
Am I using the wrong statement or intent?

Your not resetting whatever counter you are using:
#Override
public void onClick(View arg0) {
yourCounter = 0;
txtCount.setText("0");
}

You need to save the counter state in an instance field or a static field. Then set that field to zero on reset. For example:
private int counter = 0;
// ...
wreset = (Button) findViewById(R.id.wreset);
wreset.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View arg0)
{
counter = 0;
txtCount.setText(String.valueOf(counter));
}
}
increment = (Button) findViewById(R.id.increment);
increment.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View arg0)
{
counter++;
txtCount.setText(String.valueOf(counter));
}
}

public void onClick(View view) {
int count = 0;
count++;//increment the count
TextView text = (TextView) findViewById(R.id.counttxt);// resorce location
text.setText("No.of Clicks " + count);// view in the text

Related

Button OnClick Listener

I have a button with the "btnadd" id ,
Button add = (Button) findViewById(R.id.btnadd);
And a function with two inputs ,
public void CheckNumber (int i , int j) { if (i != j) Toast.makeText(getBaseContext,"i is not equal to j"); }
And I want to set this function for the click event of this button
add.setOnClickListener(CheckNumber(2,4));
This code is not correct, but how can I do this?
You need to define a new View.OnClickListener inside of setOnClickListener.
Try this:
add.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
checkNumber(2, 4);
}
});
Complete Solution:
Try something like this:
First, Create a method: (Consider your case)
public void CheckNumber (int i , int j) {
if (i != j)
Toast.makeText(this, "i is not equal to j", Toast.LENGTH_SHORT);
}
Second, declare the button inside onCreate():
Button add = findViewById(R.id.btnadd);
Then finally, add a click listener:
add.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
CheckNumber(2, 4);
}
});
That's it. Hope it helps.

changing text on textview with button's onClickListener

Hi I'm working at my first bigger app in android studio "FlashCards". I would like it to work so after you click on the button the flashcard's textview changes its text to next random flashcard untill you see all of the them how can i do something like 'continue' to my loop from inside onClick method.
here's the loop's code:
while(i < mTestDeck.size()) {
// generates random number which will represent position in deck.
int random = randomGenerator.nextInt() % mTestDeck.size();
// if random flashcard was already shown create random number again
if (mTestDeck.get(random).wasShown())
continue;
//text view that we will operate on
TextView deckTextView = (TextView) findViewById(R.id.flashcard_text_view);
// set text
deckTextView.setText(mTestDeck.get(random).getFront());
// set mWasShown to true
mTestDeck.get(random).flashcardShown();
Button myButton = (Button) findViewById(R.id.know_answer);
myButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mTestDeck.correctAnswer();
}
});
myButton = (Button) findViewById(R.id.dont_know_answer);
myButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
}
}
First, this way, you have a potential infinite loop. And if it can happens, it will happens! It's not a good idea to "get random item and check if it's ok or try again".
I think that it's better to keep a list with all items in a random order. You just have to iterate over it.
Something like:
int currentPosition = 0;
List<Card> items = new ArrayList<Card>(mTestDeck).shuffle();
// Call this method once in onCreate or anywhere you initialize the UI
private void function setCurrentCard() {
Card currentItem = items.get(currentPosition);
[...] // Set all UI views here
myButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (currentPosition > items.size) {
// TODO? End?
return;
}
currentPosition++;
setCurrentCard();
}
});
}

Update Button in Android after object value changes?

I have an array that stores objects of type Cup. The button displays the value of numberOfPebbles in the cup. Clicking the button causes all the buttons to increase by 1 - this i have logged in logcat and the array updates but I can't get the button to update with the new array value when using the emulator. Here is my code:Bare in mind they are in separate classes
button1 = (Button)findViewById(R.id.button1);
button1.setText(move.cups.get(0).toString());
button1.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
move.makeMove(move.cups.get(0));
}
});
public List<Cup> cups;
public List<Integer> cupValues;
public void newGame(){
cups = new ArrayList<>();
cupValues = new ArrayList<>();
//Reset all cups and values
for(int i=0; i<16; i++){
Cup cup = new Cup(7);
cups.add(i, cup);
Log.d("Move.java", "Printing array valuess:" + cups.get(i));
}
}
You need to update the button's text each time a click occurs
button1 = (Button) findViewById(R.id.button1);
button1.setText(move.cups.get(0).toString());
button1.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
move.makeMove(move.cups.get(0));
button1.setText(move.cups.get(0).toString()); //or whatever you want to display in the button after each click
}
});

Event get String from 2 buttons

I am using an Adapter to generate buttons in a GridView.
Using an OnClickListener, whenever I click a button from the GridView, it is possible to get the String of the button in a variable. However, I want to click a button, store its text in a variable, then click a different button, store its text in another variable so later I can compare the two texts. How could I do that?
gI'm using OnClickListener, therefore my previous implementation for one button was the following:
public void onClick(View v) {
Button b = (Button)v;
String text = b.getText.toString();
With this approach, I can only get and store text from first button clicked, unless I store all the values in an array.
Ok so from what I can understand, you want to compare the strings of the two items in a grid view. If this is what you want, heres how I would do it(just a psuedocode):
String mValue1, mValue2;
boolean isOneClicked = false;
onItemClickedListener(item, position){
if(!isOneClicked){
mValue1 = item.stringValue;
isOneClicked = true;
}
else{
mValue2 = item.stringValue;
//do whatever you want here
//Reset when done
mValue1 = "";
mValue2 = "";
isOneClicked = false;
}
}
Please you can share your code to turn the things more comprehensible? Edit your question please... Anyways:
MainActivity.java
public class MainActivity extends Activity {
private Button button1;
private Button button2;
private String butString, butString2;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
butString = (String) button1.getText();
}
});
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
butString2 = (String) button1.getText();
}
});
}
}
After that you can compare the two strings.

Getting the State of a Toggle Button

I may be going about this all wrong, but I have a view that contains a few ToggleButtons and a Save button. When the Save button is pressed, I want to collect the states of the various toggles as boolean values. I have tried the following in the onClickHandler for the Save button:
ToggleButton tb = (ToggleButton) findViewById(R.id.button1);
boolean pol = tb.isChecked();
and I would expect pol to be set to the state of button1, but it keeps being set to true. Of course I have tried this with the button in both states.
Thanks
you can try this
ToggleButton tb = (ToggleButton) findViewById(R.id.button1);
boolean pol = false;
tb.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(tb.isChecked()) {
pol = true;
} else {
pol = false;
}
}
});
}

Categories