Question

I got an custom class. Which works great.

public class FocusGameView extends SurfaceView implements Runnable

At the activity itself I want to put the 'FocusGameView' on a view that I already created on the xml file.

so I tried to use the 'inflate' like this:

public class FocusGame extends Activity {

    FocusGameView fgv;
    View v;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        fgv= new FocusGameView(this);

        v=(View) findViewById(R.id.frame_focus_game);

        LayoutInflater mInflater;
        mInflater = LayoutInflater.from(fgv.getContext());

        v = mInflater.inflate(R.layout.activity_focus_game, null);


        setContentView(R.layout.activity_focus_game);
}

The result of this code is opening the activity and set the layout. without put the custom view on the view itself.

I really hope you could help me with that.

Thanks in advance;

Yaniv.

Was it helpful?

Solution

You can only add a view to a view group, not a view, so lets image your View v is a RelativeLayout:

public class FocusGame extends Activity {

FocusGameView fgv;
RelativeLayout v;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_focus_game);

    v=(View)findViewById(R.id.frame_focus_game);

    fgv= new FocusGameView(this);
    v.addView(fgv); //You only need to add the view to a parent to make it appear
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top