Simple and Useful Weight Conversion Android App
Purpose
Write a full functional Android app.
Description
When you go to a food market, you may want an app to do a weight conversion for you. This simple app takes the input value in oz. It calculates the equivalent weight in gram that matches the number in your cooking recipe.
App Image
Here is a screen shot of the app. There are two buttons and one input box. The user can enter a value in oz. The clear button is used to clear the previous result or input entry. The convert button is to display the calculated result.

Requirements
SDK: Android 4.2 (Jelly Bean) API 17
Tool: Android Studio 4.0.1
Language: Java
Download Android Studio Project with Source Code for this example
Click the link below to get the Getting Started Doc (MSFT Presentation) and the project directory.
App Design Overview
File Directory inside the project
The main Java source code is in the MainActivity file and the interface layout design is in the activity_main.xml file.

Layout Design
It is consisted of one input text box and two buttons.

Main Code (written in Java)
In the MainActivity class, three objects are declared for the code to access the three widgets (input box and two buttons) in the interface layout. A constant conversion ratio is also declared in this section of the code.
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
// the name used to refer to widgets in the layout
private EditText userInput;
private Button convertButton;
private Button clearButton;
private static final float convertRatio = 28.3495f; // oz to lb
After this declaration, the onCreate method contains all the app code. First, the three widget objects (userInput, convertButton, clearButton) are instantiated. Then, two onClickLister methods are defined to execute some codes when the buttons are pressed.
For debugging purpose, the Log.d() calls are used to display the flow of the code and the value of the variables during the program run in the Logcat window.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(TAG, "onCreate: in.");
userInput = (EditText) findViewById(R.id.editText);
convertButton = (Button) findViewById(R.id.convert);
clearButton = (Button) findViewById(R.id.clear);
View.OnClickListener ourOnClickListener = new View.OnClickListener() {
@Override
public void onClick(View view) {
String userText = userInput.getText().toString();
Log.d(TAG, "onClick convert button: in.");
Log.d(TAG, "onClick convert button: User Input Text is " + userText);
try {
float val_oz = Float.valueOf(userText);
Log.d(TAG, "onClick convert button: User Input weigh is " + val_oz + " oz.");
float val_gram = val_oz * convertRatio;
val_gram = Math.round(val_gram * 100.0f) / 100.0f; // 2 decimal points round off
Log.d(TAG, "onClick convert button: User Input weigh is " + val_gram + " gram.");
userInput.setText(String.valueOf(val_oz) + " oz = " + String.valueOf(val_gram) + " gram");
} catch (NumberFormatException e) {
Log.d(TAG, "onClick convert button: User Input num is not a float number.");
// System.out.println("Exception thrown :" + e);
}
Log.d(TAG, "onClick convert button: in.");
}
};
View.OnClickListener clearOnClickListener = new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.d(TAG, "onClick clear button: in.");
userInput.setText("");
Log.d(TAG, "onClick clear button: out.");
}
};
convertButton.setOnClickListener(ourOnClickListener);
clearButton.setOnClickListener(clearOnClickListener);
Log.d(TAG, "onCreate: out.");
}
Debugging Window during Run
As the app is run, the debugging message is displayed to inform which part of the code is running at the time. Also, the user input is being displayed and the calculated result is also shown afterward. This is useful for debugging the source code if the app is not working properly.


Conclusion
The app design is very simple. The function of the app is practical because this clean interface is easier to use than with a web page on the phone. The big text are easy to read. The app can easily be expanded for doing other types of conversion like length metric conversion or currency conversion.
Tags: