Before I delve into it, I'm very new to Android and I have just started learning Java last month. I've hit bumps while trying to develop my first simple app. Most of these hurdles were jumped thanks to random tutorials online. MY CODE IS VERY MESSY. Any tips are appreciated.
The question above is quite broad, but this is what I want to do: It's a essentially a blood alcohol content calculator / drink keeper-tracker. Basic layout: http://i.imgur.com/JGuh7.jpg
The buttons along the bottom are just regular buttons, not ImageButtons (had problems with that) Here's some example code of one:
<Button android:id="@+id/Button01"
android:layout_width="wrap_content"
android:layout_marginRight="5dp"
android:background="@drawable/addbeer"/>
The buttons and TextView are all in main.xml.
I have variables defined in a class called Global.java:
package com.dantoth.drinkingbuddy;
import android.app.Activity;
public class Global extends Activity{
public static double StandardDrinks = 0;
public static double BeerOunces = 12;
public static double BeerPercentAlcohol = .05;
public static double BeerDrink = BeerOunces * BeerPercentAlcohol;
public static double BeerDrinkFinal = BeerDrink * 1.6666666;
public static double ShotOunces = 1.5;
public static double ShotPercentAlcohol = .4;
public static double ShotDrink = ShotOunces * ShotPercentAlcohol;
public static double ShotDrinkFinal = ShotDrink * 1.6666666;
public static double WineOunces = 5;
public static double WinePercentAlcohol = .12;
public static double WineDrink = WineOunces * WinePercentAlcohol;
public static double WineDrinkFinal = WineDrink * 1.6666666;
public static double OtherOunces;
public static double OtherPercentAlcohol;
public static double OtherDrink = OtherOunces * (OtherPercentAlcohol * .01);
public static double OtherDrinkFinal = OtherDrink * 1.6666666;
public static double GenderConstant = 7.5; //9 for female
public static double Weight = 180;
public static double TimeDrinking = 60;
public static double Hours = TimeDrinking / 60;
public static double Bac = ((StandardDrinks / 2) * (GenderConstant / Weight)) - (0.017 * Hours);
}
The last variable is the important part. It calculates your BAC based on the factors involved.
When I press the add beer button (Button01) I make it add 1 to StandardDrinks, simulating drinking one beer. The other variables in the Bac formula have values assigned to them in Global.java.
The code that makes the beer button do stuff is in my regular class, drinkingbuddy.java:
public class DrinkingBuddy extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button) findViewById(R.id.Button01);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Global.StandardDrinks = Global.StandardDrinks + Global.BeerDrinkFinal;
Toast.makeText(DrinkingBuddy.this, "Mmmm... Beer", Toast.LENGTH_SHORT).show();
}
});
By my perception, StandardDrinks should now have a value of 1. However, when I click the Calculate BAC button (Button05) it merely outputs the variable Bac as if StandardDrinks was still set to 0. Here is the code for the Calculate BAC button (Button05):
Button button4 = (Button) findViewById(R.id.Button05); button4.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) {
TextView texty;
texty = (TextView) findViewById(R.id.texty1);
texty.setText("Your BAC is " + Global.Bac );
}
});
It outputs the following to the text view: "Your BAC is -0.017". This is the Bac value for if StandardDrinks was still 0, so obviously there is some problem communicating between the classes. Can anyone help me??
The other elements of the formula (weight, time spent drinking, and the alcohol %'s and such) are variables because I will ultimately allow the user to change those values in the settings.
I've heard around the water cooler that global variables are not good programming style, but this is the closest I've come to getting it to work. Any other ways of doing it are very much welcomed!