Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Improve this question
I want to get user input of minutes/seconds for a timer
new CountDownTimer(30000, 1000) { }
The 30000 (30 second) value I want that from a user
t= (EditText)findViewById(R.id.time);
The value is stored in t
How do I achieve this?
new CountDownTimer(t, 1000) { }
This wont work...
You have to get the text from EditText from android convert it to long milliseconds.
t= (EditText)findViewById(R.id.time);
long userinput = Long.parseLong(t.getText().toString());
new CountDownTimer(userinput, 1000) { } ;
Though you have to do some input validation checking else you might get exception later on.
new CountDownTimer(t, 1000) { }
Here your t variable is EditText - its object and first parameter of CountDownTimer expects long so you need to get content of EditText and then parse it to long:
Long.parseLong(t.getText().toString());
Note: Also good practise is to validate input of User (input is not always correct you need to assume that User is "stupid" and can add some bullshit) if its correct number that can be parsed into long:
String input = t.getText().toString();
if (input.matches("\\d+")) {
// its valid number
Long.parseLong(t.getText().toString());
}
Or you can ensure it via XML when you'll specify inputType for EditText:
android:inputType="number"
t is EditText if you want get value in Long you must use:
Long.parseLong(t.getText().toString())
if you want any type else you must cast from t.getText.toString()
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I am creating an Android Studio app in the Kotlin language and that utilizes the doAfterTextChanged function as a TextWatcher for an editText. However, when the user changes the text in the editText after it has already been changed, the app crashes. Is there any alternative (and simple) function I can use or something to add to my program. Here is an example of what I currently have:
editText.doAfterTextChanged() {
score = score + 15 * editText.text.toString().toInt()
totalscore.text = "Score: " + score.toString()
}
What would happens when you clear the text from the EditText, e.g. making it empty (text = "")?
score = score + 15 * editText.text.toString().toInt()
In that scenario, you are basically trying to convert "" to an integer, which does not work obviously. NOTE, you are not seeing this the first time, because the code only triggers after text changed.
Try this instead:
score += 15 * (editText.text.toString().toIntOrNull() ?: 0)
totalscore.text = "Score: $score"
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I am a beginner at programming Android, please help with this simple project.
I want an integer to show up in toast, which I write in textBox. I do not know what to put in the highlighted part here.
Sorry for the link, but I cannot post the picture
You need to use the getText() method to get the text the user entered.
Change the myEdit declaration to:
final EditText myEdit = ... ;
and then use:
Toast.makeText(MainActivity.this, myEdit.getText().toString(), ...
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
in my application i have got 0.00 in edit text.next when i enter 2or3 or any digit its getting concat with 0.00 and getting 0.003.
if(item.equals("0.00")){
item=item+string;}
i changed the code.but this time too its getting an error
if(item.equals("0.00")){
int a=Integer.parseInt(item.toString());
int b=Integer.parseInt(string.toString());
int c=a+b;
item=String.valueOf(c);}
If you are using decimal places then you will want to use a data type that supports those, such as double. Try this:
double d = Double.parseDouble(item.toString());
NOTE: If both your variables are of string type, then you won't need to use toString()
You want O.OO but still using Integer. Try using FLOAT.
Also, FLOAT is NEVER a perfect value. So you might have .XX added.
You have an error, because "0.00" has decimal places, so it is parsed as a floating point number, not integer.
Parse it as a double:
String item = "0.00";
double result = Double.parseDouble(item) + 0.03; // result is 0.03
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I need to point out the users that a number can only start from 9 and and its length is to be exactly equal to 10 digits. How do I manipulate this with a regex ? Please don't mind the question, I am relatively new.
To validate that the input is exactly 10 digits, starting with 9, use:
/^9\d{9}$/
The ^9 part matches a starting 9, {d}9 matches 9 digits
Here you are. A simple solution (not with regex):
String number = "12345";
...
if (number.length() != 10 || number.startsWith("9") == false)
{
Toast.makeText(getApplicationContext(), "Please check your input.", Toast.LENGTH_SHORT).show();
}
Hope this helps. Write if you have any problems.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Well, I want a Text view to resolve the date and the time of the phone, and then add it as a variable in my email intent to be sent. How do I do this? I dont event know where to start.
I want a Text view to resolve the date and the time of the phone
The current date and time of the device can be retrieved using Date date = new Date().
To display the date in a TextView using the user's default formatting:
String dateTime = DateFormat.getDateTimeInstance().format(date);
textView.setText(dateTime);
and then add it as a variable in my email intent to be sent
Assuming you want to include it in the message body:
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, textView.getText().toString());
Do you want to fetch the current data and time and show it in a textview without user intervention? If yes, you can do so using the below code
DateFormat dateFmt = SimpleDateFormat.getDateTimeInstance(SimpleDateFormat.MEDIUM, SimpleDateFormat.MEDIUM);
String strDate = dateFmt.format(new Date());
and then you can show this string in your text view. and you can send the same in your email intent as part of your subject or body.