Tom Hammond
Tom Hammond

Reputation: 6080

Android View View XML Programmatically

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?

Upvotes: 1

Views: 200

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006614

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.

Upvotes: 2

Related Questions