Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8639937
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T11:09:02+00:00 2026-06-12T11:09:02+00:00

Hi I have write this code to move a simple ball with an ImageView

  • 0

Hi I have write this code to move a simple ball with an ImageView and accelerometer but don’t work.
I have used ball.scrollTo(xa, ya) but this code doesn’t work with a variable catching accelerometer

post my code:

Main.java

package com.crowley.test;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.widget.ImageView;
import android.widget.TextView;
import android.util.Log;


public class Main extends Activity implements SensorEventListener {
final String tag = "AccLogger";
SensorManager sensore=null;
TextView x=null;
TextView y=null;
TextView z=null;
TextView xat=null;
TextView yat=null;
String a;
String b;
private ImageView ball;

int xa=0;
int ya=0;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    sensore = (SensorManager) getSystemService(SENSOR_SERVICE);
    x= (TextView) findViewById(R.id.x);
    y= (TextView) findViewById(R.id.y);
    z= (TextView) findViewById(R.id.z);
    xat= (TextView) findViewById(R.id.textView1);
    yat= (TextView) findViewById(R.id.textView2);

    ball = (ImageView) findViewById(R.id.ball);
    ball.scrollTo(xa, ya);


    ball.setImageResource(R.drawable.ball);


 }


  public void onSensorChanged(SensorEvent event){
     Sensor sensor = event.sensor;
     float [] values = event.values;
     synchronized (this) {
             Log.d(tag, "onSensorChanged: " + sensor + ", x: " + 
                             values[0] + ", y: " + values[1] + ", z: " + values[2]);
     if (sensor.getType() == Sensor.TYPE_ACCELEROMETER ) {
         x.setText("x"+ values[0]);
         y.setText("y"+ values[1]);
         z.setText("z"+ values[2]);

         xa=(int)values[0];// this part of code is only test to see int x and y on Activity
         ya=(int)values[1];

         a=Integer.toString(xa);
         b=Integer.toString(ya);

         xat.setText(a);
         yat.setText(b);

         //x.setText("x"+ SensorManager.DATA_X);
         //y.setText("y"+ SensorManager.DATA_Y);
         //z.setText("z"+ SensorManager.DATA_Z);
     }
     }
  }

 public void onAccuracyChanged(Sensor sensor, int accuracy) {
    Log.d(tag,"onAccuracyChanged: " + sensor + ", accuracy: " + accuracy);
 }

  protected void onResume() {
    super.onResume();
    Sensor Accel = sensore.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
   // register this class as a listener for the orientation and accelerometer sensors
    sensore.registerListener((SensorEventListener) this, Accel,        SensorManager.SENSOR_DELAY_FASTEST);
  }

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
}

and my main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<TextView
    android:id="@+id/x"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginTop="16dp"
    android:text="" />

<TextView
    android:id="@+id/y"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/x"
    android:text="" />

<TextView
    android:id="@+id/z"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/y"
    android:text="" />

<ImageView
    android:id="@+id/ball"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ball" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_marginBottom="71dp"
    android:layout_marginRight="66dp"
    android:text="TextView" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignRight="@+id/textView1"
    android:layout_alignTop="@+id/textView1"
    android:layout_marginTop="35dp"
    android:text="TextView" />

    </RelativeLayout>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T11:09:04+00:00Added an answer on June 12, 2026 at 11:09 am

    You don’t change the ball’s position when the sensor changes.

    I haven’t tested it but you should do something like that:

    at the end of onSensorChanged() add ball.scrollBy(xa, ya); or something similar.

    You make a call to ball.scrollTo(x,y); in your onCreate(), but once your sensor starts reporting, you don’t move the ball at all.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have write this code but it does work only if I am already
I have this code to write to a file, it works perfect but I
I have this code to update users data, but I can't write the for
I have this code: $coder = JSON::XS->new->utf8->pretty->allow_nonref; %perl = $coder->decode ($json); When I write
I have problem with this code: file = tempfile.TemporaryFile(mode='wrb') file.write(base64.b64decode(data)) file.flush() os.fsync(file) # file.seek(0)
I've wrote this simple piece of code. And I have a slight problem with
I have to fix a glitch with this code someone wrote but I'm not
I have a short question i have wrote this in java. Old code: class
hi all i have jqgrid and set update inline . i wrote this code
Okay, so i have this code i wrote: class Connection { public static StreamWriter

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.