0

Hello :) I have a problem in my app, I need to use twice the setContentView which makes my app choose only one, so here is my onCreate:

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    **setContentView(R.layout.activity_main);** (setting the layout)
    highscore_int = (TextView) findViewById(R.id.highscore_int);
    SharedPreferences prefs = this.getSharedPreferences("myPrefsKey",
            Context.MODE_PRIVATE);
    time = prefs.getInt("key", 0);
    highscore_int.setText("Highscore:" + time + "seconds.");
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
    accelerometer = sensorManager
            .getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    lastUpdate = System.currentTimeMillis();
    animatedView = new AnimatedView(this);
    **setContentView(animatedView);** (setting the ball .. 
    }

For the ones who asking why I am setting a ball, my app is a ball based on the motion sensor, here is everything after the onCreate:

    @Override
    protected void onResume() {
        super.onResume();
        sensorManager.registerListener(this, accelerometer,
                SensorManager.SENSOR_DELAY_GAME);
    }

    @Override
    protected void onPause() {
        super.onPause();
        sensorManager.unregisterListener(this);
    }

    @Override
    public void onAccuracyChanged(Sensor arg0, int arg1) {
        // TODO Auto-generated method stub
    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        // TODO Auto-generated method stub
        if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
            sensorY = (int) event.values[1];
            sensorX = (int) event.values[0];
            x -= sensorX * 3;
            y += sensorY * 3;
            if (x <= 0 || x >= screen_width || y <= 0 || y >= screen_height) {
                prefs = this.getSharedPreferences("myPrefsKey",
                        Context.MODE_PRIVATE);
                int oldScore = prefs.getInt("key", 0);
                if (TimeCounter > oldScore) {
                    Editor edit = prefs.edit();
                    edit.putInt("key", TimeCounter);
                    edit.commit();
                }
                finish();
                Intent myIntent = new Intent(this, YouLost.class);
                startActivity(myIntent);
            }
        }
    }

    public class AnimatedView extends ImageView {

        Display display = getWindowManager().getDefaultDisplay();
        Point size = new Point();
        static final int width = 50;
        static final int height = 50;

        @SuppressLint("NewApi")
        public AnimatedView(Context context) {
            super(context);
            // TODO Auto-generated constructor stub
            display.getSize(size);
            screen_width = size.x;
            screen_height = size.y;
            mDrawable = new ShapeDrawable(new OvalShape());
            mDrawable.getPaint().setColor(0xffffAC23);
            mDrawable.setBounds(0, 0, screen_width, screen_height);
        }

        @Override
        protected void onDraw(Canvas canvas) {
            mDrawable.setBounds(x, y, x + width, y + height);
            if (firstDraw) {
                x = screen_width / 2;
                y = screen_height / 2;
                firstDraw = false;
            }
            mDrawable.draw(canvas);
            invalidate();
        }
    }
}

So how can I use my activity_main layout and still have the moving ball?

DavidBalas
  • 333
  • 7
  • 21

1 Answers1

0

Setting a content view replaces any views that already exist. So you can't call it twice and keep both sets. You'll need to redesign things so you have 1 layout with everything.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
  • I just need the ball and a textView, I tried to add a textView programmatically but my app crashes when I do that.. Do you have any solvement? – DavidBalas Jul 08 '14 at 01:07