Android: Programatically Add UI Elements to a View
- by Shivan Raptor
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();
}
}