Update "Log.i" in TextView with ScrollBar - android

I have in my layout.xml a TextView with "id = txtLog".
Where do the test results from my application using:
Log.i("Result:", "Value of x = " + x);
for show result in LogCat.
It is possible to show these results "Log.i" within the TextView?
Note: I left a space at the bottom of my application to show the TextView.
Like a console.
I would like to display these messages on TextView.
If possible create a scroll bar and display every time I use Log.i
I am a beginner, do not know if it is possible. Yet thanks.

I would think
myTextView.setText(myTextView.getText() + "Value of x = " + x + "\n");
would work.
EDIT:
Also, to make the TextView scrollable, you need to set a movement method like so:
myTextView.setMovementMethod(new ScrollingMovementMethod());
EDIT 2:
If you want the information to go to both Log.i and a TextView, then you need a method that holds a reference to the TextView you want to update.
public static void LogToView(TextView myTextView, String title, String message) {
Log.i(title, message);
myTextView.setText(myTextView.getText() + title + ": Value of x = " + x + "\n");
}
Put that in whatever class or in your Activity class. Use it instead of Log.i and the message will be passed to both.

Related

Android java compare xml string with edittext

I want to compare a xml string with a string from an edittext oder button.
first I set the text of the button:
button1.setText(getString(R.string.okey));
and now I want to check if the text from the button is the same as R.string.okey from the xml file. Like this I can leave out a new variable.
Is it possible to check if the strings are the same with something like this?
if (button1.getText().toString().equals(getString(R.string.okey))){
}
But that doesn't work for me.
Thank you in advance.
this must work, its just to simple. you must change somehow text on button or maybe getString returns different text (Locale changed?). use logging or debugger to check what is button1.getText().toString() and getString(R.string.okey) at the moment of comparison (equals call)
boolean areEqual = button1.getText().toString().equals(getString(R.string.okey));
Log.i("justChecking", "getString:" + getString(R.string.okey) +
", button1.getText:" + button1.getText().toString() +
", are equal:" + areEqual);
if (areEqual){
}
How to Write and View Logs with Logcat
Store them in variables
String a = button1.getText()+"";
String b = getString(R.string.str)+"";
if(a.equals(b)){ }

String with \n in single string android

I have multiple value in single string, but I want to send in every string value on new line. I have written the following code, but it's not working.
String text ="Address" + strpropertyAddress +"\n"+ "Price" + strPrice;
This is my service method where i pass the string.
sendPropertyApi(text, sendto);
I have checked every where, and it is exactly same. Basically the string data should be sent in on a new line, but it's sent in on a single line.
Assuming(Because of Android tag) you are setting this text in textView or editText, for that I think you have to escape \. So use \\n instead. Try following:
String text ="Address" + strpropertyAddress +"\\n"+ "Price" + strPrice;
Or it is always better to use default line separator like following:
String text ="Address" + strpropertyAddress +System.getProperty("line.separator")+ "Price" + strPrice;

I Build a Brain Trainer app but Not Showing my TEXTVIEW when i run my app and warning is showing do not concatenate text display with setText

sumTextView.setText(Integer.toString(a) + " + " + Integer.toString(b));
This Line show warning you see in pic..
Use String.format();
sumTextView.setText(String.format("%1$d + %2$d", a, b));
With this you can format a string correctly with multiple variables, no matter whether they are strings or integers. This example takes the value of variable a and replaces the placeholder %1$d with it. Same goes for the other variable.
take an string copy whole line in it, then show string in setText
String str = (Integer.toString(a) + " + " + Integer.toString(a));
sumTextView.setText(str);
1. The First String Says that do not concate string with setText property.
String txt = String.valueOf(a) + " + " + String.valueOf(b);
sumTextView.setText(str);
2. Second warning says that your program have possibility to crash or genearte an exception in case if value of a or b is null or not an integer.
So check condition if(a!=null and b!=null) then display text in if condition.

Changing text within textview

So is it possible to change text size of part of the text in textview dynamically
Let's say we have a method that does that...what am I missing...I want to make x variable twice as big as y...they are string btw...
private void Data_transfer() {
test3.setText( x + " " + y);
}
You can use the Spannable type with a TextView to set multiple styles in one field.
In your case, you can do this like so, using the fromHtml() method which generates a Spannable string:
test3.setText( Html.fromHtml( "<font size='10'>" + x + "</font> <font size='5'>" + y + "</font>" ) );
And you can, of course, change 10 and 5 to whatever values you like.
private void Data_transfer() {
TextView tv = (TextView)findViewById(R.id.textView1);
tv.setText("Welcome to android");
}

TextView setText options

Trying to set a TextView, to a certain message containing a variable.
for Example
tvOddEven.setText("You have entered:", odd, "% of numbers");
odd is an int variable
it's giving me an error, that I can't use those parameters.
ideas?
tvOddEven.setText("You have entered:" + odd + "% of numbers");
Try this:
tvOddEven.setText("You have entered:" + String.valueOf(odd) + "% of numbers");
Since odd is not a String you need to convert it to string.

Categories

Resources