Elendas
Elendas

Reputation: 763

setText on EditText is not working properly

I have some problems with putting the right content in a EditText, which is "gyro" in this application. I'm trying to show te values of the gyroscope on my phone, wich I can succesfully print in the console.

The problem starts when I use gyro.setText(string) and it does nothing. The only thing I see 0.0, 0.0, 0.0, while I get the correct numbers in the console. Does something overwrite the current values or am I missing something here?

@Override
    public void onSensorChanged(SensorEvent event) {
        EditText gyro = (EditText)findViewById(R.id.editText1);
        float x = event.values[0];
        float y = event.values[1];
        float z = event.values[2];

        if (!mInitialized) {
            mLastX = x;
            mLastY = y;
            mLastZ = z;
            tvX.setText("0.0");
            tvY.setText("0.0");
            tvZ.setText("0.0");
            gyro.setText("nog niet gestart");
            mInitialized = true;
        } else {
            float deltaX = Math.abs(mLastX - x);
            float deltaY = Math.abs(mLastY - y);
            float deltaZ = Math.abs(mLastZ - z);
            if (deltaX < NOISE) deltaX = (float)0.0;
            if (deltaY < NOISE) deltaY = (float)0.0;
            if (deltaZ < NOISE) deltaZ = (float)0.0;
            mLastX = x;
            mLastY = y;
            mLastZ = z;
            tvX.setText(Float.toString(deltaX));
            tvY.setText(Float.toString(deltaY));
            tvZ.setText(Float.toString(deltaZ));
            iv.setVisibility(View.VISIBLE);
            if (deltaX > deltaY) {
                iv.setImageResource(R.drawable.horizontal);
            } else if (deltaY > deltaX) {
                iv.setImageResource(R.drawable.vertical);
            } else {
                iv.setVisibility(View.INVISIBLE);
            }

            if (timestamp != 0) {
                final float dT = (event.timestamp - timestamp) * NS2S;
                // Axis of the rotation sample, not normalized yet.
                float axisX = event.values[0];
                float axisY = event.values[1];
                float axisZ = event.values[2];

                if (omegaMagnitude > EPSILON) {
                  axisX /= omegaMagnitude;
                  axisY /= omegaMagnitude;
                  axisZ /= omegaMagnitude;
                }

                float thetaOverTwo = omegaMagnitude * dT / 2.0f;
                float sinThetaOverTwo = (float) Math.sin(thetaOverTwo);
                float cosThetaOverTwo = (float) Math.cos(thetaOverTwo);
                deltaRotationVector[0] = sinThetaOverTwo * axisX;
                deltaRotationVector[1] = sinThetaOverTwo * axisY;
                deltaRotationVector[2] = sinThetaOverTwo * axisZ;
                deltaRotationVector[3] = cosThetaOverTwo;

                String latestDeltaRotationVector = Double.toString(deltaRotationVector[0]*0.000277777778)+", "+Double.toString(deltaRotationVector[1]*0.000277777778)+", "+Double.toString(deltaRotationVector[2]*0.000277777778)+", ";
                System.out.println(latestDeltaRotationVector);
                gyro.setText(latestDeltaRotationVector);    

            }
              timestamp = event.timestamp;
              float[] deltaRotationMatrix = new float[9];
              SensorManager.getRotationMatrixFromVector(deltaRotationMatrix, deltaRotationVector);

        }
    }

Upvotes: 0

Views: 242

Answers (3)

Melquiades
Melquiades

Reputation: 8598

The docs say that EditText has a method setText() which takes 2 args:

setText(CharSequence text, TextView.BufferType type)

Sets the text that this TextView is to display (see setText(CharSequence)) and also sets whether it is stored in a styleable/spannable buffer and whether it is editable.

(TextView.BufferType docs here)

So in your code, you're doing the following:

String latestDeltaRotationVector = Double.toString(deltaRotationVector[0]*0.000277777778)+", "+Double.toString(deltaRotationVector[1]*0.000277777778)+", "+Double.toString(deltaRotationVector[2]*0.000277777778)+", ";

System.out.println(latestDeltaRotationVector);

gyro.setText(latestDeltaRotationVector); 

Try setting the text this way:

gyro.setText(latestDeltaRotationVector, TextView.BufferType.EDITABLE); 

or

gyro.setText(latestDeltaRotationVector, TextView.BufferType.NORMAL);

Also, in your onCreate() method, are you setting content view?

setContentView(R.layout.YOUR_LAYOUT_NAME_WITH_editText1);

Upvotes: 1

Hariharan
Hariharan

Reputation: 24853

Try this..

you can do this in two ways

tvX.setText(""+deltaX);
tvY.setText(""+deltaY);
tvZ.setText(""+deltaZ);

or

tvX.setText(String.ValueOf(deltaX));
tvY.setText(String.ValueOf(deltaY));
tvZ.setText(String.ValueOf(deltaZ));

Upvotes: 1

Bhoomika Brahmbhatt
Bhoomika Brahmbhatt

Reputation: 7415

Try to use:

        tvX.setText(String.ValueOf(deltaX));
        tvY.setText(String.ValueOf(deltaY));
        tvZ.setText(String.ValueOf(deltaZ));

Upvotes: 1

Related Questions