Open In App
Related Articles

How to Print to the Console in Android Studio?

Improve Article
Save Article
Like Article

In computer technology, the console is simply a combination of a monitor and an input device. Generally, the input device is referred to here as the pair of mouse and keyboard. In order to proceed with the topic, we have to understand the terminology of computer science, which is an important part of the process of developing software, and it’s called debugging. Debugging is the process of identifying a bug or an error and fixing it properly for the software. We have to test the software before producing it on market in multiple phases. We have to debug the errors also, then only software will be pure error-free and it will be ready for production. The most of the things which computer does with our code is invisible for us. For Debugging, we have to identify the error first then only we can solve that error. If you want to see the error, then you have to print or log it to our console directly. There are many and different methods in different programming languages for doing it. 

In C, We do it using printf(), in C++ we will use cout, and in Java, we generally use System.out.println. We all know, that in the android studio we have to code in a different way. Android has its own methods and components, we have to code the application using them. This is slightly different from normal programming. In today’s article we are going to learn about that how can we print to the console in Android Studio. 

What is Logcat Window?

The Logcat is a window in android studio, which displays system information when a garbage collection occurs and also the messages which you have added to your Log class. It displays the message in real-time. It also keeps the history of the messages. You can also learn more about the Logcat window from Here.

Introduction and Types of Log Class

The log class is a pre-defined class in android studio which allows the developer to print messages in Logcat Window, which is the console for Android Studio. Every message is written using a log, Contains a special type or format that represents for what purpose the message is being written. 


Log.d(tag, message);


Log.d(tag, message)

Above is the sample of a default code for printing something to the logcat. The d is the symbol here that the message is written for debugging the code. More symbols and types are below mentioned in the Log class. The priority of verbose is the lowest and the assert has the highest priority. Below is the list of types of messages in the log class in chronological order.

  • V (Verbose)
  • D (Debug)
  • I (Information)
  • W (Warning)
  • E (Error)
  • A (Assert)

Log class always takes two arguments, the tag, and the message. The tag is like an identifier of your message you can choose it according to preference and in place of messages, you have to type your log message.

How to Print to the Console in Android Studio using Log Class?

Now, we are aware that in the android studio we have to use the Log Class to print something on the Logcat window which is the console for android. So, Let’s see a real-world implementation of this method called Logcat.

Step 1: Start a New Project in Android Studio or Open an existing project on which you want to work. Here is the guide to Starting a new project on Android Studio.

Step 2: Go to your Java or Kotlin file for the activity, and in your onCreate method write the log messages with help of the Log class. Below is our code that we have used in the MainActivity for Random number generation and Printing log messages. 


package com.voicex.printlogmessages;
import android.os.Bundle;
import android.util.Log;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
    protected void onCreate(Bundle savedInstanceState) {
        final int random = new Random().nextInt(5);
        switch (random) {
            case 0:
                Log.i("NumberGenerated", "Function has generated zero.");
            case 1:
                Log.i("NumberGenerated", "Function has generated one.");
            case 2:
                Log.i("NumberGenerated", "Function has generated two.");
            case 3:
                Log.i("NumberGenerated", "Function has generated three.");
            case 4:
                Log.i("NumberGenerated", "Function has generated four.");
            case 5:
                Log.i("NumberGenerated", "Function has generated five.");

Note: Choose your desired activity, for which you want to print on console. For example, here we are working on MainActivity and generating a random number, and printing it using conditional statements. You can do this or can do something similar to it. 

At bottom of the page, we will also share the GitHub repository of the application which we have made in this article. You can refer to that.

Step 3: Now try to build and run your android application, in the meantime also click on the Logcat button which will be on the bottom. Log messages will appear according to the condition cause here we have used the conditional statements.

Button in Logcat Window

In the logcat window, these are the buttons for many tasks:

  • Clear logcat: Clears the visible Logcat window
  • Scroll to the end: Take you to the end of the logcat window, where you can see the latest messages.
  • Up the stack trace and Down the stack trace: Navigates up and down the stack traces in the log
  • Use soft wraps: Enables the line wrapping and prevents the horizontal scrolling
  • Print: Print logcat messages on paper or save them as a PDF.
  • Restart: Clears the log and restarts it.
  • Logcat header: Open customization options for log messages
  • Screen capture: Captures the logcat window as an image
  • Screen record: Records the video up to 3 Minutes of logcat window.

Search Logcat Window

You can select regex optionally, for using a regular expression search pattern. Then type something in the search field which you want to search. The search results will be displayed. If you want to store the search string in this session then press enter after typing the search string.

Filter in Logcat Window

On the top right corner of the logcat window, you will see a filter button and will find three options:

  1. Show only selected applications: Displays the messages generated by the app code only.
  2. No Filters: Apply no filters
  3. Edit Filter Configurations: Modify your custom filter or create new filters

GitHub link as a Resource: Click Here

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 23 Mar, 2021
Like Article
Save Article
Similar Reads
Complete Tutorials