I wanted to print the value of a variable on the console for my debugging purpose, but System.out.println
doesn't work.
-
Related to: http://stackoverflow.com/questions/3007644/how-to-output-messages-to-the-eclipse-console-when-developing-for-android – Emanuil Rusev Mar 05 '13 at 09:34
9 Answers
System.out.println
and Log.d
both go to LogCat, not the Console.

- 60,504
- 58
- 273
- 437

- 53,459
- 16
- 107
- 112
-
1Using LibGDX and System.out.println goes to console. Looking for a way to get it into logcat so i can log/debug my app on a actual device. – Madmenyo Jul 23 '14 at 18:08
Writing the followin code to print anything on LogCat works perfectly fine!!
int score=0;
score++;
System.out.println(score);
prints score on LogCat.Try this

- 1,782
- 7
- 25
- 47

- 2,373
- 2
- 17
- 13
I'm new to Android development and I do this:
1) Create a class:
import android.util.Log; public final class Debug{ private Debug (){} public static void out (Object msg){ Log.i ("info", msg.toString ()); } }
When you finish the project delete the class.
2) To print a message to the LogCat write:
Debug.out ("something");
3) Create a filter in the LogCat and write "info" in the input "by Log Tag". All your messages will be written here. :)
Tip: Create another filter to filter all errors to debug easily.

- 18,244
- 26
- 87
- 112
-
8It is not a good idea to write code like this because the Log class is familiar to most...all android developers. By wrapping it up in your own class and not adding new functionality, all you do is obscure the meaning of your code to people other than yourself. – Thomas Dignan Mar 10 '11 at 18:51
I think the toast maybe a good method to show the value of a variable!

- 66
- 2
-
1Problem comes with Toast when values are many and fast, like a x,y coordinates logging on ACTION_MOVE. In such cases a screen text or a Log output might be better solution. – Niki Romagnoli Oct 03 '13 at 07:29
Ok, Toast is no complex but it need a context object to work, it could be MyActivity.this
, then you can write:
Toast.maketext(MyActivity.this, "Toast text to show", Toast.LENGTH_SHORT).show();
Although Toast is a UI resource, then using it in another thread different to ui thread, will send an error or simply not work
If you want to print a variable, put the variable name.toString()
and concat that with text you want in the maketext String parameter ;)

- 10,828
- 3
- 41
- 60

- 31
- 2
If the code you're testing is relatively simple then you can just create a regular Java project in the Package Explorer and copy the code across, run it and fix it there, then copy it back into your Android project.
The fact that System.out is redirected is pretty annoying for quickly testing simple methods, but that's the easiest solution I've found, rather than having to run the device emulator just to see if a regular expression works.

- 3,109
- 1
- 33
- 46
toast is a bad idea, it's far too "complex" to print the value of a variable. use log or s.o.p, and as drawnonward already said, their output goes to logcat. it only makes sense if you want to expose this information to the end-user...

- 3,934
- 10
- 48
- 72
By the way, in case you dont know what is the exact location of your JSONObject inside your JSONArray i suggest using the following code: (I assumed that "jsonArray" is your main variable with all the data, and i'm searching the exact object inside the array with equals function)
JSONArray list = new JSONArray();
if (jsonArray != null){
int len = jsonArray.length();
for (int i=0;i<len;i++)
{
boolean flag;
try {
flag = jsonArray.get(i).toString().equals(obj.toString());
//Excluding the item at position
if (!flag)
{
list.put(jsonArray.get(i));
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
jsonArray = list;

- 1,868
- 23
- 32