i've made a simple counter program, where i click the plus or minus button and it adds or removes 1 from a counter.
I would like to be able to add more counters: ex. counter1, counter2, counter3 etc.
Here's an example of how i'm doing now with 2 counters, but as you can see i have declared my variables, and i would like to generate them automaticly
int counter, counter2;
ImageButton add, sub, add2, sub2, btnSet;
TextView display, display2;
And here's how i am using the plus button
// Plus button setup
add.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
counter++;
display.setText(""+counter);
Vibrator vibr = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
vibr.vibrate (20);
You could create a class that holds the information related to your increment button:
class IncrementButton {
int step;
int counter;
ImageButton add, sub;
TextView display;
public IncrementButton(int step) {
this.step = step;
add = ...
sub = ...
display = ...
add.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
counter += step;
display.setText("" + counter);
Vibrator vibr = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
vibr.vibrate (20);
}
}
...
}
}
and put objects of that class in a list - for example for 3 buttons:
List<IncrementButton> buttons = new ArrayList<IncrementButton> ();
for (int i = 0; i < 3; i++) {
IncrementButton btn = new IncrementButton(i + 1);
buttons.add(btn);
}
Create your won widget that contains counter, 2 buttons (for add and subscribe) and text view,
put all you logic there (on click listener etc...), and just hold list of how-many-instances-that-you-want in your activity.
Related
I am programming a darts counter app and at the moment I am trying to get an user input (which field on the dart board they hit) by pressing on a specific button.
Each button press will return an int which will be used to update list values that are used by my adapter to then update the views.
The method that should happen in looks like this:
private void startRound(MatchActivityPlayerAdapter adapter) {
for (int playerIndex = 0; playerIndex < getMatchParticipants().size(); playerIndex++) {
for (int currentDart = 1; currentDart <= maximumDartsToThrow; currentDart++) {
// Here I want the activity to "wait" until the user presses a button
if (pointsButtonClicked) {
setDartValue(playerIndex, currentDart);
setDartsCombinedValues(playerIndex, currentDart);
setRemainingPointsValues(playerIndex, currentDart);
adapter.notifyDataSetChanged();
}
}
}
setCurrentRound(getCurrentRound() + 1);
}
Since I cant really stop the activity at the point mentioned above until user has made an input I'll propably have to include a while loop. But in order to do this I think I'll have to create a second thread and handle things differently. That actually overwhelmed me, even though I've been reading through this.
Can anyone please explain to me how I have to design my code in order to achieve what i want?
Edit: Actually pointsButtonClicked is a boolean.
I have over 20 buttons in the global OnClick method and whenever one of them is clicked pointsButtonClicked will be set to true.
Button button1 = findViewById(R.id.btn1);
button1.setOnClickListener(v -> {
outputInt = 1;
pointsButtonClicked = true;
});
Button button2 = findViewById(R.id.btn2);
button2.setOnClickListener(v -> {
outputInt = 2;
pointsButtonClicked = true;
});
// [...]
I think that there should be a better way to this without the while-wait loop.
First of all I suppose that you used an android.widget.Button to implement that pointsButtonClicked
Here's what I would do:
//Global in your activity
int playerIndex = 0;
int currentDart = 1;
Then
private void startRound(MatchActivityPlayerAdapter adapter)
{
pointsButtonClicked.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
setDartValue(playerIndex, currentDart);
setDartsCombinedValues(playerIndex, currentDart);
setRemainingPointsValues(playerIndex, currentDart);
adapter.notifyDataSetChanged();
nextThrow();
}
});
}
And finally
//Also in your activity
private void nextThrow()
{
if (currentDart == maximumDartsToThrow)
{
currentDart = 1;
if (playerIndex == getMatchParticipants().size()-1)
{
playerIndex = 0;
setCurrentRound(getCurrentRound() + 1);
}
else
{
++playerIndex;
}
}
else
{
++currentDart;
}
}
It can be better
You can use private variables and access them with getter and setter
Explained
Using this approach you do not create a thread that waits for every single button pressed. You simply create a button that listen for every click event that it receives and then you apply the logic to cycle the players and the darts for every players.
I hope this could be helpful.
[EDIT]
In this case I would apply the same OnClickListener to every button you use.
First of all I would create an inner class that implements OnClickListener and allows you to manage outputInt variable.
//Also in Activity (it is an inner class)
public class MyOnClickListener implements View.OnClickListener
{
private int myOutputInt;
public MyOnClickListener (int outputInt)
{
this.myOutputInt = outputInt;
}
#Override
public void onClick(View v)
{
pointsButtonClicked = true;
outputInt = myOutputInt;
setDartValue(playerIndex, currentDart);
setDartsCombinedValues(playerIndex, currentDart);
setRemainingPointsValues(playerIndex, currentDart);
nextThrow();
}
}
And then you create 20 MyOnClickListener passing only the int you want to assign. For example:
button1.setOnClickListener(new MyOnClickListener(1));
button2.setOnClickListener(new MyOnClickListener(2));
button3.setOnClickListener(new MyOnClickListener(3));
etc etc...
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();
}
});
}
I have 10 buttons (like calculator does) displaying digits from 0 to 9.
The problem is that I want to display unique random numbers on each button every time when I press a button. How can I achieve this behavior?
My code is:
public void generate(View view) {
Random rand=new Random();
int number=rand.nextInt(10);
but2=(Button)findViewById(R.id.button5);
but3=(Button)findViewById(R.id.button6);
but4=(Button)findViewById(R.id.button7);
but5=(Button)findViewById(R.id.button8);
but6=(Button)findViewById(R.id.button9);
but7=(Button)findViewById(R.id.button10);
but8=(Button)findViewById(R.id.button11);
but9=(Button)findViewById(R.id.button12);
but0=(Button)findViewById(R.id.button13);
but1=(Button)findViewById(R.id.button);
String mynumber=String.valueOf(number);
but2.setText(a);
but3.setText(mynumber);
but4.setText(mynumber);
but5.setText(mynumber);
but6.setText(mynumber);
but7.setText(mynumber);
but8.setText(mynumber);
but9.setText(mynumber);
but0.setText(mynumber);
but1.setText(mynumber);
but2.setText(mynumber);
Yes, it is possible.
How:
find your button. Event: onCreate
add onClickListener event to your button.
Example:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.button_component = (Button) findViewById(R.id.button_component);
this.button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// RANDOM YOUR NUMBER.
}
});
}
check complete answer
int randomWithRange(int min, int max){
int range = (max - min) + 1;
return (int)(Math.random() * range) + min;
}
I'm trying to press the first button just one time. This button usually return a random card but I want it just one time. Then the button should be inactive. How can I do that?
That's my code:
public class GameActivity extends Activity {
Boolean buttonPressed = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
final Button background = (Button) findViewById(R.id.A);
Resources res = getResources();
final TypedArray Deck = res.obtainTypedArray(R.array.Deck);
final Random random = new Random();
if (!buttonPressed){
buttonPressed = true;
background.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
//Genrate a random index in the range
int randomInt = random.nextInt(Deck.length()-1);
// Generate the drawableID from the randomInt
int drawableID = Deck.getResourceId(randomInt, -1);
background.setBackgroundResource(drawableID);
}
});
you can disable your button with (place it inside your onClick)
background.setEnabled(false);
and you also might initialize your button to true in onCreate method
If the player has an action to do next, the simpler way is to deactivate the button when is pressed first time and after in the next action activated again.
You can do this adding
background.setEnabled(false);
in your anonymous class listener and after add
background.setEnabled(true);
in player's next action.
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