18

Is it possible to display the log messages (which I print using android.util.Log) on screen in an Android application?

Is there any other better method to just output lines on the screen?

Something like System.out.println?

TryinHard
  • 4,078
  • 3
  • 28
  • 54
n1kh1lp
  • 14,247
  • 6
  • 29
  • 28
  • 14
    Amazing how people just don't read the question. Obviously the OP wanted to print lines of text on the screen and all the answers keep pointing to logging solutions. – Kyberias Apr 08 '11 at 18:03

4 Answers4

3

Yes zero4

what you are attempting to do is dropping 'logcat' comand on android shell & getting command output as output stream.This link will help you.

100rabh
  • 6,156
  • 5
  • 27
  • 41
3

Like others have suggested, you can use log cat. If you are using the emulator or debugging a device, you can use adb logcat to view the messages. In Eclipse debug perspective, there is a window that will do that for you.

Another way, without a debugger attached, is to use the CatLog - Logcat Reader application.

kgiannakakis
  • 103,016
  • 27
  • 158
  • 194
  • 1
    It is very useful, but if you need the logs included in your app, 100rabh's answer below is what was asked for. (a method for reading android.util.Log and displaying it in an app) – dubmojo Jul 25 '13 at 17:05
  • 1
    Also, CatLog now needs Root. If you don't have Root available, the only way I've found so far is to call "logcat" from your app as a non-root shell command. (see the link 100rabh's answer provided) – dubmojo Jul 25 '13 at 17:08
  • the link to androidPIT.com is broken 1/25/2018 – Mneckoee Jan 25 '18 at 12:11
2

I use "android.widget.Toast.makeText(Context context, CharSequence text, int duration)" to do something like what you are asking. Seems like the easiest way to get some quick messages on the screen and make it go away automatically (based on the last parameter).

:-)

  • 1
    Really? Toast is for a message, not a stream of log output like android.util.Log will take. Try Toast'ing with LENGTH_SHORT 50 times in 10 seconds. You'll be viewing Toasts one after the other for 5 minutes. Also, how are you reading from android.util.Log? – dubmojo Jul 25 '13 at 17:02
  • Refer to the question before u decide to down vote. He wanted a way to print log messages to the screen. Note the question was not how to read from log and writing to screen, but controlling it at the time of his code doing the log itself. I was not suggesting writing ALL his logs using toast (which clearly is not a good idea), but if he wanted to see SOME key messages on screen (which was the primary requirement in his question), I still stand by my "alternate" Toast suggestion (in addition to logcat), unless I see your "better and generic" solution which is NOT logcat. – Kumar Rangarajan Jul 28 '13 at 06:59
  • I did. "He wanted a way to print log messages to the screen." No, he wanted a way to print android.util.Log messages to the screen. If you've needed to do this yourself (which I do, which is why I stumbled on this SO question) you wouldn't have suggested Toast, which doesn't help read from Log from an app, nor a method of displaying the kind of messages that are logged to android.util.Log. Log is not just a way to debug where Toast is an alternative debug tool. You can't Toast from just anywhere in an app, but you can Log. A -1 loses points for me as well. This answer doesn't help. – dubmojo Jul 28 '13 at 15:34
  • Fair point. Guess I misunderstood the Q and deserve the -1 :) – Kumar Rangarajan Jul 29 '13 at 18:17
  • excellent idea!. i lost my logcat like many others (see: http://stackoverflow.com/questions/17432358/android-studio-logcat-nothing-to-show). this is a great work around. thanks – Ray Tayek Oct 04 '15 at 17:50
0

Well, there is a solution to log anything you want on screen using this lib. It didn't worked for me, so I develop my own solution you can find an example of it here. It's really simple, just add a class OnScreenLog to your project

package br.com.ideiageni.onscreenlogSample;

import android.app.Activity;
import android.graphics.Color;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;


/**
 * Created by ariel on 07/07/2016.
 */
    public class OnScreenLog {
    private static int timeoutTime = 1000;
    private static TextView tvLog;
    private static int logCount = 0;
    private static int logCountMax = 30;
    private static String[] logs = new String[logCountMax];
    private static int cntClicks = 0;
    private static boolean visibility = false;
    private static Activity activity;
    private int maxClicks = 5;

    public OnScreenLog(){}

    public OnScreenLog(Activity activity, int ViewID){
        OnScreenLog.activity = activity;
        tvLog = new TextView(activity.getApplicationContext());
        maintainLog("Log is working");
        tvLog.setLayoutParams(new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT));
        tvLog.setTextColor(Color.BLACK);
        tvLog.setBackgroundColor(Color.LTGRAY);
        tvLog.setAlpha((float) 0.4);

        View v = null;
        LinearLayout linearLayout;
        RelativeLayout relativeLayout;
        try {
            linearLayout = (LinearLayout) activity.findViewById(ViewID);
        } catch (ClassCastException e) {linearLayout = null;};

        try {
            relativeLayout = (RelativeLayout) activity.findViewById(ViewID);
        } catch (ClassCastException e) {relativeLayout = null;};
        if(linearLayout != null) {
            linearLayout.addView(tvLog);
            v = linearLayout;
        } else if(relativeLayout != null) {
            relativeLayout.addView(tvLog);
            v = relativeLayout;
        }

        if(v != null) {
            v.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    switch (event.getAction()) {
                        case MotionEvent.ACTION_DOWN:
                            cntClicks++;
                            timerHandler.removeCallbacks(rTimeout);
                            timerHandler.postDelayed(rTimeout, timeoutTime);

                            if (cntClicks > maxClicks-1) {
                                setLogVisible(!visibility);
                                timerHandler.removeCallbacks(rTimeout);
                                cntClicks = 0;
                            }
                            break;

                    }
                    return false;
                }
            });
        }

    }

    public void log (String text){
        String logText = text;
        maintainLog(logText);
    }

    public void log (int text){
        String logText = String.valueOf(text);
        maintainLog(logText);
    }

    public void log (int[] text){
        StringBuilder builder = new StringBuilder();
        for (int i : text) {
            builder.append(i);
            builder.append("-");
        }
        String logText = builder.toString();
        maintainLog(logText);
    }

    public void log (byte[] text){
        StringBuilder builder = new StringBuilder();
        for (int i : text) {
            builder.append(i);
            builder.append("-");
        }
        String logText = builder.toString();
        maintainLog(logText);
    }

    private void maintainLog(String newText){
        String logText = "";
        if(logCount<logCountMax) logCount++;
        for(int i=logCount-1; i>0; i--){
            logs[i] = logs[i-1];
        }
        logs[0] = newText;
        for(int i=0; i<logCount; i++){
            if(i<logCount-1) logText+=logs[i]+System.getProperty("line.separator");
            else logText+=logs[i];
        }
        tvLog.setText(logText);
    }

    public void clearLog(){
        tvLog.setText("");
    }

    public void setLogVisible(boolean visibility){
        if(visibility) tvLog.setVisibility(View.VISIBLE);
        else tvLog.setVisibility(View.INVISIBLE);
        OnScreenLog.visibility = visibility;
    }

    public static int getLogCountMax() {
        return logCountMax;
    }

    public static void setLogCountMax(int logCountMax) {
        OnScreenLog.logCountMax = logCountMax;
        logs = new String[logCountMax];
    }

    public int getMaxClicks() {
        return maxClicks;
    }

    public void setMaxClicks(int maxClicks) {
        this.maxClicks = maxClicks;
    }

    Handler timerHandler = new Handler();
    Runnable rTimeout = new Runnable() {

        @Override
        public void run() {
            cntClicks = 0;
        }
    };
}

then, for instance:

public class Activity1 extends AppCompatActivity {

private OnScreenLog log;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_1);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    log = new OnScreenLog(this, R.id.content_1);
    log.log("Started log on Activity 1");

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(getApplicationContext(), Activity2.class);
            startActivity(intent);
            log.log("Starting Activity 2");
            Snackbar.make(view, "Starting Activity 2", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
}

Where R.id.content_1 is the name of the main LinearLayout or RelativeLayout of your activity.

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/content_1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="br.com.ideiageni.onscreenlogSample.Activity1"
tools:showIn="@layout/activity_1">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Activity 1!" />

</RelativeLayout>

Neither solutions print the current log messages, so you'll need to tell it to log to screen the same informations you log today on your current log.

Work not finished yet but can be used for anyone in need. Missing some directions on how to use. Suggestions are welcome.