0

My post is based on a previous post and greatly simplified.

(Android: Two Views On Top of Each Other Using XML)

The file/object DrawV populates the screen with pink circles and allows one to touch a circle to make it disappear. In another file, private DrawV drawView = new DrawV(this); This populates the screen but does not participate in the layout.

setContentView(drawView) shows the dots,so I know it works. I want to use a layout named setContentView(R.layout.activity_title); which includes two buttons at the top of the screen and dots below. In other words, I was wondering if there is a method to put the dots shown in some sort of View that can be included with buttons in the same layout.

Any help? Please?

Tell me if you need anything.

1 Answer 1

0

If DrawV is an Android View (or extends View), you can include it in a regular xml layout file, and then use that layout file with setContentView(int).

To reference the DrawV class in your layout, you'll need to use the fully-qualified name (with the package).

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

  <LinearLayout
    android:id="@+id/buttons"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <Button
      android:id="@+id/button_one"
      android:text="One"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1.0" />

    <Button
      android:id="@+id/button_two"
      android:text="Two"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1.0" />
  </LinearLayout>

  <com.example.views.DrawV
    android:layout_below="@id/buttons"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

</RelativeLayout>

Above, the RelativeLayout is your root view. The LinearLayout, buttons, is a ViewGroup just to hold the two buttons and keep them of equal width (note the layout_width=0dp and equal layout_weight). Your DrawV view will be laid out below the buttons View, and then will match the parent container's width and height (fill it).

If you save this under src/main/res/layout/activity_circles.xml, you'll be able to use setContentView(R.layout.activity_circles) in your Activity to set the layout.

4
  • I am in favor of your methods, only they're not working: <com.category.tap.DrawV android:layout_below="@id/buttons" android:layout_width="match_parent" android:layout_height="match_parent" /> gives me the error "The application TAP(process com.category.tap) has stopped unexpectedly. Please try again."
    – user963070
    Jul 13, 2014 at 23:46
  • @user963070 that's the error which shows to the user on the device. Edit your question and put the stack trace that's visible in LogCat.
    – ataulm
    Jul 19, 2014 at 23:50
  • Hey, added logcat. Not sure if it's a logcat for a slightly different version of a layout, but there it is.
    – user963070
    Aug 1, 2014 at 0:13
  • where did you add the logcat? You should add the logcat that shows the error when you get a crash.
    – ataulm
    Aug 1, 2014 at 9:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.