1

I'm trying to debug a view that I'm creating programmatically. I'd like to be able to see what the exact XML is of the view after it's created.

Really I'd like to be be able to do something like Log.d(TAG,view.toString())

And get an output like this:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    android:id="@+id/activity_main"
    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="sampleapp.example.com.tr221test.MainActivity">

    <Button android:id="@+id/surveyButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Take Survey!"/>
</RelativeLayout>

Is there anyway I can programmatically see what the xml is of a view?

3
  • Is this for debugging purposes? Commented Mar 12, 2017 at 18:12
  • Indeed. I'm pretty sure that I have messed up the order of the additions of some views - trying to re-create an existing view programmatically. Commented Mar 12, 2017 at 18:16
  • See CommonsWare's answer for links to appropriate tools. Commented Mar 12, 2017 at 18:18

1 Answer 1

2

There is nothing built in for that. You would have to write your own View-hierarchy-to-XML code or see if somebody has a library for that.

Or, use Android Studio's layout inspector.

Or, use uiautomatorviewer.

Or, use Stetho.

Sign up to request clarification or add additional context in comments.

2 Comments

Layout inspector is a nice new tool! I don't suppose there's any easy way to copy that over as XML? :)
@TomHammond: Not that I am aware of. Stetho's output is XML, but more in an attempt to get a layout to appear to be valid HTML for Chrome's Dev Tools to handle.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.