Android Println() to a TextView - java

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

Related

How to convert a string in java to be accessible for an xml TextView for android

I'd like to take a string which was declared and assigned in MainActivity.java and have it be assigned as the text for a TextView object in a file named activity_second.xml. Any suggestions?
i'm not sure it's what you're asking since i can't see your code but i think it's;
String b= "This is a string variable.";
TextView textView2 = (TextView)findViewById(R.id....);
textView2.setText(b);

TextView.setText(int) causes app to crash

I was implementing the following Java code in Android Studio:
private void display(int number) {
TextView quantityTextView = (TextView) findViewById(R.id.quantity_text_view);
quantityTextView.setText(number);
...
}
This is a part of a larger application.
As you can see, I've passed only an integer value to the quantityTextView.setText(number) method.
When running the app, it crashes as soon as this method is called. Can you tell me why such a thing is happening?
Yes, use String.valueOf(), like this:
private void display(int number) {
TextView quantityTextView = (TextView) findViewById(R.id.quantity_text_view);
quantityTextView.setText(String.valueOf(number));
}
Because setText() accepts only String values or Resource ID of a String (which is infact int).
Check here: setText() Method
You can use String.valueOf(number); as input parameter of setText() or you can refer to String ID in XML with getResources().getString(R.string.number) as input value.
Convert the integer to string before putting it in the TextView:
quantityTextView.setText(Integer.toString(number));
or simply
quantityTextView.setText(number+"");
The reason your code is crashing is that setText(int) expects a resource ID. It's not very well documented, so you'd be forgiven for thinking that you could pass it an integer and have the TextView convert it to text.
You should first convert it to a String, for example with:
String.valueOf(number)
and then it will be alright.
setText() method of TextView accepts CharSequence, not integers. So, you must convert your number to String before.
Try to use this:
quantityTextView.setText(Integer.toString(x));
The reason is that, setText() only expects string or char[].
So either you can perform type casting or you can add quotes with the number
(1). by type casting
String.valueOf(number)
(2). by adding "" with the number
quantityTextView.setText(""+number);
or
quantityTextView.setText(number+"");

Android Studio getText() and setText() not working with error: "can't resolve method "getText/setText"()"

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

Getting the a text from EditText (but a Number)

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

String from EditText to float

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

Categories

Resources