tags:

views:

49

answers:

2

I have a ViewFlipper defined that contains 3 views...

<?xml version="1.0" encoding="utf-8"?>
<ViewFlipper xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/flipper" android:layout_width="fill_parent"
android:layout_height="fill_parent">
  <include android:id="@+id/first" layout="@layout/first_view" />
  <include android:id="@+id/second" layout="@layout/second_view" />
  <include android:id="@+id/third" layout="@layout/third_view" />
</ViewFlipper>

I also have a custom View defined within my Activity...

private class CompassView extends View { 
  @Override 
  protected void onDraw(Canvas canvas) {
  ...
  }
}

Some how, I need these linked together so that the 'third_view' defined in the XML layout file needs to be a CompassView, or have a CompassView added to it.

What I can do is drop the 'third_view' from the layout and then add in the CompassView manually..

viewFlipper = (ViewFlipper)findViewById(R.id.flipper);
viewFlipper.addView(new CompassView(this));

But then I lose the ability to define other view controls within the layout file.

Can I add CompassView to 'third_view' declaratively?

+1  A: 

If the question is how to add a custom view in an Android XML layout file, try this:

<com.example.CustomView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
</com.example.CustomView>

The open and close tags should just be the fully qualified class of your custom view.

danh32
A: 

Thanks, that works but in my case the view class is a sub class of my main Activity - when I do this the app crashes in OnCreate/SetContentView.

I guess the next question is, are views defined within the Activity supported declaratively?

MarkB