Here is my code for a add and subtraction program but the question is what is the best way to incoporate vibrations on the phone so for example the text layout shows -1 for a number.. So the phone then vibrates a certain way for -1.. I know I need to do a if statement, but I seem to be having trouble.. Here is the java code for the program.. And thank you for the help!
package com.example.untitled3;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MyActivity extends Activity {
int counter;
Button add , sub;
TextView display;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
counter = 0;
add = (Button) findViewById(R.id.bAdd);
sub = (Button) findViewById(R.id.bSub);
display = (TextView) findViewById(R.id.tvDisplay);
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Adds 1 to the counter
counter = counter + 1;
display.setText(" Your total is :" + counter);
}
});
sub.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Subtract 1 from counter
counter = counter - 1;
display.setText(" Your total is :" + counter);
}
});
}
}
You can make your phone vibrate using the following code:
On some condition(In your case, when the text shows -1) call this:
And don’t forget you need to add the permission to the manifest (after the
</application>tag):EDIT:
Create an integer variable initialized to zero. Increment the variable on clicking the add button and decrement it on clicking the subtract button. Every time after the click check the value of the count variable, if it is zero then call the
vibe.vibrate(200);.