0

I'm kinda new to android. So I'm trying calculate the price of something using EditText instead of ListView, and I have increment and decrement buttons included to increase the value on the EditText component.

So the increment and decrement buttons are working okay. They are decrementing and incrementing the value in EditText, and are calculating the price just fine, but when I type in the EditText instead of using the buttons the value I typed doesn't get used.

Here's my Code.

XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop"
        android:src="@drawable/latte" />

    <TextView
        android:id="@+id/quantitytxt_textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:text="Quantity ( Cup(s) )"
        android:textAllCaps="true"
        android:textColor="@android:color/white"
        android:textSize="16sp" />

    <Button
        android:id="@+id/decrementButton"
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:layout_below="@id/quantitytxt_textview"
        android:onClick="decrement"
        android:text="-"
        android:textSize="15sp" />

    <EditText
        android:id="@+id/quantity_textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/quantitytxt_textview"
        android:layout_toRightOf="@id/decrementButton"
        android:padding="10dp"
        android:inputType="text"
        android:text="0"
        android:textColor="@android:color/white"
        android:textSize="16sp" />

    <Button
        android:id="@+id/incrementButton"
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:layout_below="@id/quantitytxt_textview"
        android:layout_toRightOf="@id/quantity_textview"
        android:onClick="increment"
        android:text="+"
        android:textSize="15sp" />

    <TextView
        android:id="@+id/pricetxt_textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/quantity_textview"
        android:padding="10dp"
        android:text="Price (1 Cup = KES 5)"
        android:textAllCaps="true"
        android:textColor="@android:color/white"
        android:textSize="16sp" />

    <TextView
        android:id="@+id/price_textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/pricetxt_textview"
        android:padding="10dp"
        android:text="KES 0"
        android:textColor="@android:color/white"
        android:textSize="16sp" />


    <Button
        android:id="@+id/myButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/price_textview"
        android:onClick="submitOrder"
        android:text="Order"
        android:textSize="15sp" />

</RelativeLayout>

Java

package com.teqqli.justjava;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import java.text.NumberFormat;


/**
 * This app displays an order form to order coffee.
 */
public class MainActivity extends AppCompatActivity {

    int quantity = 0;

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

    /**
     * This method is called when the order button is clicked.
     */
    public void submitOrder(View view) {
        int price = quantity*5;
        displayPrice(price);
    }

    public void increment (View view) {
        quantity = quantity + 1;
        display(quantity);
    }

    public void decrement (View view) {
        if (quantity>0){
            quantity = quantity - 1;
            display(quantity);
        }
    }

    /**
     * This method displays the given quantity value on the screen.
     */
    private void display(int number) {
        EditText quantityText = (EditText) findViewById(R.id.quantity_textview);
        quantityText.setText("" + number);
    }

    /**
     * This method displays the given price on the screen.
     */
    private void displayPrice(int number) {
        TextView priceTextView = (TextView) findViewById(R.id.price_textview);
//        priceTextView.setText(NumberFormat.getCurrencyInstance().format(number));
        priceTextView.setText("KES " + number);
    }
}

Kindly can anyone assist me?

Philip Kimathi
  • 3
  • 1
  • 1
  • 5
  • the value doesn't get used because you're constantly reading from the `number` variable. You have two options, save the text from your EditText to the numbers variable whenever the user changes it, or remove the `numbers` variable, and increment the already displaying number. so `int number = Integer.parseInt(quantityText.getText().toString();` Now the `number` variable needs to be incremented and set again in the edittext – Zun Jul 12 '18 at 10:59
  • on button click, update `quantityText.setTest(quantityText.getText().toString());` – Rakesh R Jul 12 '18 at 11:00
  • Possible duplicate of [Get Value of a Edit Text field](https://stackoverflow.com/questions/4531396/get-value-of-a-edit-text-field) – InsaneCat Jul 12 '18 at 11:07

7 Answers7

0

Use quantityText.getText() function to get the value and store it in a variable and then do the increment and decrement operation on this value.

Something like this:

int value = Integer.parseInt(quantityText.getText().toString().trim());

and then pass this value to your increment and decrement functions.

Einzig7
  • 543
  • 1
  • 6
  • 22
0
String yourValue = quantityText.getText().toString(); //gets you the contents of edit text
tvTextView.setText(yourValue); //displays it in a textview..
Tobias Reich
  • 4,952
  • 3
  • 47
  • 90
InsaneCat
  • 2,115
  • 5
  • 21
  • 40
0

Use quantity = Integer.parseInt(quantityText.getText().toString());

Tobias Reich
  • 4,952
  • 3
  • 47
  • 90
faran.javed
  • 418
  • 2
  • 15
0

Try:

public class MainActivity extends AppCompatActivity {

    EditText quantityText;
    int quantity = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        quantityText= (EditText) findViewById(R.id.quantity_textview);
    }

    /**
     * This method is called when the order button is clicked.
     */
    public void submitOrder(View view) {
        quantity = Integer.parseInt(quantityText.getText().toString());
        int price = quantity*5;
        displayPrice(price);
    }

    public void increment (View view) {
        quantity = quantity + 1;
        display(quantity);
    }

    public void decrement (View view) {
        if (quantity>0){
            quantity = quantity - 1;
            display(quantity);
        }
    }

    /**
     * This method displays the given quantity value on the screen.
     */
    private void display(int number) {
        quantityText.setText("" + number);
    }

    /**
     * This method displays the given price on the screen.
     */
    private void displayPrice(int number) {
        TextView priceTextView = (TextView) findViewById(R.id.price_textview);
        priceTextView.setText("KES " + number);
    }
}
Nabil
  • 654
  • 7
  • 21
0

its simple, for create onclick event:

final EditText input = (EditText) findViewById(R.id.textview_id);
final TextView view = (TextView) findViewById(R.id.textview_id);
final Button button = findViewById(R.id.button_id);
     button.setOnClickListener(new View.OnClickListener() {
         public void onClick(View v) {
             view.setText(input.getText().toString());
         }
     });
0

The reason your EditText doesn't get used is because you are not getting the value from EditText. So get that value like;

String valueFromEditText=quantityText.getText().toString(); // Getting string value from EditText

int valueFromEditTextInIntegerFormat=Integer.parseInt(valueFromEditText) // Converting string value received into integer format

Now use this integer value for performing whatever operations you want

Good Luck :)

0
    You need to create this method: 
       private void display()
        {
            EditText quantityText = (EditText) findViewById(R.id.quantity_textview);



                int  value = Integer.parseInt(quantityText.getText().toString());
                quantity=value;
                Log.i("vl", "display: "+value);

        }
    //replace your method with this 
        public void submitOrder(View view) {
            if(quantity==0)
            {
                display();
            }
            int price = quantity*5;
            quantity=0;
            displayPrice(price);
        }
//if you get any problem ask me in a comment
Vishal Sharma
  • 1,051
  • 2
  • 8
  • 15