2

I've created a custom view which extends RelativeLayout, and I want to preview how it looks in the designer.

The java is something like this:

// The view for a snap in the search/browse fragment.
public class MyView extends RelativeLayout
{
    public MyView(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
        LayoutInflater inflater = LayoutInflater.from(context);
        inflater.inflate(R.layout.the_layout, this);
    }

    public void setData(String text)
    {
        mText.setText(text);
    }

    // *** Views ***
    private TextView mText;

    @Override
    protected void onFinishInflate()
    {
        super.onFinishInflate();

        mText = (TextView)findViewById(R.id.text);
    }
}

And the XML is like this:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <!-- (And lots more views) -->
</RelativeLayout>

There are a few problems with this however:

  1. This actually creates a RelativeLayout within a RelativeLayout which is pointless. It can be solved by changing the XML <RelativeLayout> to a <merge> but then I can't preview the layout at all!
  2. I want to use the isInEditor() function (or whatever it is called) in the java to add some sample data for previewing purposes, but I can't find a way to tell the XML editor that it should display a preview of my class instead of the actual XML.

One unsatisfying solution I can think of is to create an empty preview.xml file with only <com.foo.bar.MyView/> in it... But that seems kind of silly. Is there a better way? (I don't really care about editing as much as previewing, since - let's face it - Eclipse/ADT are way too slow and flaky to make graphical layout editing usable.)

Timmmm
  • 88,195
  • 71
  • 364
  • 509
  • Try adding the 2 other constructors (MyView(Context context){this(context,null);} and MyView(Context context, AttributeSet attrs){this(context,attrs,0);} – Simon Sep 20 '12 at 17:30
  • Yeah that makes no difference. Not sure why it would either. For now I am just going with the `preview.xml` solution. – Timmmm Sep 21 '12 at 09:18
  • There is a long winded way to solve. If you install IDEA and import your project, it will create a log showing an errors encountered when previewing a custom class. Quite often, I find classes have a dependency on some other class in their constructors. With a little rearranging and perhaps some additional field initialisation, I've always managed to get my custom layouts to preview correctly. Anyway, good luck with it... – Simon Sep 21 '12 at 09:45
  • See also: [Custom Android Views in Eclipse Visual Editor](http://stackoverflow.com/a/10743970/1402846). – Pang Jan 05 '13 at 03:35

1 Answers1

1

If I understand your problem correctly I would say that the solution is to just replace the "RelativeLayout" tags (or any other tag for that matter) in your xml layout with "your.packagename.MyView" as in:

<your.packagename.MyView xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent" >

  <!-- (And lots more views) -->

</your.packagename.MyView>

If you'll get any exceptions regarding MyView class while running your app, add all the missing super/parent constructors.

I do this for almost all of my custom xml layouts. Extending your class with RelativeLayout, LinearLayout or any other GUI class also gives you great controll over how your GUI should behave (because you can also override parent methods etc.).

croc
  • 1,416
  • 1
  • 18
  • 24