0

I am trying to add a custom surfaceView class to a surfaceView that has been declared in a xml layout. The problem is that I cannot get the custom class add the the xml defined surfaceView. Some how it just does not start.

This is my code:
MainActivity

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    sineWaveSurfaceView = (SineWaveSurfaceView) findViewById(R.id.surfaceView);
}

MainActivity Layout

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

    <mark.dijkema.android.eindopdracht.SineWaveSurfaceView
        android:id="@+id/surfaceView"
        android:layout_width="fill_parent"
        android:layout_height="100dp"
        android:layout_marginTop="20dp" />

    <ListView
        android:id="@+id/song_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp" >
    </ListView>

</RelativeLayout>

SineWaveSurfaceView Class

public class SineWaveSurfaceView extends SurfaceView implements SurfaceHolder.Callback
{
private Context context;

public SineWaveSurfaceView(Context context)
{
    super(context);
    // TODO Auto-generated constructor stub

    this.context = context;
    getHolder().addCallback(this);

    Log.e("constructor", "done");
}

public SineWaveSurfaceView(Context context, AttributeSet attrs)
{
    super(context, attrs);
    // TODO Auto-generated constructor stub
}

public SineWaveSurfaceView(Context context, AttributeSet attrs, int defStyle)
{
    super(context, attrs, defStyle);
    // TODO Auto-generated constructor stub
}

protected void OnDraw(Canvas canvas, int value)
{
    //canvas.drawColor(Color.WHITE);

    Paint paint = new Paint();
    paint.setColor(Color.WHITE);

    float y = Float.valueOf(String.valueOf(value)); 

    for(float x = 0; x < 200; x += 0.1f)
    {
        canvas.drawPoint(x, y, paint);
    }
}

@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3)
{
    // TODO Auto-generated method stub

}

@Override
public void surfaceCreated(SurfaceHolder arg0)
{
    // TODO Auto-generated method stub

    Log.e("start", "started");
}

@Override
public void surfaceDestroyed(SurfaceHolder arg0)
{
    // TODO Auto-generated method stub

}

}

The problem is that the Log.e("constructor", "done"); in the constructor of the SineWaveSurfaceView is not even showing up.

What am I doing wrong, because when I do something like

sineWaveSurfaceView = new sineWaveSurfaceView(this);

then it works, but it is not attached to the surfaceView defined in the xml layout.

DijkeMark
  • 1,284
  • 5
  • 19
  • 41
  • 1
    When inflating from xml, it is the constructor with arguments `Context` and `AttributeSet` that gets called: http://stackoverflow.com/questions/8113621/which-constructor-to-be-called-for-view . Maybe you want to add your Log.e there too! – verybadalloc Jun 04 '13 at 10:18
  • 1
    Maybe you can create an answer so that we can mark this as resolved? – verybadalloc Jun 04 '13 at 10:55

1 Answers1

4

As veryBadalloc explained in the comments, it appeared that inflating from xml starts the second constructor, thus my code was working after all.

DijkeMark
  • 1,284
  • 5
  • 19
  • 41