0

I am trying to print the rectangle shape and the main xml aswell as I want the shape and the figures, is this possible? If not how can I print a shape from java code and my main xml both on to my emulator? Thank you

package com.example.accel;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.RectShape;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class AccelActivity extends Activity implements SensorEventListener
{
    /** Called when the activity is first created. */
    CustomDrawableView mCustomDrawableView = null;
    ShapeDrawable mDrawable = new ShapeDrawable();
    public static int x;
    public static int y;
    public static int z;
    private SensorManager sensorManager = null;
    TextView x1, y1, z1;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {

        super.onCreate(savedInstanceState);
        // Get a reference to a SensorManager
        sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        mCustomDrawableView = new CustomDrawableView(this);
        setContentView(mCustomDrawableView);
        setContentView(R.layout.main);

    }

    // This method will update the UI on new sensor events
    public void onSensorChanged(SensorEvent sensorEvent)
    {
        {
            if (sensorEvent.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {

                TextView tvX= (TextView)findViewById(R.id.x_axis);
                TextView tvY= (TextView)findViewById(R.id.y_axis);
                TextView tvZ= (TextView)findViewById(R.id.z_axis);

                x = (int) Math.pow(sensorEvent.values[0], 2); 
                y = (int) Math.pow(sensorEvent.values[1], 2);
                z = (int) Math.pow(sensorEvent.values[2], 2);

            }

         //   if (sensorEvent.sensor.getType() == Sensor.TYPE_ORIENTATION) {

          //  }
        }
    }

    // I've chosen to not implement this method
    public void onAccuracyChanged(Sensor arg0, int arg1)
    {
        // TODO Auto-generated method stub

    }

    @Override
    protected void onResume()
    {
        super.onResume();
        // Register this class as a listener for the accelerometer sensor
        sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
                SensorManager.SENSOR_DELAY_FASTEST);
        // ...and the orientation sensor

    }

    @Override
    protected void onStop()
    {
        // Unregister the listener
        sensorManager.unregisterListener(this);
        super.onStop();
    }

    public class CustomDrawableView extends View
    {
        static final int width = 150;
        static final int height = 250;

        public CustomDrawableView(Context context)
        {
            super(context);

            mDrawable = new ShapeDrawable(new RectShape());
            mDrawable.getPaint().setColor(0xff74AC23);
            mDrawable.setBounds(x, y, x + width, y + height);

        }

        protected void onDraw(Canvas canvas)
        {

            RectF rect = new RectF(AccelActivity.x, AccelActivity.y, AccelActivity.x + width, AccelActivity.y
                    + height); // set bounds of rectangle
            Paint p = new Paint(); // set some paint options
            p.setColor(Color.BLUE);
            canvas.drawRect(rect, p);
            invalidate(); 
        }
    }
}
skaffman
  • 398,947
  • 96
  • 818
  • 769
user1169775
  • 43
  • 2
  • 8
  • Could you explain why you want to do that? A content view is supposed to be the delegate view to you controller. You can't have two. One has to be in the other one. – Romain Piel Feb 08 '12 at 13:28
  • Maybe you want to inflate another xml layouts inside your main layout? – Jordi Coscolla Feb 08 '12 at 13:30
  • Because I want to the accelerometer values which is printed through the android main xml as well as the shape I have defined in the java code, I am not sure how to show both – user1169775 Feb 08 '12 at 13:32
  • Well your main.xml can contain multiple layouts. You can access each from your java code and do what you want with each one separately. – Romain Piel Feb 08 '12 at 13:34
  • But i am trying to print the shape shown above and the xml together, I don't understand how to print both if there is only one setContentView – user1169775 Feb 08 '12 at 15:07

1 Answers1

0

yes, you can.

setcontentview(anotherLayout), so you pring the new layout in your activity.

or you can define other elements in your mainLayout and you manipulate it by the function setVisibily

if you need elementA:

elementA = findViewById(R.id.elementA);
elementA.setVisibility(true);

and if you don't need it:

elementA.setVisibility(false);
musefan
  • 47,875
  • 21
  • 135
  • 185
haythem souissi
  • 3,263
  • 7
  • 50
  • 77
  • Thanks but I am only trying to print one thing from main xml and the shape which is created in the java code, so they are separate, so i am trying to print both in the emulator – user1169775 Feb 08 '12 at 15:04