sockeqwe
sockeqwe

Reputation: 15929

Xml layout to Java code generator

Does anybody know a tool to create java code from a xml layout file.

It would be useful, to create quickly a custom view (I do not want to create a separate library project) that I would like to include in an activities layout.

So lets say my custom view would be a Relative Layout with some child views.

It would be great if the tool could generate from a layout file like this:

<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">

    <!-- some child Views -->

</RelativeLayout>

a java class like this:

class CustomView extends RelativeLayout{

    /*
     * Generate all the Layout Params and child Views for me
     */
}

And at the end I could use this generated class in a normal XML

<LinearLayout
    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"
    />

    <TextView 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    text="Hello World" />


    <com.example.CustomView
    android:layout_width="match_parent"
    android:layout_height="100dp" 
    />

</LinearLayout>

Does such a tool exists?

Upvotes: 1

Views: 1355

Answers (2)

sergej shafarenka
sergej shafarenka

Reputation: 20416

It would be useful, to create quickly a custom view (I do not want to create a separate library project) that I would like to include in an activities layout.

You can already do it. Create a custom view class and inflate custom layout there.

package com.example.view;
class CustomView extends LinearLayout {
    public CustomView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        LayoutInflater.from(context).inflate(R.layout.custom_view, this, true);
    }
}

Create a layout for that custom view class using <merge> tag as the root. Android will add content of tag into your custom view class, which is, in fact, LinearLayout in our case.

// custom_view.xml
<merge xmlns:android="http://schemas.android.com/apk/res/android"

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        text="Hello World" />

</merge>

You are done. Now you can add this custom class to your layout.

<com.example.view.CustomView
        android:id="@id/title"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        />

Upvotes: 2

Gabe Sechan
Gabe Sechan

Reputation: 93569

No because there's 2 better ways to do it.

1)Use an <include> tag. That allows you to include a 2nd xml file.

2)Use a custom class, but have it inflate the second xml in its constructor. That way you can keep the layout in xml for the class.

Typically I use 2 if I want to create custom functionality where you set/change multiple values at one time, and 1 if I just want to break up my xml file into chunks.

Upvotes: 2

Related Questions