Increase text value problem only when double click - java

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));
}
}); }
}

Related

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{}
}
});

What am i doing wrong at my simple android app?

I am trying to make a app that has a switch a button and a text and if you turn the switch on and press the button; the number displayed on the text will be added by 1. But if the switch is turned off the number will be subtracted by 1.
but when i run my app and press the button, the app crashes...
i do not have much experience at programming and i do not know what im doing wrong. and i have only tried this code.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView text = (TextView)findViewById(R.id.textView);
final Button button = (Button)findViewById(R.id.button);
Switch mySwitch = (Switch)findViewById(R.id.mySwitch);
mySwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked== true){
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String text_string = text.getText().toString();
int text_int = Integer.parseInt(text_string);
text_int++;
text.setText(text_int);
}
});
}
if (isChecked == false) {
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String text_string = text.getText().toString();
int text_int = Integer.parseInt(text_string);
text_int++;
text.setText(text_int);
}
});
}
}
});
}
}
so this should behave as i described earlier but it doesn't.
Your app crashes because you are trying to set an int to a textview.setText()and when you pass an int to this method it expects it to be a resource id and which could not be found in your case that's why it will throw ResourceNotFoundException and crashes.
You should set text as following:
text.setText(String.valueOf(text_int));
You’re nesting listeners but that logic doesn’t work sequentially. You should declare your listeners separately. I suggest you create a boolean that holds the state of the switch and one button listener. Within the listener check if switch is enabled then run your calculations and do the same if the switch is disabled.
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(mySwitch.isChecked(){
String text_string = text.getText().toString();
int text_int = Integer.parseInt(text_string);
text_int++;
text.setText(String.valueOf(text_int));
} else {
String text_string = text.getText().toString();
int text_int = Integer.parseInt(text_string);
text_int++;
text.setText(String.valueOf(text_int));
}
}
});
You don't need a listener for the Switch, but only 1 listener for the Button:
final TextView text = (TextView)findViewById(R.id.textView);
final Button button = (Button)findViewById(R.id.button);
final Switch mySwitch = (Switch)findViewById(R.id.mySwitch);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String text_string = text.getText().toString();
int text_int = 0;
try {
text_int = Integer.parseInt(text_string);
} catch (NumberFormatException e) {
e.printStackTrace();
}
if (mySwitch.isChecked())
text_int++;
else
text_int--;
text.setText("" + text_int);
}
});
Every time you click the Button, in its listener the value in the TextView is increased or decreased depending on whether the Switch is checked or not.

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;
}
});

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();
}
}
}

using onLongClickListener to change values quickly

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
}

Categories

Resources