Android thread sleep inputted by user - android

I have a input box which the users set a time for the delay. Is it possible set delay time by user input in android?

If you are truly just wanting the user to input an integer you could do the following:
EditText tv = (EditText) view.findViewById(R.id.textview);
Button btn = (Button) view.findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Thread.Sleep(Integer.valueOf(tv.getText().toString());
}
});
Obviously the code above needs to be changed to fit your app (and checked for errors) but that is the general gist of the solution. You could check the user input for validation too.

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

Button to hide a TextView

I've been searching for a solution for this for a while but cannot seem to get one working. There are one or two on here about this subject but I can't seem to get them going. I'm also a novice in Android and while I've been on and off playing with it for a few years, I still understand next to nothing about what I'm writing.
Basically I've got a TextView and a button. Ideally I'd like to put some text in the TextView, press a button it's gone, press the button again and it's back.
I've narrowed it down to needing to understand what findViewById(R.id.button2) does but honestly I'm a bit lost.
I've added my button code but apologies that this is such a noob question
public void onClick(Button v){
TextView t1 = (TextView)findViewById(R.id.editText);
v.setVisibility(View.GONE);
Button button = (Button) findViewById(R.id.button2);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
TextView t1 = (TextView)findViewById(R.id.TextView);
v.setVisibility(View.GONE);
}
});
}
Your code has a couple of issues. I'm not going to give you the code because that won't really help you learn. Instead I'll explain things and let you try to figure it out or come back with more explicit questions.
You know that xml file you set using setContentView? Some of the tags in it had a property android:id="xxxx". That xxxx is the id of that view, its used so you can find that view in your code. The findViewById function walks through all the views on screen and finds a view with that id and returns it. That gives you a reference to the view so you can change it. For example, you can set its visibility, set its background color, or set an OnClickListener.
So to have a button toggle the visibility of another view, you need to be able to do the following things:
1)Find the view who's visibility you want to change
2)Figure out what its visibility currently is
3)Figure out what you want it to be (the opposite of what it currently is
4)Set that visibility
You need to write a function that does all that. Then you need to do this
1)Find the button you want to use to change the visibility
2)Tell it to call your function when its pressed.
Figure out how to do each of those steps individually, and you should be able to put it together. Good luck.
findViewById(R.id.button2) finds the view with the id button2.
You can check inside onClick whether t1 is visible or not (t1.setVisibility(View.GONE); not v.setVisibility(View.GONE);), and toggle between View.GONE and View.VISIBLE.
Remember that your findViewById() should have a real id. They are normally set on the activity_name.xml.
You are using a onClick inside a onClick. Personally I recommend setting the listener manually with setOnClickListener.
There's a lot of work for you, start with these tutorials. Keep trying and try to understand what you are doing.
Look like you need a toogle button feature, here is a piece of code.
Important: you must pay heed to #GabeSechan and #SkyDriver2500 answers.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
//your other code
Button button = (Button) findViewById(R.id.button2);
final TextView t1 = (TextView) findViewById(R.id.editText);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
t1.setVisibility(t1.getVisibility() == View.VISIBLE ? View.GONE : View.VISIBLE);
}
});
}
I'm not sure if the code will help you now. But just in case, here it is
final boolean[] isTvVisible = {false};
final TextView t1 = (TextView)findViewById(R.id.editText);
t1.setVisibility(View.GONE);
Button button = (Button) findViewById(R.id.button2);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (isTvVisible[0]) {
t1.setVisibility(View.GONE);
isTvVisible[0] = false;
} else {
t1.setVisibility(View.VISIBLE);
isTvVisible[0] = true;
}
}
});

Android - setOnkeyListen method how does it work with EditText?

I create a dialog window in an activity, then I put the buttons in this dialog window and listened to them. However, I want to listen to android device found in the back button. and dialog.setOnkeyListen method I used for it. I have put the program by executing dialog window EDITTEXT not enter data into the field. So when I add code to the setOnkeyListen I can not enter data into the EditText field. I hope you know what I mean
If you're asking how to set the OnClickListener for a textview here is an example hope this helps:
TextView textView = (TextView) findViewById(R.id.textView);
textView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
// Code you want to execute on the click goes here
}
});

Displaying a result after receiving user input

I'm developing an app in Android and need to implement a common app function- accepting numeric input from the user via a EditText. After typing in this value, I want the user to be able to be select a 'go' button. A result should then appear below the first TextView. What should I use to display the result (TextView etc?) and how can I go about implementing this process. Thanks
You are new to this so First check d link http://www.tutorialspoint.com/android/android_event_handling.htm .
Here You have to define 1 TextView , 1 EditText and 1 Button
like
Button _go = (Button) findViewById(R.id._btngo);
than
_go.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
TextView _getdata= (TextView) findViewById(R.id._tvresult);
EditText _result= (EditText) findViewById(R.id._etgetinput);
// get the input value and store as a string
String result = _result.getText().toString();
// display the result in textview
getdata.setText(result);
}
});
Hope you will find the solution .. :)

Make a edittext box display on a textview?

I am trying to make an android app for my local fence painting business and I simply want it to calculate quotes and payments. My problem is figuring out how to get a simple input box for the square feet of the fence (edittext) with the help of a button to display a that input to a textview. Will you help me?
Also if there is a different way to do this I would love to hear it.
Thank you!
My problem is figuring out how to get a simple input box for the
square feet of the fence (edittext) with the help of a button to
display a that input to a textview.
Pretty simple. Create a TextView, a Button and an EditText object:
TextView txtView;
Button btn;
EditText edtText;
Initialize them:
txtView = (TextView) findViewById(R.id...);
btn = (Button) findViewById(R.id...);
edtText = (EditText) findViewById(R.id...);
This is how you get the input of the EditText by clicking the Button and set the input to the TextView:
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String inputText = edtText.getText().toString(); // Get the input
txtView.setText(inputText); // Set the text to the TextView
}
});

Categories

Resources