using onLongClickListener to change values quickly - java

I'm trying to create a set of buttons that will change a value up or down by 1 with a single tap, and change the value quickly by holding the button. I can't figure out how to get the value to change quickly. Here's what I have:
btPlus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mTempo=mTempo+1;
setTempo(mTempo + 1);
tvTempo.setText(Integer.toString(mTempo));
}
});
btPlus.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public void onLongClick(View view) {
mTempo = mTempo + 1;
setTempo(mTempo + 1);
tvTempo.setText(Integer.toString(mTempo));
}
});
btMinus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mTempo=mTempo-1;
setTempo(mTempo - 1);
tvTempo.setText(Integer.toString(mTempo));
}
});
btMinus.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public void onLongClick(View view) {
mTempo = mTempo - 1;
setTempo(mTempo - 1);
tvTempo.setText(Integer.toString(mTempo));
}
});
return rootView;
}
Thanks for your help.

Holding the button is something else than long clicking it. You need an onTouchListener
In the onPressed method check if the MotionEvent is UP or DOWN and then start/stop counting.
EDIT: Try Counting up in a Thread, something like that:
Final Thread t = new Thread(new Runnable(){
#Override public void run(){
while(!t.isInterrupted(){
setTempo(mTempo++);
}
}
switch (event){
case ACTION_DOWN:
t.start();
break;
case ACTION_UP:
t.interrupt();
break
}

Related

How to Disable Button after first Click in android

I am using this code but its not working i am still able to click more than 2 times.
accept.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
accept.setEnabled(false);
if (dialog1 != null && dialog1.isShowing()) {
dialog1.dismiss();
}
handler.removeCallbacks(runnable);
}
});
its not showing any error but not working as desired.
boolean run = true;
accept.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(run) {
run = false;
//your code
}
}
});

How to restrict increment button if stock is 10?

I am new to Android. I'm trying to restrict increments if the value of stock is 10. The count value is set in textcount which is a TextView. plus and minus are button for increment and decrement.
stockshow is textview and ITEM_QTY i added in getter setter method
plus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
addtocart.setVisibility(View.VISIBLE);
count++;
textcount.setText(String.valueOf(count));
}
});
minus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
count--;
textcount.setText(String.valueOf(count));
}
});
stockshow.setText(country.getITEM_QTY());
You'll want to make use of the setEnabled() method of View to ensure that the plus button can no longer be clicked should the stock count be set to 10. Likewise, you'll also want to disable the minus button once the stock count reaches 0. You could create a method like this:
private void checkBounds() {
plus.setEnabled(count < 10);
minus.setEnabled(count > 0);
}
And then use it in your listeners like so:
plus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
addtocart.setVisibility(View.VISIBLE);
count++;
textcount.setText(String.valueOf(count));
checkBounds();
}
});
minus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
count--;
textcount.setText(String.valueOf(count));
checkBounds();
}
});
You have to add an if condition in your increment method
plus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(count<10){
addtocart.setVisibility(View.VISIBLE);
count++;
textcount.setText(String.valueOf(count));
}
//You can add an else if you want to do something else{}
}
});

Value not getting updated when making a calculator

I making a simple app in android studio. I have used a Single textview to take in both numbers. I also have buttons made, for the 10 digits, 4 operations and decimal point, and equals.
My app does not read the second number. When I press '=' button, it just displays the first number.
For example, when I put in a number 25, and I press the '+' button, the number gets stored in a float variable res, and the textview is cleared. Now if I put in a second number 11 and I press '=', the output is 25.0
Below, is my code for when I press the + button.
sum.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(result.getText()==null)
{
result.setText("");
}
else
{
num1 = Float.parseFloat(result.getText().toString());
res=res+num1;
result.setText(null);
num1=0;
}
}
});
and below is my code when I press =
equal.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
result.setText(Float.toString(res));
res=0;
}
});
num1 and res, both are float, both initialized to 0.
Edit:- adding the full java code.
public class Calculator extends AppCompatActivity {
TextView result;
Button one,two,three,four,five,six,seven,eight,nine,zero,sum,sub,mul,div,decimal,equal;
float num1,num2,res=0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calculator);
result = findViewById(R.id.result);
one = findViewById(R.id.button_one);
two = findViewById(R.id.button_two);
three = findViewById(R.id.button_three);
four = findViewById(R.id.button_four);
five = findViewById(R.id.button_five);
six = findViewById(R.id.button_six);
seven = findViewById(R.id.button_seven);
eight = findViewById(R.id.button_eight);
nine = findViewById(R.id.button_nine);
zero = findViewById(R.id.button_zero);
sum = findViewById(R.id.button_add);
sub = findViewById(R.id.button_sub);
mul = findViewById(R.id.button_mul);
div = findViewById(R.id.button_div);
decimal = findViewById(R.id.button_decimal);
equal = findViewById(R.id.button_equal);
one.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
result.setText(result.getText() + "1");
}
});
two.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
result.setText(result.getText() + "2");
}
});
three.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
result.setText(result.getText() + "3");
}
});
four.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
result.setText(result.getText() + "4");
}
});
five.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
result.setText(result.getText() + "5");
}
});
six.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
result.setText(result.getText() + "6");
}
});
seven.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
result.setText(result.getText() + "7");
}
});
eight.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
result.setText(result.getText() + "8");
}
});
nine.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
result.setText(result.getText() + "9");
}
});
zero.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
result.setText(result.getText() + "0");
}
});
decimal.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
result.setText(result.getText() + ".");
}
});
sum.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(result.getText()==null)
{
result.setText("");
}
else
{
num1 = Float.parseFloat(result.getText().toString());
res=res+num1;
result.setText(null);
num1=0;
}
}
});
sub.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(result.getText()==null)
{
result.setText("");
}
else
{
num1 = Float.parseFloat(result.getText() + "");
res=res-num1;
result.setText(null);
}
}
});
mul.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(result.getText()==null)
{
result.setText("");
}
else
{
num1 = Float.parseFloat(result.getText() + "");
res=res/num1;
result.setText(null);
}
}
});
div.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(result.getText()==null)
{
result.setText("");
}
else
{
num1 = Float.parseFloat(result.getText() + "");
res=res/num1;
result.setText(null);
}
}
});
equal.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
result.setText(Float.toString(res));
res=0;
}
});
}
}
Your should also take the second value from the user and make the calculations in every operator function respectively.
There is logical error in your all the operators functions.
Your should add this line in all the operator whenever the specific function in called.
result.setText(num1 - num2 + "");
result.setText(num1 + num2 + "");
and so on!!!
Your are using a wrong technique by making anonymous function for the operation. Please you switch statement of nested if-else.
You are not reading in the value for the second number (right of the operator). Your OnClickListener for the = button does nothing but display res. It doesn't apply the right hand of the operator i.e. the second number
E.g. 1 + 2 = 3
Step 1 press 1
Step 2 press +, the program will get the left of the operator and load it to num1 and add it to res.
Step 3 press 2
Step 4 press =, the program will display res
If you want to see what your program is doing in real time then try using the "Debug" tool where you will be able to step through the program line by line and see what variables are being set to what extra.
use switch case for operators,
let say following are fields;
public static final int plus = 1
public static final int minus= 2
public static final int divide= 3
public static final int multiply= 4
and a variable that hold the current operation,
int currentOperation;
and when some operator button clicked then assign "currentOperation" to that operation
in listener
sum.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(result.getText()==null)
{
result.setText("");
}
else
{
num1 = Float.parseFloat(result.getText().toString());
res=res+num1;
result.setText(null);
num1=0;
currentOperation = plus //plus is public static final int plus = 1
}
}
});
and update '=' lisetener with switch statement,
equal.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
num1 = Float.parseFloat(result.getText().toString());
switch(currentOperation){
case plus:
res=res+num1;
break;
case minus:
res=res-num1;
break;
case divide:
res=res/num1;
break;
case multiply:
res=res*num1;
break;
}
result.setText(""+ res);
num1=0;
}
});

Android using two countdowntimers

In my app, i'm using two different CountDownTimers that have same values. I have two buttons to control them but when i press the button twice, it starting from the beginning. I want to keep its last value.
Here is my code:
t1 = new CountDownTimer(white, 1000) {
#Override
public void onTick(long l) {
btnWhite.setText("seconds remaining: " + l / 1000);
white = l;
}
#Override
public void onFinish() {
}
};
t2 = new CountDownTimer(black, 1000) {
#Override
public void onTick(long l) {
btnBlack.setText("seconds remaining: " + l / 1000);
black = l;
}
#Override
public void onFinish() {
}
};
btnBlack.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
t1.start();
t2.cancel();
}
});
btnWhite.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
t2.start();
t1.cancel();
}
});
I have tested this and it works!
I have two TextViews and two Buttons. The black button is next to the black text view and the white button is next to the white text view.
First I declare the important constants.
//contains the elapsed time for each of the timers
long blackElapsed=0,whiteElapsed=0;
//contains the total time with which we start new timers
long totalWhite = 30000;
long totalBlack = 30000;
Next I initialise the CountDownTimers. Whatever you put in here doesn't matter. I only have this so that the timers will be initialised with some value.
The reason is that they have to be initialised in order to be able to .cancel() them later in the OnClickListeners.
black = new CountDownTimer(totalWhite, 1000){
#Override
public void onTick(long l) {
}
#Override
public void onFinish() {
}
};
white = new CountDownTimer(totalBlack, 1000){
#Override
public void onTick(long l) {
}
#Override
public void onFinish() {
}
};
Finally the OnClickListeners for the buttons. (W is white textView and B is black textView and b is black button and w is white button)
w.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
black.cancel();
//using the elapsed time to start a new timer
totalBlack = totalBlack - blackElapsed;
//this preserves milliseconds by ticking every millisecond
white = new CountDownTimer(totalBlack, 1){
#Override
public void onTick(long l) {
B.setText(l+"");
blackElapsed=totalBlack-l; //updating the elapsed time
}
#Override
public void onFinish() {
}
}.start();
}
});
//we do a similar thing with the other player's button
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
white.cancel();
totalWhite = totalWhite - whiteElapsed;
black = new CountDownTimer(totalWhite, 1){
#Override
public void onTick(long l) {
W.setText(l+"");
whiteElapsed=totalWhite-l;
}
#Override
public void onFinish() {
}
}.start();
}
});
I have checked your code.
It is obvious because your timers initialised with default values. when you start again it won't take new values of white/black.
To achieve what you want you have to initialise timer with new values before starting it.
I have done some correction in your code. you can check that out.
Make Two methods
public void timerStart1(long timeLengthMilli) {
t1 = new CountDownTimer(timeLengthMilli, 1000) {
#Override
public void onTick(long l) {
isRunning1 = true;
tv1.setText("seconds remaining: " + l / 1000);
white = l;
}
#Override
public void onFinish() {
isRunning1 = false;
}
}.start();
}
public void timerStart2(long timeLengthMilli) {
t2 = new CountDownTimer(timeLengthMilli, 1000) {
#Override
public void onTick(long l) {
isRunning2 = true;
tv2.setText("seconds remaining: " + l / 1000);
black = l;
}
#Override
public void onFinish() {
isRunning2 = false;
}
}.start();
}
and set setOnClickListener like this
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (!isRunning1) {
isRunning2 = false;
timerStart1(white);
if (t2 != null)
t2.cancel();
}
}
});
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (!isRunning2) {
isRunning1 = false;
timerStart2(black);
if (t1 != null)
t1.cancel();
}
}
});
UPDATE :
Please check updated code and take these extra variables
boolean isRunning1 = false, isRunning2 = false;
Hope this will help you.
Happy Coding.

Checking integer against array index not working

I'm making an android app where you tap a button to stop a looping counter when it is showing a particular number. I have an array (aNums) holding some int values and want the counter to stop when it is clicked when showing the first number in the array (in this case "2"). For some reason however it decides to stop on "1" when clicked. I'm not sure if my code is just wrong or if there's a timing issue and it's stopping right when it's about to change to the number "2". Here is my code so far:
public class MainActivity extends Activity {
public int a = 0;
private Handler handler = new Handler();
TextView textView2;
Button Stop;
public Runnable myRunnable = new Runnable() {
#Override
public void run() {
updateText();
a = ++a % 10;
if (a < 10) {
handler.postDelayed(this, 1000);
}
}
};
public int aNums[] = { 2, 4, 6, 8, 10, 12 };
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView2 = (TextView) findViewById(R.id.textView2);
loop();
Stop = (Button)findViewById(R.id.Stop);
Stop.setOnClickListener(handler2);
}
View.OnClickListener handler2 = new View.OnClickListener() {
#Override
public void onClick(View v) {
if (a == aNums[0]) {
Stop();
}
}
}
public void loop() {
handler.post(myRunnable);
}
public void updateText() {
textView2.setText("" + a);
}
public void Stop() {
super.onStop();
handler.removeCallbacks(myRunnable);
}
}
Does anyone know what the problem is?
Thanks
You forgot two closing brackets here:
View.OnClickListener handler2 = new View.OnClickListener() {
#Override
public void onClick(View v) {
for (int i = 0; i <= aNums.length; i++) {
if (a == aNums[0]) {
Stop();
} //I added this
}
}
}//and this
but I think there is no need for a for loop here: you want to stop the thread when you click at the moment a equals the number in your array at index 0, right?
View.OnClickListener handler2 = new View.OnClickListener() {
#Override
public void onClick(View v) {
if (a == aNums[0]) {
Stop();
}
}
}
Also, this check is not necessary because a will never be 10 or more:
if (a < 10) {
handler.postDelayed(this, 1000);
}
The last thing: when you quit the thread, the text is not updated. Try adding the updateText() after the click:
View.OnClickListener handler2 = new View.OnClickListener() {
#Override
public void onClick(View v) {
if (a == aNums[0]) {
Stop();
updateText();
}
}
}

Categories

Resources