4

My view is written as follow:

package com.mycompany;

import android.view.View;
import java.util.concurrent.TimeUnit;
import java.util.ArrayList;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.util.AttributeSet;
import android.graphics.Paint;
import android.graphics.Point;

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.widget.*;



public class GameEngineView extends View implements SensorEventListener {
    GameLoop gameloop;
    String txt_acc;
    float accY;
    ArrayList<Point> bugPath;

    private SensorManager sensorManager;

    private class GameLoop extends Thread {
        private volatile boolean running = true;

        public void run() {
            while (running) {
                try {
                    TimeUnit.MILLISECONDS.sleep(1);
                    postInvalidate();
                    pause();

                } catch (InterruptedException ex) {
                    running = false;
                }
            }
        }

        public void pause() {
            running = false;
        }

        public void start() {
            running = true;
            run();
        }

        public void safeStop() {
            running = false;
            interrupt();
        }

    }

    public void unload() {
        gameloop.safeStop();

    }

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

    }

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

    public GameEngineView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
        init(context);
    }

    private void init(Context context) {
        txt_acc = "";

        // Adding SENSOR
        sensorManager=(SensorManager)context.getSystemService(Context.SENSOR_SERVICE);
        // add listener. The listener will be HelloAndroid (this) class
        sensorManager.registerListener(this,
                sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
                SensorManager.SENSOR_DELAY_NORMAL);

        // Adding UI Elements : How ?
        Button btn_camera = new Button(context);
        btn_camera.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,
                LinearLayout.LayoutParams.FILL_PARENT));
        btn_camera.setClickable(true);
        btn_camera.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                System.out.println("clicked the camera.");
            }
        });


        gameloop = new GameLoop();
        gameloop.run();

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // TODO Auto-generated method stub
        //super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        System.out.println("Width " + widthMeasureSpec);
        setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        // super.onDraw(canvas);

        Paint p = new Paint();
        p.setColor(Color.WHITE);
        p.setStyle(Paint.Style.FILL);
        p.setAntiAlias(true);
        p.setTextSize(30);
        canvas.drawText("|[ " + txt_acc + " ]|", 50, 500, p);

        gameloop.start();

    }
    public void onAccuracyChanged(Sensor sensor,int accuracy){

    }
    public void onSensorChanged(SensorEvent event){
        if(event.sensor.getType()==Sensor.TYPE_ACCELEROMETER){
            //float x=event.values[0];
            accY =event.values[1];
            //float z=event.values[2];

            txt_acc = "" + accY;
        }
    }
}

I would like to add a Button to the scene, but I don't know how to. Can anybody give me some lights?

UPDATE: Here is my Activity :

public class MyActivity extends Activity {
    private GameEngineView gameEngine;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // add Game Engine
        gameEngine = new GameEngineView(this);

        setContentView(gameEngine);
        gameEngine.requestFocus();
    }
}

And I am following the this tutorial .

1 Answer 1

1
    detailListView = (LinearLayout) findViewById(R.id.DetailsLinearLayout); // Find the layout where you want to add button
    Button button = new Button(this);
 button.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT
                    ,ViewGroup.LayoutParams.WRAP_CONTENT));
    detailListView.addView(button);//add view to add
Sign up to request clarification or add additional context in comments.

3 Comments

You should also provide layout_width nad layout_height.
but I did not use a layout.xml, thus I do not have a LinearLayout. See my activity file as well. updated.
GameEngineView extends the view see if the addView is available for it

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.