Update Button in Android after object value changes? - java

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

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

Beginner - Syntax to concatenate a variable within a findViewById and within a string or textview assignment

I'm new to android programming with very little experience, I'll try ask the correct question but I apologize if some of my understanding isn't quite up to scratch.
I'm building a basic calculator app with buttons 0 to 9. The user can use these to input that specific number into a text field.
I have the same piece of code (below) to do my button action for all 0 to 9 buttons. Just where to see the number 1 it's replaced with that buttons relevant number.
Button button1 = (Button) findViewById(R.id.button_calc_1);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String number_1 = ("1");
TextView Insert_Number_1 = (TextView ) findViewById(R.id.editTextoutput);
TextView Insert_Number_1_view = (TextView) findViewById(R.id.CurrentDisplay);
Insert_Number_1.append(number_1);
Insert_Number_1_view.append(number_1);
}
});
So i'm writing a subroutine of the same code that I can then call upon for the relevant button.
public String numberbuttons(String value){
Button button1 = (Button) findViewById(R.id.button_calc_1);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String number_1 = ("1");
TextView Insert_Number_1 = (TextView ) findViewById(R.id.editTextoutput);
TextView Insert_Number_1_view = (TextView) findViewById(R.id.CurrentDisplay);
Insert_Number_1.append(number_1);
Insert_Number_1_view.append(number_1);
}
});
}
Bear in mind this is a work in progress.
My question is basically how do I add the variable numberbuttons to the button_calc_(insert numberbuttons here)
EDIT:
Okay so from the second block of code. I want to use the numberbuttons variable within the findViewById(R.id.button_calc_1).
So in my head I picture it like this.
findViewById(R.id.button_calc_+(numberbuttons);
The same for when I call a textview. I picture it like this.
TextView Insert_Number_+(numberbuttons) = (TextView ) findViewById(R.id.editTextoutput);
The findViewById() method requires an integer, not a String, so you cannot just pass in a String with the name of the button that you are trying to use. When you use R.id.xxx you are passing a reference to the resource, which is actually just an int.
To accomplish what you want I suggest using a list of some type to store the buttons, and then looping through this. Please see an example below.
ArrayList<Button> buttons = new ArrayList<>();
buttons.add((Button) findViewById(R.id.button1)); //Do this for all the buttons.
for(Button b : buttons){
b.setText("1"); //Do whatever you need to each button in here.
}
If you wanted to use a method to manage all your buttons, you can do this too!
changeButton((Button) findViewById(R.id.button1));
private void changeButton(Button b){
//Change the button
}
EDIT:
To be more relevant to your question if I understand correctly, to add an event handler to each.
ArrayList<Button> buttons = new ArrayList<>();
buttons.add((Button) findViewById(R.id.button1)); //Do this for all the buttons.
for(int i = 0; i < buttons.size(); i++){
Button b = buttons.get(i);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String number = String.valueOf(i);
TextView Insert_Number_1 = (TextView ) findViewById(R.id.editTextoutput);
TextView Insert_Number_1_view = (TextView) findViewById(R.id.CurrentDisplay);
Insert_Number_1.append(number_1);
Insert_Number_1_view.append(number_1);
}
});
}

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.

Reset Button for a 'Click Counter' Android App

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

Categories