1

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?

2 Answers 2

2

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"
        />
Sign up to request clarification or add additional context in comments.

3 Comments

Produces an extra container around. I will avoid that
This is the way Android internals are implemented. Merge does not produces any containers, but just re-uses custom class as a container.
Ah sorry, I overread the <merge> tag ... Yeah I guess that would be the best solution
2

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.

3 Comments

Yes I know this two possibilities and typically I also use a combination of both: I use <include> and than write some kind of Wrapper class that get the included view as constructor parameter ... But I would like to directly use the CustomView in any other xml layout ...
@sockeqwe: "But I would like to directly use the CustomView in any other xml layout" -- which can be achieved via option #2, particularly using a <merge> root element to avoid an extra container.
@CommonsWare Yeah that already came in my mind and i guess thats the only solution to achieve something like that directly in android. But I thought there will exist a tool somewhere in the world wide web that would do that autmomatically (You see Im a little bit lazy :D)

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.