Summing totals to an edit text output and preventing negatives - java

I have been stuck, regrettably, in a simple math problem.
I am creating a coffee application, as seen below, that takes the "Quantity" and multiplies it by the price to create a sub-total for each drink drink. It will then take all sub-totals from the drinks and add them together for the output at the bottom which would be updated automatically.
I have worked on the code to add or subtract from the 0 (I still can't figure out a way to prevent it from going below 0, if anyone has an idea, help is appreciated).
I have it so that the first 2 buttons (in pink) work right now for Espresso and Macchiato.
Here is my Java file
package com.example.cofeeshop;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class DrinkMenu extends Activity {
EditText quantity, quantity2, total;
Button button, plus1, minus1, plus2, minus2;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.drinkmenu);
addListenerOnButton();
// area for the espresso
plus1 = (Button) findViewById(R.id.button6);
minus1 = (Button) findViewById(R.id.button7);
quantity = (EditText) findViewById(R.id.editText2);
// area for the macchiato
plus2 = (Button) findViewById(R.id.button8);
minus2 = (Button) findViewById(R.id.button9);
quantity2 = (EditText) findViewById(R.id.editText4);
//subtotal for espresso
//subtotal for macchiato
total = (EditText) findViewById(R.id.editText9);
plus1.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View V){
String numb1 = quantity.getText().toString();
int num1 = Integer.parseInt(numb1);
int inum1 = num1+1;
quantity.setText(Integer.toString(inum1));
}
});//plus1 button
minus1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String numb1 = quantity.getText().toString();
int num1 = Integer.parseInt(numb1);
int inum1 = num1-1;
quantity.setText(Integer.toString(inum1));
}
});//minus1 button
plus2.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View V){
String numb2 = quantity2.getText().toString();
int num2 = Integer.parseInt(numb2);
int inum2 = num2+1;
quantity2.setText(Integer.toString(inum2));
}
});//plus2 button
minus2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String numb2 = quantity2.getText().toString();
int num2 = Integer.parseInt(numb2);
int inum2 = num2-1;
quantity2.setText(Integer.toString(inum2));
}
});//minus2 button
// Here is where I think I should place the sub-total multiplied by the prices
// and will be out put to the total = espresso_sub_total*espress_price +
// macchiato_sub_total*macchiato_price + and so on for the other drinks
}
}
Now, It may be imperative that I set my Total price to a Text View rather than Edit Text also, thoughts?
So after implementing ideas from both users #useruser3249477 and #Shobhit I have gotten the numbers to stop going below 0 and above 10, but then I tried to add the total together in the Total area of '0'. I have updated code below for both java and the xml file. It crashes as I press the '+' button.
updated Java source code:
package com.example.cofeeshop;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class DrinkMenu extends Activity {
EditText quantity, quantity2;
//TextView total;
Button button, plus1, minus1, plus2, minus2;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.drinkmenu);
addListenerOnButton();
// area for the espresso
plus1 = (Button) findViewById(R.id.button6);
minus1 = (Button) findViewById(R.id.button7);
quantity = (EditText) findViewById(R.id.editText2);
// area for the macchiato
plus2 = (Button) findViewById(R.id.button8);
minus2 = (Button) findViewById(R.id.button9);
quantity2 = (EditText) findViewById(R.id.editText4);
//espresso-sub-total
//macchiato-sub-total
//total = (TextView) findViewById(R.id.textView7);
plus1.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View V){
String numb1 = quantity.getText().toString();
int num1 = Integer.parseInt(numb1);
int inum1 = num1+1;
if (inum1 > 10) return;
quantity.setText(Integer.toString(inum1));
}
});//plus1 button
minus1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String numb1 = quantity.getText().toString();
int num1 = Integer.parseInt(numb1);
int inum1 = num1-1;
if (inum1 < 0) return;
quantity.setText(Integer.toString(inum1));
}
});//minus2 button
plus2.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View V){
String numb2 = quantity2.getText().toString();
int num2 = Integer.parseInt(numb2);
int inum2 = num2+1;
if (inum2 > 10) return;
quantity2.setText(Integer.toString(inum2));
}
});//plus1 buttons
minus2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String numb2 = quantity2.getText().toString();
int num2 = Integer.parseInt(numb2);
int inum2 = num2-1;
if (inum2 < 0) return;
quantity2.setText(Integer.toString(inum2));
}
});
// double subtotal = Double.parseDouble(numb1);
// Here is where I think I should place the sub-total multiplied by the prices
// and will be out put to the total = num1*3;
final TextView total = (TextView) findViewById(R.id.textView7);
TextWatcher textWatcher = new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
// Remove previous price of these items
int count = Integer.parseInt(charSequence.toString());
// Assume total holds text of an integer
int curTotal = Integer.parseInt(total.getText().toString());
int newTotal = curTotal - count*3;
total.setText(newTotal);
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
// Add the new items price
int count = Integer.parseInt(charSequence.toString());
// Assume total holds text of an integer
int curTotal = Integer.parseInt(total.getText().toString());
int newTotal = curTotal + count*3;
total.setText(newTotal);
}
#Override
public void afterTextChanged(Editable editable) {}
};
quantity.addTextChangedListener(textWatcher);
quantity2.addTextChangedListener(textWatcher);
}
//order button code that is useless to this question.
}
Here is my updated XML file that is updated:
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="#+id/button"
android:layout_alignParentTop="true"
android:layout_alignRight="#+id/button"
android:layout_marginRight="75dp"
android:layout_toLeftOf="#+id/textView2"
android:layout_toStartOf="#+id/textView2"
android:background="#FFFFFF"
android:text="Drinks:"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="24dp"
android:textStyle="bold" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="#+id/orderbtn"
android:layout_alignParentTop="true"
android:layout_alignRight="#+id/orderbtn"
android:background="#FFFFFF"
android:text="Quantity:"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="24dp"
android:textStyle="bold" />
<Button
android:id="#+id/button"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/textView"
android:layout_marginTop="30dp"
android:background="#FFFFFF"
android:text="Espresso"
android:textSize="24dp" />
<Button
android:id="#+id/button2"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/button"
android:layout_marginTop="30dp"
android:background="#FFFFFF"
android:text="Macchiato"
android:textSize="24dp" />
<Button
android:id="#+id/button3"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/button2"
android:layout_marginTop="30dp"
android:background="#FFFFFF"
android:text="Con Panna"
android:textSize="24dp" />
<Button
android:id="#+id/button5"
style="?android:attr/buttonStyleSmall"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/button4"
android:layout_marginTop="30dp"
android:background="#FFFFFF"
android:text="Latte"
android:textSize="24dp" />
<Button
android:id="#+id/button6"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/button"
android:background="#ffff45df"
android:text="+"
android:textSize="24dp" />
<EditText
android:id="#+id/editText2"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_above="#+id/button2"
android:layout_alignTop="#+id/button"
android:layout_toLeftOf="#+id/button6"
android:layout_toStartOf="#+id/button6"
android:background="#FFFFFF"
android:digits="0123456789"
android:ems="10"
android:inputType="number"
android:text="0"
android:textSize="24dp" />
<Button
android:id="#+id/button7"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/button"
android:layout_toLeftOf="#+id/editText2"
android:layout_toStartOf="#+id/editText2"
android:background="#ffff45df"
android:text="-"
android:textSize="24dp" />
<Button
android:id="#+id/button8"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/button2"
android:background="#ffff45df"
android:text="+"
android:textSize="24dp" />
<EditText
android:id="#+id/editText3"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/button2"
android:layout_toLeftOf="#+id/button8"
android:layout_toStartOf="#+id/button8"
android:background="#FFFFFF"
android:ems="10"
android:inputType="number"
android:text=" 0"
android:textSize="24dp" />
<EditText
android:id="#+id/editText4"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/button8"
android:layout_alignTop="#+id/button2"
android:layout_toLeftOf="#+id/button8"
android:layout_toStartOf="#+id/button8"
android:background="#FFFFFF"
android:digits="0123456789"
android:ems="10"
android:inputType="number"
android:text="0"
android:textSize="24sp" />
<Button
android:id="#+id/button9"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/button2"
android:layout_toLeftOf="#+id/editText3"
android:layout_toStartOf="#+id/editText3"
android:background="#ffff45df"
android:text="-"
android:textSize="24dp" />
<Button
android:id="#+id/button10"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_alignEnd="#+id/button8"
android:layout_alignRight="#+id/button8"
android:layout_alignTop="#+id/button3"
android:background="#ffff45df"
android:text="+"
android:textSize="24dp" />
<Button
android:id="#+id/button11"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/button4"
android:background="#ffff45df"
android:text="+"
android:textSize="24dp" />
<Button
android:id="#+id/button12"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/button5"
android:background="#ffff45df"
android:text="+"
android:textSize="24dp" />
<EditText
android:id="#+id/editText5"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_above="#+id/button4"
android:layout_alignTop="#+id/button3"
android:layout_toLeftOf="#+id/button10"
android:layout_toStartOf="#+id/button10"
android:background="#FFFFFF"
android:digits="0123456789"
android:ems="10"
android:inputType="number"
android:text="0"
android:textSize="24dp" />
<EditText
android:id="#+id/editText6"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/button11"
android:layout_alignTop="#+id/button4"
android:layout_toLeftOf="#+id/button11"
android:layout_toStartOf="#+id/button11"
android:background="#FFFFFF"
android:digits="0123456789"
android:ems="10"
android:inputType="number"
android:text="0"
android:textSize="24dp" />
<EditText
android:id="#+id/editText7"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/button12"
android:layout_alignTop="#+id/button5"
android:layout_toLeftOf="#+id/button12"
android:layout_toStartOf="#+id/button12"
android:background="#FFFFFF"
android:digits="0123456789"
android:ems="10"
android:inputType="number"
android:text="0"
android:textSize="24dp" />
<Button
android:id="#+id/button14"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/button4"
android:layout_toLeftOf="#+id/editText6"
android:layout_toStartOf="#+id/editText6"
android:background="#ffff45df"
android:text="-"
android:textSize="24sp" />
<Button
android:id="#+id/button15"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/button5"
android:layout_toLeftOf="#+id/editText7"
android:layout_toStartOf="#+id/editText7"
android:background="#ffff45df"
android:text="-"
android:textSize="24sp" />
<Button
android:id="#+id/button16"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/button3"
android:layout_toLeftOf="#+id/editText5"
android:layout_toStartOf="#+id/editText5"
android:background="#ffff45df"
android:text="-"
android:textSize="24sp" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/button7"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:text="$3.00 per drink"
android:textColor="#000000"
android:textSize="20sp" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/button9"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:text="$3.00 per drink"
android:textColor="#000000"
android:textSize="20sp" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/button16"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:text="$3.00 per drink"
android:textColor="#000000"
android:textSize="20sp" />
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/button14"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:text="$3.00 per drink"
android:textColor="#000000"
android:textSize="20sp" />
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/button15"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:text="$3.00 per drink"
android:textColor="#000000"
android:textSize="20sp" />
<Button
android:id="#+id/button4"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/button3"
android:layout_marginTop="32dp"
android:background="#FFFFFF"
android:text="Americano"
android:textSize="24dp"
android:textColor="#000000" />
<Button
android:id="#+id/orderbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:background="#drawable/buttonround"
android:text="Order"
android:textSize="24sp" />
<TextView
android:id="#+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/orderbtn"
android:layout_alignBottom="#+id/orderbtn"
android:layout_alignParentLeft="true"
android:background="#FFFFFF"
android:text="Total: $"
android:textColor="#000000"
android:textSize="24sp" />
<TextView
android:id="#+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView8"
android:layout_alignBottom="#+id/textView8"
android:layout_alignLeft="#+id/textView"
android:layout_alignRight="#+id/textView6"
android:layout_alignStart="#+id/textView"
android:background="#FFFFFF"
android:text="0"
android:textSize="24sp" />
</RelativeLayout>
Here are some of the errors coming out.
E/AndroidRuntime(369): FATAL EXCEPTION: main
E/AndroidRuntime(369): android.content.res.Resources$NotFoundException: String resource ID #0x0// seems to be here
E/AndroidRuntime(369): at android.content.res.Resources.getText(Resources.java:201)

A simple check will prevent negative values (using the buttons):
minus2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String numb2 = quantity2.getText().toString();
int num2 = Integer.parseInt(numb2);
int inum2 = num2-1;
if (inum2 < 0) return;
quantity2.setText(Integer.toString(inum2));
}
});
To prevent manually entering negative values, you can set android:digits="0123456789" as #ShobhitPuri suggested.
Then for the total you'll need to set TextWatchers:
final EditText total = (EditText) findViewById(R.id.editText9);
TextWatcher textWatcher = new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
// Remove previous price of these items
int count = Integer.parseInt(charSequence.toString());
// Assume total holds text of an integer
int curTotal = Integer.parseInt(total.getText().toString());
int newTotal = curTotal - count*3;
total.setText(newTotal);
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
// Add the new items price
int count = Integer.parseInt(charSequence.toString());
// Assume total holds text of an integer
int curTotal = Integer.parseInt(total.getText().toString());
int newTotal = curTotal + count*3;
total.setText(newTotal);
}
#Override
public void afterTextChanged(Editable editable) {}
};
// Now set the TextWatcher on every count EditText
// If you have different prices, you'll need multiple TextWatchers
quantity1.addTextChangedListener(textWatcher);
quantity2.addTextChangedListener(textWatcher);
...

"I still can't figure out a way to prevent it from going below 0"
One way is you can add android:digits="0123456789" to your EditText's in xml file. This will prevent user from entering anything but these numbers.
One other way is to do a check in the onClickListener. In this you'll need to check for all the EditText's values using ed.getText() and then check if its integer and is in the range acceptable by you.
One more way is to use addTextChangedListener on EditText. You can listen you what is being inputted there. You can make a check for condition when its less than 0 or an invalid character has been put in. (Its good to limit the keyboard so as to prevent garbage entry at the first place).

Related

How to send the value of chosen RadioButton and DatePicker to second Activity?

I'm going to create an app called Student Registration which includes Full Name, Student Number, Course and Section, Age, Email, Password, Gender, and Date of Birth
I did this coding on my Android using AIDE app
*the coding entails two activities with their corresponding java and xml(design interface):
The first Activity should allow users to input all the necessary information, and when the text field filled out completely there is a button of adding the information and storing it to second activity
The Second Activity on the other hand should display all the information filled from the first previous activity *
What makes me happy that it works so well, except for gender and birthdate because I found it difficult to pass RadioButton and DatePicker as a string to second activity
please help me this is my project to be submitted
Codes
1. Main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" android:background="#drawable/background_rs"
android:padding="0dp">
<EditText
android:layout_width="300dp"
android:inputType="textCapCharacters|textCapSentences|textCapWords|textPersonName"
android:layout_height="wrap_content"
android:textSize="20sp"
android:hint="Full Name"
android:textStyle="italic"
android:typeface="serif"
android:id="#+id/typeName"
android:layout_marginTop="150dp"/>
<EditText
android:layout_width="300dp"
android:inputType="number"
android:layout_height="wrap_content"
android:textSize="20sp"
android:hint="Student Number"
android:textStyle="italic"
android:typeface="serif"
android:id="#+id/typeStudent"
android:layout_marginTop="13dp"/>
<AutoCompleteTextView
android:layout_width="300dp"
android:inputType="textAutoComplete|textCapCharacters|textCapSentences|textCapWords"
android:layout_height="wrap_content"
android:textSize="20sp"
android:hint="Course and Section"
android:textStyle="italic"
android:typeface="serif"
android:id="#+id/typeLevel"
android:layout_marginTop="13dp"
android:completionThreshold="1"
android:completionHint="Course and Section"/>
<EditText
android:layout_width="300dp"
android:inputType="number"
android:layout_height="wrap_content"
android:textSize="20sp"
android:hint="Age"
android:textStyle="italic"
android:typeface="serif"
android:id="#+id/typeAge"
android:layout_marginTop="13dp"/>
<EditText
android:layout_width="300dp"
android:inputType="textEmailAddress"
android:layout_height="wrap_content"
android:textSize="20sp"
android:hint="Email Address"
android:textStyle="italic"
android:typeface="serif"
android:id="#+id/typeEmail"
android:layout_marginTop="13dp"/>
<EditText
android:layout_width="300dp"
android:inputType="textWebPassword"
android:layout_height="wrap_content"
android:textSize="20sp"
android:hint="Password"
android:textStyle="italic"
android:typeface="serif"
android:id="#+id/typePassword"
android:layout_marginTop="13dp"/>
<TextView
android:layout_width="300dp"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textStyle="normal"
android:typeface="serif"
android:id="#+id/typeGender"
android:layout_marginTop="13dp"
android:text="Select Gender"
android:layout_marginLeft="5dp"/>
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/radioGroup">
<RadioButton
android:id="#+id/rb_male"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Male"
android:textSize="20dp"
android:layout_marginLeft="70dp"
android:typeface="serif"
android:onClick="checkButton"
android:checked="true"/>
<RadioButton
android:id="#+id/rb_female"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Female"
android:textSize="20dp"
android:layout_marginLeft="70dp"
android:typeface="serif"
android:onClick="checkButton"/>
</RadioGroup>
<TextView
android:layout_width="300dp"
android:layout_height="wrap_content"
android:textSize="20sp"
android:hint="Select Birthday"
android:textStyle="italic"
android:typeface="serif"
style="?android:spinnerStyle"
android:id="#+id/typeBirthday"
android:layout_marginTop="13dp"
android:onClick="openDatePicker"
android:text="Date of Birth"/>
<Button
android:layout_width="300dp"
android:layout_height="wrap_content"
android:text="ADD TO THE LIST"
android:layout_marginTop="40dp"
android:background="#FFFFFF"
android:id="#+id/buttonAdd"/>
</LinearLayout>
2. Main Activity.java
import android.app.*;
import android.content.*;
import android.graphics.*;
import android.graphics.drawable.*;
import android.icu.util.*;
import android.os.*;
import android.util.*;
import android.view.*;
import android.widget.*;
import android.widget.RadioGroup.*;
public class MainActivity extends Activity {
private EditText typeName, typeStudent, typeAge, typeEmail, typePassword;
private Button buttonAdd;
private AutoCompleteTextView typeLevel;
private static final String[] section = new String []{"STEM 12A", "STEM12B", "STEM12C", "STEM12D", "STEM12E"};
private RadioGroup radioGroup;
private RadioButton radioButton, rb_male, rb_female;
private TextView mtypeBirthday;
private DatePickerDialog.OnDateSetListener mDateSetListener;
public static final String EXTRA_NAME = "com.krapcollaboration.srnewproject.EXTRA_NAME";
public static final String EXTRA_NUMBER = "com.krapcollaboration.srnewproject.EXTRA_TEXT";
public static final String EXTRA_LEVEL = "com.krapcollaboration.srnewproject.EXTRA_LEVEL";
public static final String EXTRA_AGE = "com.krapcollaboration.srnewproject.EXTRA_AGE";
public static final String EXTRA_EMAIL = "com.krapcollaboration.srnewproject.EXTRA_EMAIL";
public static final String EXTRA_PASSWORD = "com.krapcollaboration.srnewproject.EXTRA_PASSWORD";
public static final String EXTRA_DATE = "com.krapcollaboration.srnewproject.EXTRA_DATE";
public static final String EXTRA_GENDER = "com.krapcollaboration.srnewproject.EXTRA_GENDER";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
typeName = findViewById(R.id.typeName);
typeStudent = findViewById(R.id.typeStudent);
final AutoCompleteTextView editText = findViewById(R.id.typeLevel);
typeAge = findViewById(R.id.typeAge);
typeEmail = findViewById(R.id.typeEmail);
typePassword = findViewById(R.id.typePassword);
mtypeBirthday = findViewById(R.id.typeBirthday);
radioGroup = findViewById(R.id.radioGroup);
buttonAdd = findViewById(R.id.buttonAdd);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, section);
editText.setAdapter(adapter);
RadioButton rb_male =findViewById(R.id.rb_male);
RadioButton rb_female = findViewById(R.id.rb_female);
mtypeBirthday .setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar. YEAR);
int month = cal.get(Calendar. MONTH);
int day = cal.get(Calendar. DAY_OF_MONTH);
DatePickerDialog dialog = new DatePickerDialog(
MainActivity.this,
android.R.style.Theme_Holo_Light_Dialog_MinWidth,
mDateSetListener,
year, month, day);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.show();
}
});
mDateSetListener = new DatePickerDialog.OnDateSetListener(){
#Override
public void onDateSet(DatePicker datepicker, int year, int month, int day) {
month = month + 1;
String TAG = null;
Log.d(TAG, "onDateSet:mm/dd/yyyy: " + month + "/" + day + "/" + year);
String date = month + "/" + day + "/" + year;
mtypeBirthday.setText(date);
}
};
buttonAdd.setOnClickListener(new View.OnClickListener () {
#Override
public void onClick(View v) {
String NameStored = typeName.getText().toString();
String NumberStored = typeStudent.getText().toString();
String LevelStored = editText.getText().toString();
String UsernameStored = typeAge.getText().toString();
String EmailStored = typeEmail.getText().toString();
String PasswordStored = typePassword.getText().toString();
int GenderStored = radioGroup.getCheckedRadioButtonId();
radioButton = findViewById(GenderStored);
String BirthdayStored = mtypeBirthday.getText().toString();
Intent intention = new Intent(MainActivity. this,Main2Activity.class);
intention.putExtra(EXTRA_NAME, NameStored);
intention.putExtra(EXTRA_NUMBER, NumberStored);
intention.putExtra(EXTRA_LEVEL, LevelStored);
intention.putExtra(EXTRA_AGE, UsernameStored);
intention.putExtra(EXTRA_EMAIL, EmailStored);
intention.putExtra(EXTRA_PASSWORD, PasswordStored);
intention.putExtra(EXTRA_DATE, BirthdayStored);
intention.putExtra(EXTRA_GENDER, GenderStored);
startActivity(intention);
}
});
}
public void checkButton(View v) {
int GenderStored = radioGroup.getCheckedRadioButtonId();
radioButton = findViewById(GenderStored);
} }
3. Main2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:gravity="center"
android:background="#drawable/background_rs2"
android:textAlignment="viewStart">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="FULL NAME:"
android:typeface="monospace"
android:textStyle="italic"
android:textSize="13sp"
android:textColor="#848484"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="#+id/textViewName"
android:textColor="#C25AF4"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#848484"
android:text="STUDENT NUMBER:"
android:typeface="monospace"
android:layout_marginTop="10dp"
android:textStyle="italic"
android:textSize="13sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="#+id/textViewStudent"
android:textColor="#C25AF4"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#848484"
android:text="COURSE AND SECTION:"
android:typeface="monospace"
android:layout_marginTop="10dp"
android:textStyle="italic"
android:textSize="13sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="#+id/textViewLevel"
android:textColor="#C25AF4"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#848484"
android:text="AGE:"
android:typeface="monospace"
android:layout_marginTop="10dp"
android:textStyle="italic"
android:textSize="13sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="#+id/textViewAge"
android:textColor="#C25AF4"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#848484"
android:text="EMAIL ADDRESS:"
android:typeface="monospace"
android:layout_marginTop="10dp"
android:textStyle="italic"
android:textSize="13sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
`android:textAppearance="?android:attr/textAppearanceMedium`"
android:id="#+id/textViewEmail"
android:textColor="#C25AF4"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#848484"
android:text="PASSWORD:"
android:typeface="monospace"
android:layout_marginTop="10dp"
android:textStyle="italic"
android:textSize="13sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="#+id/textViewPassword"
android:textColor="#C25AF4"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#848484"
android:text="GENDER:"
android:typeface="monospace"
android:layout_marginTop="10dp"
android:textStyle="italic"
android:textSize="13sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="#+id/textViewGender"
android:textColor="#C25AF4"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#848484"
android:text="DATE OF BIRTH:"
android:typeface="monospace"
android:layout_marginTop="10dp"
android:textStyle="italic"
android:textSize="13sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="#+id/textViewBirthday"
android:textColor="#C25AF4"/>
<Button
android:layout_width="300dp"
android:layout_height="wrap_content"
android:text="CONFIRM INFORMATION"
android:layout_marginTop="40dp"
android:id="#+id/buttonConfirm"
android:textSize="16sp"
android:background="#FFFFFF"
android:textColor="#000000"/>
</LinearLayout>
4. Main2Activity.java
import android.app.*;
import android.content.*;
import android.os.*;
import android.view.*;
import android.widget.*;
public class Main2Activity extends Activity
{
private TextView textViewName, textViewStudent, textViewLevel, textViewAge, textViewEmail, textViewPassword, textViewGender, TextViewBirthday;
private Button buttonConfirm;
#Override
protected void onCreate(Bundle SavedInstanceState) {
super.onCreate(SavedInstanceState);
setContentView(R.layout.main2);
Intent intention = getIntent();
String NameStored = intention.getStringExtra(MainActivity.EXTRA_NAME);
String NumberStored = intention.getStringExtra(MainActivity.EXTRA_NUMBER);
String LevelStored = intention.getStringExtra(MainActivity.EXTRA_LEVEL);
String UsernameStored = intention.getStringExtra(MainActivity.EXTRA_AGE);
String EmailStored = intention.getStringExtra(MainActivity.EXTRA_EMAIL);
String PasswordStored = intention.getStringExtra(MainActivity.EXTRA_PASSWORD);
int BirthdayStored = intention.getIntExtra(MainActivity.EXTRA_DATE, 0);
int GenderStored = intention.getIntExtra(MainActivity. EXTRA_GENDER, 0);
textViewName = findViewById(R.id.textViewName);
textViewStudent = findViewById(R.id.textViewStudent);
textViewLevel = findViewById(R.id.textViewLevel);
textViewAge = findViewById(R.id.textViewAge);
textViewEmail = findViewById(R.id.textViewEmail);
textViewPassword = findViewById(R.id.textViewPassword);
textViewGender = findViewById(R.id.textViewGender);
TextViewBirthday = findViewById(R.id.textViewBirthday);
textViewName.setText(NameStored);
textViewStudent.setText(NumberStored);
textViewLevel.setText(LevelStored);
textViewAge.setText(UsernameStored);
textViewEmail.setText(EmailStored);
textViewPassword.setText(PasswordStored);
textViewGender.setText(" " + GenderStored);
TextViewBirthday.setText(" " + BirthdayStored);
buttonConfirm = findViewById(R.id.buttonConfirm);
buttonConfirm.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
Toast.makeText(getApplicationContext(),"Successfully Registered",Toast.LENGTH_SHORT).show();
}
});
}}
```![enter image description here](https://i.stack.imgur.com/ejKQC.jpg)![enter image description here](https://i.stack.imgur.com/Q7rfm.jpg)
On MainActivity
String GenderStored ="Male";
if(R.id.rb_female==radioGroup.getCheckedRadioButtonId()){
GenderStored ="Female";
}
Use this to get the Gender.
And on Main2Activity, as you are passing Date of Birth and Gender as String
String BirthdayStored = intention.getStringExtra(MainActivity.EXTRA_DATE);
String GenderStored = intention.getStringExtra(MainActivity.EXTRA_GENDER);

Text not appearing but exists, Trying to make a simple app that calculates average

Been trying to make a simple app that can calculate the average of 8 numbers(with decimals). I already made the layout and made an initial project that adds two numbers which when i tested is working fine but when i decided to continue on my original goal, make an app that can calculate the average of 8 numbers(with decimal), a problem occured. When i tested it on my phone, i typed some numbers on the number field, it moves indicating that something is being typed but what i typed didn't appear. At first my hunch is that the font color is white which is not, but it still doesn't show. Please help.
my layout:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="#+id/sub"
android:layout_width="140dp"
android:layout_height="20dp"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:editable="true"
android:text="Subject"
app:fontFamily="sans-serif-black"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/sub1" />
<TextView
android:id="#+id/sub1"
android:layout_width="140dp"
android:layout_height="20dp"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:editable="true"
android:text="Subject"
app:fontFamily="sans-serif-black"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/sub2"
android:layout_width="140dp"
android:layout_height="20dp"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:editable="true"
android:text="Subject"
app:fontFamily="sans-serif-black"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/sub" />
<TextView
android:id="#+id/sub4"
android:layout_width="140dp"
android:layout_height="20dp"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:editable="true"
android:text="Subject"
app:fontFamily="sans-serif-black"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/sub2" />
<TextView
android:id="#+id/sub5"
android:layout_width="140dp"
android:layout_height="20dp"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:editable="true"
android:text="Subject"
app:fontFamily="sans-serif-black"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/sub4" />
<TextView
android:id="#+id/sub6"
android:layout_width="140dp"
android:layout_height="20dp"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:editable="true"
android:text="Subject"
app:fontFamily="sans-serif-black"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/sub5" />
<TextView
android:id="#+id/sub7"
android:layout_width="140dp"
android:layout_height="20dp"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:editable="true"
android:text="Subject"
app:fontFamily="sans-serif-black"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/sub6" />
<TextView
android:id="#+id/sub8"
android:layout_width="140dp"
android:layout_height="20dp"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:editable="true"
android:text="Subject"
app:fontFamily="sans-serif-black"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/sub7" />
<EditText
android:id="#+id/num"
android:layout_width="60dp"
android:layout_height="20dp"
android:layout_marginStart="30dp"
android:layout_marginLeft="30dp"
android:layout_marginTop="20dp"
android:editable="true"
android:ems="10"
android:inputType="numberDecimal"
android:textColor="#00FF0000"
app:layout_constraintStart_toEndOf="#+id/sub1"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="#+id/num1"
android:layout_width="60dp"
android:layout_height="20dp"
android:layout_marginStart="30dp"
android:layout_marginLeft="30dp"
android:layout_marginTop="10dp"
android:ems="10"
android:inputType="numberDecimal"
app:layout_constraintStart_toEndOf="#+id/sub"
app:layout_constraintTop_toBottomOf="#+id/num" />
<EditText
android:id="#+id/num2"
android:layout_width="60dp"
android:layout_height="20dp"
android:layout_marginStart="30dp"
android:layout_marginLeft="30dp"
android:layout_marginTop="10dp"
android:ems="10"
android:inputType="numberDecimal"
app:layout_constraintStart_toEndOf="#+id/sub2"
app:layout_constraintTop_toBottomOf="#+id/num1" />
<EditText
android:id="#+id/num3"
android:layout_width="60dp"
android:layout_height="20dp"
android:layout_marginStart="30dp"
android:layout_marginLeft="30dp"
android:layout_marginTop="10dp"
android:ems="10"
android:inputType="numberDecimal"
app:layout_constraintStart_toEndOf="#+id/sub4"
app:layout_constraintTop_toBottomOf="#+id/num2" />
<EditText
android:id="#+id/num4"
android:layout_width="60dp"
android:layout_height="20dp"
android:layout_marginStart="30dp"
android:layout_marginLeft="30dp"
android:layout_marginTop="10dp"
android:ems="10"
android:inputType="numberDecimal"
app:layout_constraintStart_toEndOf="#+id/sub5"
app:layout_constraintTop_toBottomOf="#+id/num3" />
<EditText
android:id="#+id/num5"
android:layout_width="60dp"
android:layout_height="20dp"
android:layout_marginStart="30dp"
android:layout_marginLeft="30dp"
android:layout_marginTop="10dp"
android:ems="10"
android:inputType="numberDecimal"
app:layout_constraintStart_toEndOf="#+id/sub6"
app:layout_constraintTop_toBottomOf="#+id/num4" />
<EditText
android:id="#+id/num6"
android:layout_width="60dp"
android:layout_height="20dp"
android:layout_marginStart="30dp"
android:layout_marginLeft="30dp"
android:layout_marginTop="10dp"
android:ems="10"
android:inputType="numberDecimal"
app:layout_constraintStart_toEndOf="#+id/sub7"
app:layout_constraintTop_toBottomOf="#+id/num5" />
<EditText
android:id="#+id/num7"
android:layout_width="60dp"
android:layout_height="20dp"
android:layout_marginStart="30dp"
android:layout_marginLeft="30dp"
android:layout_marginTop="10dp"
android:ems="10"
android:inputType="numberDecimal"
app:layout_constraintStart_toEndOf="#+id/sub8"
app:layout_constraintTop_toBottomOf="#+id/num6" />
<Button
android:id="#+id/btnAvr"
android:layout_width="80dp"
android:layout_height="30dp"
android:layout_marginStart="180dp"
android:layout_marginLeft="180dp"
android:layout_marginTop="12dp"
android:text="Get Average"
android:textSize="8sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/tvAnswer" />
<TextView
android:id="#+id/tvAnswer"
android:layout_width="60dp"
android:layout_height="20dp"
android:layout_marginStart="190dp"
android:layout_marginLeft="190dp"
android:layout_marginTop="30dp"
android:text="Average"
android:textSize="14sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/num7" />
My Java Code
package com.example.cardmkii;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView sub;
private TextView sub1;
private TextView sub2;
private TextView sub4;
private TextView sub5;
private TextView sub6;
private TextView sub7;
private TextView sub8;
private EditText numb;
private EditText numb1;
private EditText numb2;
private EditText numb3;
private EditText numb4;
private EditText numb5;
private EditText numb6;
private EditText numb7;
private Button average;
private TextView result;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sub = (TextView) findViewById(R.id.sub);
sub1 = (TextView) findViewById(R.id.sub1);
sub2 = (TextView) findViewById(R.id.sub2);
sub4 = (TextView) findViewById(R.id.sub4);
sub5 = (TextView) findViewById(R.id.sub5);
sub6 = (TextView) findViewById(R.id.sub6);
sub7 = (TextView) findViewById(R.id.sub7);
sub8 = (TextView) findViewById(R.id.sub8);
numb = (EditText) findViewById(R.id.num);
numb1 = (EditText) findViewById(R.id.num1);
numb2 = (EditText) findViewById(R.id.num2);
numb3 = (EditText) findViewById(R.id.num3);
numb4 = (EditText) findViewById(R.id.num4);
numb5 = (EditText) findViewById(R.id.num5);
numb6 = (EditText) findViewById(R.id.num6);
numb7 = (EditText) findViewById(R.id.num7);
average = (Button) findViewById(R.id.btnAvr);
result = (TextView) findViewById(R.id.tvAnswer);
average.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
double number = Double.parseDouble(numb.getText().toString());
double number1 = Double.parseDouble(numb1.getText().toString());
double number2 = Double.parseDouble(numb2.getText().toString());
double number3 = Double.parseDouble(numb3.getText().toString());
double number4 = Double.parseDouble(numb4.getText().toString());
double number5 = Double.parseDouble(numb5.getText().toString());
double number6 = Double.parseDouble(numb6.getText().toString());
double number7 = Double.parseDouble(numb7.getText().toString());
double average = (number + number1 + number2 + number3 + number4 + number5 + number6 + number7)/8;
result.setText("Answer: " + String.valueOf(average));
}
});
}
}
Set height in every edittext to wrap content
eg.
android:layout_height="wrap_content"
and set textcolor dark currently i run your layout in white background it not see any text.
so use visible color for text

Crashing whenever any buttons are clicked

The app executed just fine, and the interface loads just fine as well, but when I try to press any of the buttons, nothing that should happen happens. The point is fairly simple: press the '+' button, the quantity increases, and the price immediately updates to match said quantity, vice versa for the '-' button. I don't know what I've done wrong, but any button interaction with the app crashes it.
Here's my Java:
package com.t99sdevelopment.mobile.eyy;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import java.text.NumberFormat;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public int quantityValue = 1;
public void increaseQuantity (View view) {
quantityValue = quantityValue + 1;
updateQuantityValue(view);
}
public void decreaseQuantity (View view) {
quantityValue = quantityValue - 1;
updateQuantityValue(view);
}
public void updateQuantityValue(View view) {
updateQuantity(quantityValue);
updatePrice(quantityValue);
}
private void updateQuantity(int number) {
TextView quantity = (TextView) findViewById(
R.id.quantityValue);
quantity.setText(number);
}
private void updatePrice(int number) {
TextView price = (TextView) findViewById(R.id.priceValue);
price.setText(NumberFormat.getCurrencyInstance().format(number * 5));
}
}
Here's my XML:
<TextView
android:text="Quantity"
android:id="#+id/quantityText"
android:textAllCaps="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textColor="#000000"
android:autoText="false"
android:paddingBottom="20dp" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="center_horizontal">
<TextView
android:text="0"
android:id="#+id/quantityValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="16dp"
android:paddingBottom="16dp" />
<Button
android:layout_width="48dp"
android:layout_height="48dp"
android:text="-"
android:id="#+id/button"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/quantityValue"
android:layout_toStartOf="#+id/quantityValue"
android:layout_marginRight="15dp"
android:layout_marginEnd="15dp"
android:onClick="decreaseQuantity" />
<Button
android:layout_width="48dp"
android:layout_height="48dp"
android:text="+"
android:id="#+id/button2"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/quantityValue"
android:layout_toEndOf="#+id/quantityValue"
android:layout_marginLeft="15dp"
android:layout_marginStart="15dp"
android:onClick="increaseQuantity" />
</RelativeLayout>
<TextView
android:text="Price"
android:id="#+id/priceText"
android:textAllCaps="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textColor="#000000"
android:autoText="false"
android:paddingTop="20dp" />
<TextView
android:text="$0"
android:id="#+id/priceValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="20dp"
android:paddingBottom="20dp" />
<Button
android:text="Order"
android:textAllCaps="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
The formatting of the actual code is just fine, but it got screwy when I pasted in into stack, so I know that's not the problem.
Change you updateQuantity and updatePrice method like :
private void updateQuantity(int number) {
TextView quantity = (TextView) findViewById(
R.id.quantityValue);
quantity.setText(String.valueOf(number));
}
private void updatePrice(int number) {
TextView price = (TextView) findViewById(R.id.priceValue);
price.setText(String.valueOf(NumberFormat.getCurrencyInstance().format(number * 5)));
}
}

How to use spinners in android?

I am new to android. I am trying to make simple unit converter. The layout contains EditText for input, two spinners (to and from), an ImageButton and EditText to display answer.
I want to add values to spinners and when these values are selected i want to perform action.
My xml code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context=".BitsBytes" >
<Spinner
android:id="#+id/SpinnerFrom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView2"
android:layout_alignRight="#+id/inputvalue"
android:layout_below="#+id/textView2"
android:entries="#array/bitsbytesfrom"
android:prompt="#string/from_prompt" />
<Spinner
android:id="#+id/SpinnerTo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView3"
android:layout_alignRight="#+id/SpinnerFrom"
android:layout_below="#+id/textView3"
android:entries="#array/bitsbytesto"
android:prompt="#string/to_prompt" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/SpinnerFrom"
android:layout_below="#+id/SpinnerFrom"
android:layout_marginTop="16dp"
android:text="To:"
android:textColor="#000000"
android:textSize="20dp" />
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_below="#+id/textView1"
android:layout_marginTop="16dp"
android:gravity="center"
android:text="Value: "
android:textSize="20dp" />
<EditText
android:id="#+id/inputvalue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView6"
android:layout_alignBottom="#+id/textView6"
android:layout_toRightOf="#+id/textView6"
android:ems="10"
android:hint="Enter value"
android:inputType="numberDecimal" >
<requestFocus />
</EditText>
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView6"
android:layout_below="#+id/inputvalue"
android:layout_marginTop="16dp"
android:text="From:"
android:textColor="#000000"
android:textSize="20sp" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="19dp"
android:text="Bits/Bytes Conversion"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="25dp" />
<ImageButton
android:id="#+id/convertButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/SpinnerTo"
android:layout_centerHorizontal="true"
android:background="#00000000"
android:src="#drawable/btnconvert"
android:clickable="true"/>
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/answer"
android:layout_centerHorizontal="true"
android:text="Answer"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="#+id/answer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="28dp"
android:ems="10"
android:inputType="numberDecimal" />
</RelativeLayout>
My java code:
package com.easy.convert;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Spinner;
import android.widget.TextView;
public class BitsBytes extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.bitsbytes);
ImageButton ib = (ImageButton) findViewById(R.id.convertButton);
ib.setOnClickListener(new View.OnClickListener() {
EditText etValue = (EditText) findViewById(R.id.inputvalue);
Spinner spBitsBytesFrom = (Spinner) findViewById(R.id.SpinnerFrom);
Spinner spBitsBytesTo = (Spinner) findViewById(R.id.SpinnerTo);
String txtFromSpinner = spBitsBytesFrom.getSelectedItem().toString();
String txtToSpinner = spBitsBytesTo.getSelectedItem().toString();
TextView Answer = (TextView) findViewById(R.id.answer);
public void onClick(View v)
{
if (txtFromSpinner.equals("Bits") && txtToSpinner.equals("Bytes"))
{
Double value = Double.parseDouble(etValue.getText().toString());
Double answer = (double) 0;
answer = value/8;
String stringAnswer = Double.toString(answer);
Answer.setText(stringAnswer);
}
}
});
}
}
I am filling the spinners using string array. I am not able to figure out whats wrong in my code because there is no error in logcat.
Change this,
(txtFromSpinner.equals("Bits") && txtToSpinner.equals("Bytes"))
to
if((txtFromSpinner.getSelectedItem().toString().equals("Bits")) && (txtToSpinner.getSelectedItem().toString().equals("Bytes")))
this will get the text from the item selected in the spinner.

Simple calculator program not showing proper output

I have some simple code where I input two numbers in EditText fields, add them together, and show them in a TextView.
Here is my code:
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world"
android:visibility="invisible"/>
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_below="#+id/textView1"
android:layout_marginTop="32dp"
android:ems="10"
android:inputType="number" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/editText1"
android:layout_below="#+id/editText1"
android:layout_marginTop="21dp"
android:ems="10"
android:inputType="number" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/button1"
android:layout_alignBottom="#+id/button1"
android:layout_alignRight="#+id/editText2"
android:text="Clear"
android:onClick="Clicked" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/editText2"
android:layout_marginTop="35dp"
android:text="Total"
android:onClick="Clicked" />
And here is my Clicked method:
public void Clicked(View v) {
int total;
EditText t1= (EditText)findViewById(R.id.editText1);
EditText t2= (EditText)findViewById(R.id.editText2);
TextView tv= (TextView)findViewById(R.id.textView1);
if(v.getId()==R.id.button1)
{
total= Integer.parseInt(t1.getText().toString()+t2.getText().toString());
tv.setText(total);
tv.setVisibility(1);
}
else if (v.getId()==R.id.button2)
{
t1.setText("");
t2.setText("");
}
}
I input two numbers and when I press clear it works fine but the total button does not add. Instead, this happens:
It might be a problem with my integer casting or a problem with logic.
Change this:
total= Integer.parseInt(t1.getText().toString()+t2.getText().toString());
To this:
int v1 = Integer.parseInt(t1.getText().toString());
int v2 = Integer.parseInt(t2.getText().toString());
total = v1 + v2;
Its stopped because you may be setting a Int value to setText of TextView. You need to convert it to String
textView.setText(CharSequence text)
int v1 = Integer.parseInt(t1.getText().toString());
int v2 = Integer.parseInt(t2.getText().toString());
total = v1 + v2;
tv.setText(total+""); //this converts total to string
//tv.setText(total.toString()); //can use either

Categories

Resources