I know that you can get a normal text like this:
EditText input = (EditText) findViewById(R.id.input);
String value = input.getText().toString();
but now I need a to an input like this: 0xFFFFFF (a Hex-Color), an Integer... But value is only a String and I don't know how to convert it... I'm using view.setBackgroundColor(color);
Thanks a lot!
you can use the Color.parseColor(colorString); function to convert your string to a color int
Related
So my app needs to read text from a text box with a tag of "inText" do stuff to it (that stuff works) then write the output to a box with the id of "outView". I've been doing this with setText() and getText().
setText() was for writing the output below is what I used:
(TextView)findViewById(R.id.outView.setText(textoutput));
getText() was for reading the input text then writing it to a variable and below is what I used:
String mEdit = (EditText)findViewById(R.id.inText.getText()).toString();
You're chaining the method at the wrong spot. Remove .getText() from R.id.inText and place it after the brackets like (same thing for TextView):
String mEdit = ((EditText)findViewById(R.id.inText)).getText().toString();
Though this is an uncommon way to do thing. Rather initialize the EditText first and then get the text, it's much clearer that way:
EditText mEdit = (EditText)findViewById(R.id.inText);
String mText = mEdit.getText().toString();
You set the getText() and setText() method in wrong place.
getText() and setText() are methods of TextView and EditText classes.
But here you used it as a method of id. That's why it's showing "Can't resolve method getText/setText()". As id has no such methods.
You can do the following.
((TextView) findViewById(R.id.outView)).setText(textoutput);
and
String mEdit = ((EditText) findViewById(R.id.inText)).getText().toString();
It's not working since you need to first set the TextView:
TextView tvOut = (TextView) findViewById(R.id.outView);
TextView tvIn = (TextView) findViewById(R.id.inText);
String out = tvOut.getText().toString();
String in = tvIn.setText(out);
If you use a field check the compiler didn't automatically set it as a view instead of EditText type.
Also the casts are redundant now so you better not use them if you don't have to
I would like to take some variables, listed here;
double dBench = (((Math.floor((dWeight*.664)/10))*10)-10)+dPush;
double dFlies = (dBench)*.6;
double dCurls = (dWeight*dPull)*.25;
double dLats = ((Math.floor((dWeight*.5)/10))*10);
System.out.println(dBench);
System.out.println(dFlies);
System.out.println(dCurls);
System.out.println(dLats);
But how do I get the println()'s to print to a textview or something? Thanks.
You do not have to do println to textview. TextView has method called setText and getText. setText can set text to TextView and getText can get text from textview. Look here for more info.
Android Tutorial for TextView
For double,
textView = (TextView)findViewById(R.id.myTextView);
textView.setText(String.valueOf(Your double value));
Use the textview's set text method
textView = (TextView)findViewById(R.id.myTextView);
textView.setText("This text will be placed in the textView");
For a double value it will look like this
textView = (TextView)findViewById(R.id.myTextView);
textView.setText(String.valueOf(double_variable);
In case you want to print a value in textView:
TextiView textView = (TextView) findViewById(R.id.textViewId);
textView.setText(...see below...);
Integer: Integer.valueOf(integerVariable).toString()
the same can be done with Long, Double etc. Here is analogic example for Double:
double a = some value;
String toShow = Double.valueOf(a).toString();
textView.setText(toShow);
If you want to check those values (debug) you can use Log class.
Log.d("VALUE", Integer.valueOf(integerVariable).toString());
In this case the value will be shown in LogCat (tag VALUE). You can filter messages typing tag:VALUE
Integer, Long, Double are wrapper classes for primitives and those classes have toString() method which you can use to pass String into the TextView.
If you want to see the value which is stored into variables you can use log cat commands to print them into console as follows:
Log.v("Title" , variable_name);
or for numeric value you can use
textView = (TextView)findViewById(R.id.myTextView);
textView.setText(""+Your double value);
I want to display an integer that the user will define in the XML.
TextView Years = (TextView) findViewById(R.id.Years);
Years.setText(Age);
I know that this is right, but I don't know what to put in the XML to have the integer displayed.
Thank you.
You put strings in xml. And if you want to use that string as an integer. You use the line
int number = Integer.parseInt(stringNumber);
Basically, I want an EditText in Android where I can have an integer value entered into. Perhaps there is a more appropriate object than EditText for this?
For now, use an EditText. Use android:inputType="number" to force it to be numeric. Convert the resulting string into an integer (e.g., Integer.parseInt(myEditText.getText().toString())).
In the future, you might consider a NumberPicker widget, once that becomes available (slated to be in Honeycomb).
Set the digits attribute to true, which will cause it to only allow number inputs.
Then do Integer.valueOf(editText.getText()) to get an int value out.
First of all get a string from an EDITTEXT and then convert this string into integer like
String no=myTxt.getText().toString(); //this will get a string
int no2=Integer.parseInt(no); //this will get a no from the string
You can do this in 2 steps:
1: Change the input type(In your EditText field) in the layout file to android:inputType="number"
2: Use int a = Integer.parseInt(yourEditTextObject.getText().toString());
I have an EditText for which will be using for a float number. So I'm trying to read the text from the EditText and put it into a float variable. But I seem to have a text to float problem. This is the line I have:
float Number = ( ( EditText )findViewById( R.id.edit_float ) ).getText();
I've tried using Float.parseFloat(string) and just general casting, but nothing seem to do it. What can I do here? Also, is there a way to check for a valid float number before writing it to a variable?
Try this.
EditText edt = (EditText) findViewById(R.id.edit_float);
float number = Float.valueOf(edt.getText().toString());
You use the valueOf() method if the Float wrapper class to convert a string to a float. IN this example I get the Editable object of that EditText with getText() on which I call the toString() method to obtain a string from it.
Update: Totally right guys sorry. Time to increment my sheep counter.
float getFloatFrom(EditText txt) {
try {
return NumberFormat.getInstance().parse(txt.getText().toString()).floatValue();
} catch (ParseException e) {
return 0.0f;
}
}
Do NOT use Float.valueOf() if you want the code to work in countries that use decimal comma instead of decimal point.
String stext = editText.getText().toString();
float text = Float.parseFloat(stext);