when i try to increase text value with button i actually have to press it 2 times to increase once, what is really annoying.
I use checkbox
There is my code:
TextView tvCounter;
Button btnIncrement;
private CheckBox i1, i2, i3;
TextView tvCounter2;
int counter=0;
int counter2=0;
private void initialStates(Intent intent) {
i1=findViewById(R.id.q1_1);
i2=findViewById(R.id.q1_2);
i3=findViewById(R.id.q1_3);
}
btnIncrement.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
tvCounter.setText(String.valueOf(counter));
tvCounter2.setText(String.valueOf(counter2));
if (i1.isChecked())
counter++;
if(i2.isChecked())
counter2++;
if (i3.isChecked())
counter2++;
}
}); }
}
You need to increment them first, then set the values.
btnIncrement.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (i1.isChecked())
counter++;
if(i2.isChecked())
counter2++;
if (i3.isChecked())
counter2++;
tvCounter.setText(String.valueOf(counter));
tvCounter2.setText(String.valueOf(counter2));
}
}); }
}
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
}
}
});
I want to increase counter android in ListView adapter, but next click row always failure increased.
counting_update_cycle = actual.getJson().getCycle();
counting_update = counting_update_cycle;
holder.counting.setText(String.valueOf(counting_update_cycle));
//action tambah
holder.btntambahekekusi.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
holder.counting.setText(String.valueOf(counting_update));
counting_update++;
}
});
Please try below code:
counting_update_cycle = actual.getJson().getCycle();
counting_update = counting_update_cycle;
holder.counting.setText(String.valueOf(counting_update_cycle));
//action tambah
holder.btntambahekekusi.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// added code
counting_update_cycle = actual.getJson().getCycle();
counting_update = counting_update_cycle;
holder.counting.setText(String.valueOf(counting_update));
// update instance property of object
actual.getJson().setCycle(counting_update++);
}
});
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;
}
});
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
}