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?