Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 842057
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T05:53:58+00:00 2026-05-15T05:53:58+00:00

Before I delve into it, I’m very new to Android and I have just

  • 0

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: https://i.stack.imgur.com/jyEjv.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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T05:53:59+00:00Added an answer on May 15, 2026 at 5:53 am

    There are some logical errors in your program.

    public static double Bac = ((StandardDrinks / 2) * (GenderConstant / Weight)) - (0.017 * Hours);
    

    This variable Bac would be initialized with the value of the formula. The further changes in the variables used in the formula to calculate BAC wouldn’t reflect back to it unless you explicitly do so. I would recommend having a function to update BAC as follows

    You could do all the above in a single activity.

           public class DrinkingBuddy extends Activity {
    /** Called when the activity is first created. */
    
     double StandardDrinks = 0;
     double BeerOunces = 12;
    double BeerPercentAlcohol = .05;
     double BeerDrink = BeerOunces * BeerPercentAlcohol;
     double BeerDrinkFinal = BeerDrink * 1.6666666;
     double ShotOunces = 1.5;
     double ShotPercentAlcohol = .4;
     double ShotDrink = ShotOunces * ShotPercentAlcohol;
     double ShotDrinkFinal = ShotDrink * 1.6666666;
     double WineOunces = 5;
     double WinePercentAlcohol = .12;
    double WineDrink = WineOunces * WinePercentAlcohol;
    double WineDrinkFinal = WineDrink * 1.6666666;
     double OtherOunces;
     double OtherPercentAlcohol;
     double OtherDrink = OtherOunces * (OtherPercentAlcohol * .01);
     double OtherDrinkFinal = OtherDrink * 1.6666666;
     double GenderConstant = 7.5; //9 for female
     double Weight = 180;
     double TimeDrinking = 60;
     double Hours = TimeDrinking / 60;
     double Bac;
    
    
    @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) {
                StandardDrinks = StandardDrinks + BeerDrinkFinal;
                Toast.makeText(DrinkingBuddy.this, "Mmmm... Beer", Toast.LENGTH_SHORT).show();
    
            }
            });
    
    Button button4 = (Button) findViewById(R.id.Button05); 
    button4.setOnClickListener(new OnClickListener() 
    { 
    @Override 
    public void onClick(View v) {
    
                TextView texty;
                Bac = ((StandardDrinks / 2) * (GenderConstant / Weight)) - (0.017 * Hours);
                texty = (TextView) findViewById(R.id.texty1);
                texty.setText("Your BAC is " + Bac );
    
            }
            });
    

    MY CODE IS VERY MESSY.

    Very messy indeed. I had a hard time removing all the unnecessary public and static declarations. This document may help on conventions to follow while writing java code.

    As per quoted from that document

    Variables should be initialized where
    they are declared and they should be
    declared in the smallest scope
    possible.

    Hope it helps a bit in the right direction.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Before I delve into some serious lowlevel debugging, I was just wondering if there
Before I delve into the abyss of Microsoft documentation any deeper, I'd like to
Before I start let me just say that I'm really new to programming so
Before I get started on a very large project for creating audiovisual presentations in
Before asking the question let me preface with the fact that I am new
Before asking my question, let me explain the context. CONTEXT: I have a web
(Before I start, yes I have asked a similar question before; unfortunately due to
Before saying anything I have to say that, albeit I'm an experienced programmer in
Before I ask the question let me state that I have attempted to google
I've never touched threads before, and by the looks of it I might have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.