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);
}
});
Related
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;
}
};
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.
The question really is that simple. I have an activity with a button that randomly changes the text of a textview box. How do I add another button that gathers the previous number generated so the previous textview text comes back - for a quotes app.Is there a feature I require? I have searched, but I cannot find a feature that will 'go back' on the random number generator.
Thank you in advance.
You dont need to go back on the generator itself, just remember the previous values. Store it in a variable. Here we are using prev as an array, because we need it to be final so we can access it in the anonymous inner classes (click handlers). But we also need to change it. So we use the prev[0] value.
Here I am assuming you have a button called nextNum which generates the next random number. But that could be anything, it doesnt matter. When you generate a new random number simply update the 0th element of the prev array and you should be fine.
final String[] prev = new String[] { "" }
#Override
public void onCreate(Bundle bundle) {
// ....
Button BackQuote = (Button)findViewById(R.id.back);
final TextView display = (TextView) findViewById(R.id.textView1);
// number generator button
Button nextNum = (Button) findViewById(R.id.random);
nextNum.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// generate a random number
// store it in prev
prev[0] = rndNum.toString();
}
});
BackQuote.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
display.setText(prev[0]);
}
});
}
Unless there is a specific reason you want to avoid this, then this would work perfectly fine. And there is no way to go back on a random generator. By definition it IS random! (Yes, Pseudo-random. Nonetheless)
So if you want to keep an history, you are going to have to implement those yourself. I have yet to see a random generator which has a history feature. I'd be surprised if i do too.
I am currently creating an android application with different options. One of the option would be to have a button that would show "Activate" as default. When the application would be running, clicking on it would change it to "Disable" and then to "activate" if clicked again. I believe that all I have to do is to .getText with a string variable then use this variable in a if statement but it seems like it is not reacting to any of my conditions...
final Button button = (Button) findViewById(R.id.bSensor);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
String buttonText = button.getText().toString();
if (buttonText == "#string/Disable") {
button.setText(R.string.Enable);
}
else if (buttonText == "#string/Enable"){
button.setText(R.string.Disable);
}
}
});
Thanks for help
Phyzikk
You shouldn't use the == operator when comparing strings in Java. Source
You should either use the .equals() method of the string, or alternatively you could keep a global boolean state flag to determine which value is set. This way you won't need to do a string compare every time you need to figure out if it's active or disabled.
Use .equals to compare strings. You wont need the #String/ prefix as this is not part of what the button displays.
final Button button = (Button) findViewById(R.id.bSensor);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
String buttonText = button.getText().toString();
if (buttonText.equals(getResources().getText(R.string.Disable)) {
button.setText(R.string.Enable);
}
else if (buttonText.equals(getResources().getText(R.string.Enable)){
button.setText(R.string.Disable);
}
}
});
Im going to write some android app, which will basically consists of two activities. So first should have a lot of buttons (100+) and on click on any of them I will just get some special id and move to second activity. But is there any alternative to declare that hundreds of buttons and copy/paste one piece of code to every of them setting almost same onClickLister? Is there any special construction? Thanks
Edit: every of buttons are actually indexed from 1 to n. And on the click second activity will be launched and get that index to show it. I cant basically use any spinner or smth else, because there will be 3 rows of clickable things and each of them carring different images
Edit 2: so, to give you an idea, im going to do some table of buttons like in Angry Birds menu when you actually choosing the level you want to play. So, on click you will get id of button and start second activity
Call the method to add buttons
private void addButton(){
LinearLayout view = (LinearLayout) findViewById(R.id.linear_layout_id_here);
Button btn = null;
int w = 50;
int h = 25;
for(int i=1; i<100; i++) {
btn = new Button(this);
btn.setLayoutParams(new LayoutParams(w,h));
btn.setText("button " +i);
btn.setTag(""+i);
btn.setOnClickListener(onClickBtn);
view.addView(btn);
btn = null;
}
}
Call this method for handling onclick on button
private View.OnClickListener onClickBtn = new View.OnClickListener() {
public void onClick(View view) {
final int tag = Integer.parseInt(view.getTag().toString());
switch (tag) {
case 1:
// Do stuff
break;
case 2:
// Do stuff
break;
default:
break;
}
}
};
You should use a ListView.
ListViews are great for handling a lot of items at the same time. They are also natural for the user. Additionally, you use only one click listener - OnItemClickListener.
There's a useful example on how to work with ListViews in the Android Referenence.
You may add buttons in code, something like this:
#Override
public void onCreate(Bundle savedInstanceState) {
/*your code here*/
GroupView gw =findViewById(R.id.pnlButtonscontainer); //find the panel to add the buttons
for(int i=0; i<100; i++) {
Button b = new Button(this);
b.setLayoutParameters(new LayoutParameters(w,h));
b.settext = i+"";
b.setOnClickListener(new OnClickListener(){
});
}
}
I coded directly into browser, so some syntax error may appear in my code, but this is the point, a way, not the only one, to add 100 buttons.