1

In my app I have a very basic looking compass which is rendered within my activity through a class. I am trying to display the compass with a layout. So rather than having just a circle with a line pointing north, I can include text box and buttons. How do I render this within a layout? Currently my activity sets the content view like so:

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        compassView = new CompassView(this);
        setContentView(compassView);

I have tried setContentView(R.layout.activity_display_compass) which is my xml file however it only display "hello world" (the TextView), not the compass. See my xml file below.

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"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"/>

    <View
        class = "com.example.gpsfinder.CompassView"
        android:id="@+id/compassView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</LinearLayout>

Any guidance would be greatly appreciated.

Nate
  • 31,017
  • 13
  • 83
  • 207
Calgar99
  • 1,670
  • 5
  • 26
  • 42

1 Answers1

0

To use a custom View subclass in your xml layout file:

<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"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"/>

    <com.example.gpsfinder.CompassView
        android:id="@+id/compassView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</LinearLayout>

And then you probably need to add some of these constructors in your Java code:

// you used this ctor when creating the view programmatically
public CompassView(Context context) {
    this(context, null);
    // add additional initialization here
}

// this constructor is needed for the class to be used in XML layout files!
public CompassView(Context context, AttributeSet attributeSet) {
    super(context, attributeSet);
    // add additional initialization here
}

// this constructor is needed for the class to be used in XML layout files,
//  with a class-specific base style
public CompassView(Context context, AttributeSet attributeSet, int defStyle) {
    super(context, attributeSet, defStyle);
    // add additional initialization here
}
Community
  • 1
  • 1
Nate
  • 31,017
  • 13
  • 83
  • 207
  • I spoke too soon, my compassView remained static. ie does not move depending on orientation? – Calgar99 Jun 04 '13 at 21:03
  • That would be a **separate** problem. The code I posted is what's needed to integrate a custom View subclass into an Android XML layout. I'd recommend posting a new question, where you explain that the compass isn't rotating. You'll need to show the full source code for your compass class (or at least most of it, including the part that handles rotation). – Nate Jun 04 '13 at 21:05
  • @Calgar99, I just want to make sure I understand you, though. You're saying that the compass **needle** doesn't rotate as you hold the device flat, and point it North, South, East, West? Or are you saying that the entire `CompassView` does not rotate, for example, when the user turns the phone from portrait to landscape orientation? Those are different problems, so I want to make sure I'm clear what you're describing. Thanks. – Nate Jun 04 '13 at 21:08
  • The needle does not move, which it did before. – Calgar99 Jun 04 '13 at 21:10
  • Ok, that's what I thought you meant at first. As I said, that's unrelated to the problem of just using a custom View in a layout xml file. The only thing I can think of, without seeing the CompassView code, is that I suggested that you add a couple more constructors (which are needed by Android in this situation). Did you remember to initialize your view the same way in all of your constructors? Or chain them together, so that the same initialization code gets run no matter which constructor is used? – Nate Jun 04 '13 at 21:13