Android - API - Getting the current method name with StackTraceElement

Always dreamed to see the stack without your debugger?

Maybe this tutorial will help you with the StackTraceElement Java class.

Of course we are going to use an Android application to see all that on our screen!

Let's get started.

First of all

I've created a while loop in order to use every element in the stack until the last one.
 
A special classe has been created (BadprogHelper) in order to use a specific method to have a delay between each element.
 
At the end we can display the name of the current method!
 
Very helpful.
 
Code

MainActivity.java

// Badprog.com
package com.badprog.android.tutorial.badprogactivity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.WindowManager;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

  TextView textView_1;
  BadprogHelper bh = new BadprogHelper();
  private int time = 0;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
    textView_1 = (TextView) findViewById(R.id.xml_textview_1);
    final StackTraceElement[] ste = Thread.currentThread().getStackTrace();
    int i = 0;
    i = ste.length - 1;
    while (i >= 0) {
      time += 500;
      bh.generateTimerClassic(textView_1, String.valueOf(i) + " " + ste[i].getMethodName(), time);
      --i;
    }
    bh.generateTimerClassic(textView_1, "\nThe name of this method is: \"" +  ste[2].getMethodName() + "()\".", time);
  }

} // end class MainActivity

BadprogHelper.java

// Badprog.com
package com.badprog.android.tutorial.badprogactivity;

import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

public class BadprogHelper {

  private TextView textView_1;

  /**
   * Manages a timer each time the method is called.
   *
   * @param textView_0
   * @param charSequence
   * @param delay
   */
  public void generateTimerClassic(TextView textView_0, final CharSequence charSequence, long delay) {
    textView_1 = textView_0;
    Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
      @Override
      public void run() {
        textView_1.append(charSequence + "\n");
      }
    }, delay);
  }

}

res/layout/activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    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"
    tools:context="com.badprog.android.tutorial.badprogactivity.MainActivity">

    <TextView
        android:id="@+id/xml_textview_1"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:scrollbars="vertical"
        android:textColor="@color/white"
        android:background="@drawable/back"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="10dp"
        android:padding="10dp"
        android:textStyle="bold"
        />

</RelativeLayout>

res/drawable/back.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="rectangle" >

    <!-- view background color -->
    <solid
        android:color="@android:color/holo_blue_bright" >
    </solid>

    <!-- view border color and width -->
    <stroke
        android:width="1dp"
        android:color="@color/colorPrimaryDark" >
    </stroke>

    <!-- The radius makes the corners rounded -->
    <corners
        android:radius="2dp"   >
    </corners>

</shape>

Conclusion

You are now able to create a debugger from scratch. ;-)

Well done you did it! laugh

 

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.