How to setText on last button click in a series - android

I have a simple counter app which displays the button count in a TextView. This is also sent in an ArrayList of an ArrayAdapter to log the time the button was pressed. At the moment, every time the button is pressed it logs it. Although, i want it to only log the last time it is pressed in a series of pressed.
for example: if the button is pressed 3 times in the space of 3 second i want the log to just show 1 line displaying '3' instead of 3 lines displaying '1'. Then if it is pressed another 5 times in 3 seconds it shows '3' on one line and '5' on the next. At the moment it would display 8 lines all with '1' on it.
ill need some sort of onLastClickListener or something along those lines but i can't work it out...

There's no on"last"clickListener... You have to teach an onClickListener how to understand what a last click is. Example to get you started:
View.OnClickListener listener = new View.OnClickListener() {
long lastClickTime = 0;
int countClicks = 0;
#Override
public void onClick(View v) {
long newClickTime = System.currentTimeMillis();
if((newClickTime - lastClickTime) <= 3000){
countClicks++;
}else{
lastClickTime = newClickTime;
countClicks = 1;
}
//Do Something with countClicks;
}
};

Related

How to avoid unintended touch-inputs in an android app?

for research purposes I am developing a native Android app (with Java), which allows geriatric patients after an ambulatory rehab to record a nutrition diary.
Due to the high age of the target group it has to be taken into account that the users have a tremor in their hands.
My approach to avoid "unintended" inputs:
Is there some kind of global setting that defines a "minimum time" between two touch inputs? If this time is underrun, the app only executes the first input and ignores further inputs within this timeframe.
Of course I am open for other approaches and ideas. Maybe Android itself provides input assistance for people with tremor? So far I could not find anything that helps me with this topic.
To give you an idea of the situation:
The user clicks a button. This causes the UI to change and a new button to appear in the exact same place where the user clicked. This button should not be directly "clickable". But of course, buttons at other locations should not react directly either.
Remember when the last Button was clicked, then compare that time to the time the next Button was clicked:
final long noClickWithinThisAmountOfMS = 2000; //2000ms = 2 seconds
long oldTime = 0;
long newTime = 0;
Button buttonOne = (Button) view.findViewById(R.id.button_one);
Button buttonTwo = (Button) view.findViewById(R.id.button_two);
buttonOne.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
newTime = System.currentTimeMillis();
if((newTime - oldTime) > noClickWithinThisAmountOfMS) {
//Don't ignore the click
oldTime = newTime;
//TODO: Add actual "onClick" code here
} //else: Ignore the click
}
});
buttonTwo.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
newTime = System.currentTimeMillis();
if((newTime - oldTime) > noClickWithinThisAmountOfMS) {
oldTime = newTime;
//TODO: Add actual "onClick" code here
}
}
});
Do this for every Button that has to be affected.
You can even create "clusters" of buttons this way by using multiple oldTime and newTime if e.g. ButtonOne and ButtonTwo are next to each other but should act independently of ButtonThree and ButtonFour.
Why don't you put all the buttons in an array and give them all the same onClickListener interface
which when onClick a for loop iterates over this array and disable each else button something like this
ArrayList<Button> buttons = new ArrayList<>;
buttons.add(button1);
buttons.add(button2);
buttons.add(button3);
for(Button button : buttons){
button.addOnClickListener(new View.onClickListener(){
for(Button button1 : buttons){
button1.setEnabled(false);
}
});

100 Buttons and only 1 Active

I have one activity and here i have 100 buttons, i want that when i press Button 1 then press another Button the Button 1 should get unpressed.
i know i can make this with
if(Button1.isPressed()) {
Button2.setPressed(false);
Button3.setPressed(false);
Button4.setPressed(false);
Button5.setPressed(false);
Button6.setPressed(false);
Button7.setPressed(false);
Button8.setPressed(false);
......................... }
else { do nothing }
.... BUT!
it's too much code
Coders will kill me or will just laugh on me.
any ideas?
maybe there is a way to unpress the all buttons from the activity?
Not the prettiest solution ever, but you could make an OnClickListener like this:
View.OnClickListener listener = new View.OnClickListener() {
public void onClick(View v) {
ViewGroup parent = (ViewGroup) v.getParent();
for (int i = 0; i < parent.getChildCount(); i++) {
View current = parent.getChildAt(i);
if (current != v && current instanceof Button) {
((Button) current).setPressed(false);
}
}
((Button) v).setPressed(true);
}
}
and attach it to all of your buttons.
Then, whenever a button is clicked, it will iterate over all views that are in the same layout (or actually, view group) as the clicked button, and, for any of those views that are buttons except for the clicked button, it will call setPressed(false).
Note that this only works out of the box if all the buttons are in the same layout. If they are in nested layouts, you will have to adapt it a little.
Off topic: What do you need 100 buttons for? That's a lot of buttons. You may want to redesign your user interface
Ok so instead of looping through all the buttons on over and over again when one button is pressed, you can just store a variable which stores the button number of the button that was last pressed. Now, when the second button is pressed, disable the button that was pressed earlier, you get its index from the saved variable, enable the button that was pressed and store its index in the variable.
Heres an example pseudo code to give you and idea:
int buttonLastPressed = 0;
void onButtonClick(Button buttonPressed){
if(buttonLastPressed != 0){
disableButton(buttonLastPressed);
enableButton(buttonPressed);
buttonLastPressed = buttonPressed.getIndex()
}
}
Saves you from looping through each and every button to disable it.
Define id of button 1 to 100
When press button occurs save it's id in some member variable like previous_pressed
Before updating a previous_pressed value find and unpress previous pressed button like this
Button previous_pressed_button = (Button) findViewById(previous_pressed);
Now you have the previous pressed button, So upress it or whatever.

swithcing to next textview

Ok im making app and it have 15 button's and 6 textview's.I want when I press first button to change value of first textview(value is "") to something (number one for example).But problem is when i press second button if first textview is already set to some value to set set second textview to second value.
If you need something else ask in comments (sorry for bad English)
here is what I was doing(this is under onclick)when i press second button
if(textview1.equals("1")){
textview2.setText("2");}
else if (textview1.equals("")){
textview1.setText("2");
}
It sounds like you wish to show last 6 buttons pressed.
Store all pressed buttons in a List (i.e. LinkedList) of size 6. Initially, it will be empty.
Then whenever any button is pressed do two things:
1) add it to the List and delete old elements if size exceeds six.
2) set button values from List.
Second step can be achieved like this:
// all TextViews should be in a list
private List<TextView> textViews;
// declare as field
private List<String> memoryQueue = new ArrayList<String>();
public void update() {
//set fields for the pressed buttons
for (int i=0; i<6 && i<memoryQueue.size(); i++) {
String text = memoryQueue.get(i);
textViews.get(i).setText(text);
}
// set empty fields
for (int i = memoryQueue.size(); i<6; i++) {
textViews.get(i).setText("");
}
}
This code snippet assumes that you store your TextViews in a List.
And Easiest way to keep track of last six button:
public void buttonPressed(Button button) {
//get text based on your button
String text = button.getText();
if (memoryQueue.contains(text)) {
return;
}
memoryQueue.add(text);
if (memoryQueue.size() > 6) {
memoryQueue.remove(0);
}
}
Since you're concerned with the text inside of your text view, you should be using the object's getText method:
if( textview1.getText().equals("1") ){ // Edited
textview2.setText("2");
} else if (textview1.getText().equals("")){ //Edited
textview1.setText("2");
}
At first, you have to get the String text from TextView using getText() method then you can compare that String with another String. Now, change your condition as follows...
if(textview1.getText().toString().equals("1")){
textview2.setText("2");}
else if (textview1.getText().toString().equals("")){
textview1.setText("2");
}

How to check if a button is clicked or not?

I have a Activity in android that has 4 buttons.
The first 3 buttons fetches a json data from a weather API for 1 day, next 5 days and next 10 days respectively.
I have a 4th button placed at the bottom of the screen, which takes user to second activity.
I want to restrict the entry of user to second Activity if no button from top 3 is clicked.
If the data is fetched, I mean any one of the top 3 buttons have been clicked, allow him to go to second activity on 4th button click else show a message.
How can i check on click of 4th button if any of the top 3 buttons have been clicked before?
Thanks
Put a boolean field in your activity, name it clicked and set it to false on the onCreate method of your first activity, then in the onClick method of your 3 buttons, set it to true,
and in the onClick method of your 4th button check it, if it's true go startActivity, else launch a Toast
You can make the 4th button look disable in "OnCreate" with the function "setEnabled"(may be wrong),
and then just set "setOnClickListener" for the 4th button when you click any of the others.
ps.
Can provide code example if needed.
Why don't you use if statement? You can keep the clicked count data under the first three buttons. Like this;
import java.util.stream.*;
int[] btnMemory = new int[4];
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
btnMemory[0] = 1;
// your code
}
});
after, you can check it with if statement under 4th button;
button4.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
int sum = IntStream.of(btnMemory).sum();
if(sum >= 3)
// your code
}
});

Android multiple button click

Android multiple button click
I have a button and I want on the button's first click i display a thing, and on it's second i display another etc ..
I have a button and want it to have 11 clicks .. on the first click Num.settext("First");
on second click Num.settext("Second");
etc .. until the tenth click .. then on the 11th click its's Num.settext("0"); and it resets from the begning ..
like ..
1,2,3,4,5,6,7,8,9,10,11(0) 1,2,3,4,5,6,7,8,9,10,11(0)
Wouldn't be easier if you store the click number in a variable?
For instance:
//...
int clickNumber = 0;
//...
public void onClick() {
if(clickNumber > 10) {//reset variable
clickNumber = 0;
}
if(clickNumber == 0) {
Num.setText("First");
clickNumber++;
}
else if(clickNumber == 1) {
Num.setText("Second");
clickNumber++;
}
//...
}
//...
Maintain a field called cycle and an array of texts.
Then on click:
Num.setText(texts[cycle]);
cycle=(cycle + 1)%texts.length;

Categories

Resources