ayala
ayala

Reputation: 37

Android development - Implementing onClickListener in my MainActivity

This might be more of a question about class structure but I am trying to add a simple on click listener to the following piece of code. Once clicked it will change the "SENSOR_DELAY_FASTEST" value to "SENSOR_DELAY_NORMAL". So I can toggle on the fly. But as I am in MainActivity i cannot implement onClickListener. Could anyone give me any pointers as to how I could handle this?

As I said. It might just be my lack of Java knowledge hindering me here. Perhaps it is better to setup another class to handle the clicks?

public class MainActivity extends AppCompatActivity implements SensorEventListener {

private TextView xText, yText, zText;
private Sensor accelerometerSensor;
private SensorManager sensorManager;

@Override
public void onSensorChanged(SensorEvent event) {

    // display the sensor values in the x,y and z text views
    xText.setText("X: " + event.values[0]);
    yText.setText("Y: " + event.values[1]);
    zText.setText("Z: " + event.values[2]);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // create sensor manager
    sensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);

    // create accelerometer sensor
    accelerometerSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

    // register listener
    sensorManager.registerListener(this, accelerometerSensor, SensorManager.SENSOR_DELAY_FASTEST);

    // assign text views
    xText = (TextView)findViewById(R.id.xText);
    yText = (TextView)findViewById(R.id.yText);
    zText = (TextView)findViewById(R.id.zText);
}
}

Upvotes: 0

Views: 2239

Answers (3)

reflective_mind
reflective_mind

Reputation: 1515

Option #1:

Of course you can do it in your MainActivity. All you have to do is find the UI element that you want to listen to a click event. For example if your UI element is a simple Button, you can do the following:

// find button with id "myButtonId" in the XML layout
Button b = (Button) findViewById(R.id.myButtonId);     

// set a listener on that button
b.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) { // gets called when clicked
        // button has been clicked,
        // your code here
    }
});

Option #2:

But you can also define in your XML layout, that your method onClick_Button() shall be called after the button click.

Inside your XML layout:

<Button
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:id="@+id/myButtonId"
    android:text="Click Me"
    android:onClick="onClick_Button">
</Button>

Inside your MainActivity:

public void onClick_Button(View v) {
        // your code here
}

Upvotes: 2

Prince Bansal
Prince Bansal

Reputation: 1655

In Java, a single class can be derived only from one class but can have multiple interfaces.

As per as I can interpret, you are facing problem in implementing OnclickListener interface as you think that you have already implemented SensorEventListener.

You can easily implement multiple interfaces by seperating with comma. For example:

public class MainActivity extends AppCompatActivity implements     SensorEventListener, View.OnClickListener {

private Button button;
private TextView xText, yText, zText;
private Sensor accelerometerSensor;
private SensorManager sensorManager;

@Override
public void onSensorChanged(SensorEvent event) {

// display the sensor values in the x,y and z text views
xText.setText("X: " + event.values[0]);
yText.setText("Y: " + event.values[1]);
zText.setText("Z: " + event.values[2]);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// create sensor manager
sensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);

// create accelerometer sensor
accelerometerSensor =    sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

// register listener
sensorManager.registerListener(this, accelerometerSensor,   SensorManager.SENSOR_DELAY_FASTEST);

// assign text views
xText = (TextView)findViewById(R.id.xText);
yText = (TextView)findViewById(R.id.yText);
zText = (TextView)findViewById(R.id.zText);
//Declare button in activity_main.xml
button=(Button)findViewById(R.id.button);
button.setOnClickListener(this);
}
@Override
public void onClick(View v){
   changeSensorDelay();
}
public void changeSensorDelay(){

 //Your own implementation
}
}

Upvotes: 1

Pavul Zavala
Pavul Zavala

Reputation: 425

sometimes when android studio does not found some classes its due problems in the installation or paths where resources are.

if you are sure its well installed, try to implement the interface directly anonymoue for example:

...setOnclickListener( new ClickListener( View v) { ...//more code here }

)

butif you are implementing the interface in your activity must not compile, because you have to implement the methods of that interface, check that if don't work, please give me more information

Upvotes: 0

Related Questions