Kalina
Kalina

Reputation: 5594

How to use my SurfaceView with main.xml on Android?

In the main class of my project, I have

public class MyClass extends Activity{
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        mySurfaceView = new MySurfaceView(this);
        setContentView(mySurfaceView); 
        ...
    }

    class MySurfaceView extends SurfaceView implements Runnable{
       ...
    }
}

I want to add buttons and wheels to this program, which I have in a separate mini-program right now with a main.xml file. I know that to access that, I have to have the line setContentView(R.layout.main), but then, I lose access to my SurfaceView. How do I make it so that I have both? I saw something online about adding a few lines with a FrameLayout but I can't figure out where, and I've tried sticking it in various places in my main.xml but it just causes my entire program to break..

Currently my main.xml looks like this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
 <Button android:id="@+id/selectButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Select Time Interval"
android:onClick="selfDestruct"
/>
<Button android:id="@+id/finishButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Done"
android:visibility="gone"
android:onClick="selfDestruct"
/>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_gravity="top|left"
    android:paddingLeft="12dp"
    android:paddingRight="12dp"
    >
    <kankan.wheel.widget.WheelView android:id="@+id/startHour"
        android:visibility="gone"
        android:layout_height="wrap_content"
        android:layout_width="43dp"/>
    <kankan.wheel.widget.WheelView android:id="@+id/startMins"
        android:visibility="gone"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"/>
        <kankan.wheel.widget.WheelView android:id="@+id/startAmpm"
        android:visibility="gone"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"/>

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent" >
        <TextView android:id="@+id/theWordTo"
            android:visibility="gone"
            android:layout_width="wrap_content" 
            android:layout_height="fill_parent"
            android:gravity="center"
            android:text="   to   "/>
        </LinearLayout>

        <kankan.wheel.widget.WheelView android:id="@+id/endHour"
        android:visibility="gone"
        android:layout_height="wrap_content"
        android:layout_width="43dp"
        />
    <kankan.wheel.widget.WheelView android:id="@+id/endMins"
        android:visibility="gone"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"/>
        <kankan.wheel.widget.WheelView android:id="@+id/endAmpm"
        android:visibility="gone"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"/>
</LinearLayout>

</LinearLayout>

Upvotes: 4

Views: 8337

Answers (2)

theisenp
theisenp

Reputation: 8719

It's certainly possible to add your SurfaceView to the layout at run time (in the onCreate() method for instance), but it would probably be far simpler to add it in the XML layout.

For simplicity's sake, I would first define MySurfaceView in its own file, instead of in the same file as your MyClass Activity. Make sure you include a constructor of the form MySurfaceView(Context context, AttributeSet attrs) so that the view can be inflated properly. Then simply insert the following into your XML layout wherever you want.

<com.example.package.MySurfaceView
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>

Where the first line is the path to MySurfaceView (com.example.package is the package name, and MySurfaceView is the file in which you have defined the custom view).

Upvotes: 1

nammer
nammer

Reputation: 173

I think you want to use FrameLayout. Change your main.xml to look like this:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<kankan.current.app.MyClass.MySurfaceView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/surfaceView" />

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
 <Button android:id="@+id/selectButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Select Time Interval"
android:onClick="selfDestruct"
/>
<Button android:id="@+id/finishButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Done"
android:visibility="gone"
android:onClick="selfDestruct"
/>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_gravity="top|left"
    android:paddingLeft="12dp"
    android:paddingRight="12dp"
    >
    <kankan.wheel.widget.WheelView android:id="@+id/startHour"
        android:visibility="gone"
        android:layout_height="wrap_content"
        android:layout_width="43dp"/>
    <kankan.wheel.widget.WheelView android:id="@+id/startMins"
        android:visibility="gone"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"/>
        <kankan.wheel.widget.WheelView android:id="@+id/startAmpm"
        android:visibility="gone"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"/>

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent" >
        <TextView android:id="@+id/theWordTo"
            android:visibility="gone"
            android:layout_width="wrap_content" 
            android:layout_height="fill_parent"
            android:gravity="center"
            android:text="   to   "/>
        </LinearLayout>

        <kankan.wheel.widget.WheelView android:id="@+id/endHour"
        android:visibility="gone"
        android:layout_height="wrap_content"
        android:layout_width="43dp"
        />
    <kankan.wheel.widget.WheelView android:id="@+id/endMins"
        android:visibility="gone"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"/>
        <kankan.wheel.widget.WheelView android:id="@+id/endAmpm"
        android:visibility="gone"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"/>
</LinearLayout>

</LinearLayout>

</FrameLayout>

Then in in the activity:

public class MyClass extends Activity{
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main); 
        mySurfaceView = (MySurfaceView) findViewById(R.id.surfaceView);
    }

    class MySurfaceView extends SurfaceView implements Runnable{
       ...
    }
}

Upvotes: 8

Related Questions