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?

sockeqwe
  • 15,574
  • 24
  • 88
  • 144

2 Answers2

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.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
  • Yes I know this two possibilities and typically I also use a combination of both: I use 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 Aug 18 '13 at 21:12
  • @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 `` root element to avoid an extra container. – CommonsWare Aug 18 '13 at 21:19
  • @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) – sockeqwe Aug 18 '13 at 21:26
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"
        />
sergej shafarenka
  • 20,071
  • 7
  • 67
  • 86