Documente Academic
Documente Profesional
Documente Cultură
Calculator
Fugo Of FormGet
In this blog, we will make our First Android applications by using text views, button
and image view for background. You can learn about Views in Android from our
previous blog.
BMI Android Application will take weight and height from the users to calculate Body
Mass Index (BMI) with the information, whether user is underweight, normal or
overweight.
In previous blogs we have already learned how to make an android application project
and basic designing using views in XML that you can refer first before moving forward
in this post.
Download script LIVE DEMO & GET WP THEME
Get any desired background; copy paste your desired image in Drawable folder
then access it from @drawable/image_name.
Text views To display content like BMI Calculator, weight, height and final
result.
Edit texts To get the input values for height and weight.
After you have done the proper designing, then to implement the desired features in the
application you need to it using java, that we have explained later in this blog.
Running of application:
Download the code from the link provided above and follow the steps given in
install.txt file to run the project on your PC.
There is a .apk file given in the live demo section. You can also download that file
to run it on your Android devices (Mobile Phones).
Working:
In PC:
Right click on your project, select Run as and then select your desired device. Then
enter your height and weight, you will get your BMI below the Calculate button.
In Android Devices:
Download .apk file and install it. The app icon will start showing up on your menus
section. Run the app and follow the steps as given above.
This app will tell you that your BMI is normal or you are underweight or overweight. If
you dont enter any value in height and weight columns it will automatically ask to
input it.
XML coding
Now we will see XML coding of this application which is used for designing front view
of application.
Activity_main.xml
XML file provide basic environment or design for your android application. By using
different views and layouts you can design application of your choice. For this you just
have to know the basic XML Tags for designing interface.
<!-- Edit text for entering weight with hint in kgs -->
<EditText
android:id="@+id/et1"
android:layout_width="96dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:hint="IN KGs"
android:ems="10"
android:fadingEdgeLength="10dp"
android:inputType="numberDecimal"
android:textAlignment="center" >
<requestFocus />
</EditText>
</LinearLayout>
<!-- Linear layout ends here -->
Java codes
While we create our project at that time an XML file and a java file is created
separately. MainActivity.java is the file where we do coding for java to implement logic
and functionality in the app.
MainActivity.java
After you have designed your application using XML coding, you have to use the
JAVA code so that when Calculate button is clicked, it calculates the value and show
you the desired result. This is done after designing the user interface. Java file can be
found in your project in src folder.
import com.magnetbrains.R;
import android.os.Bundle;
import android.app.Activity;
import android.text.TextUtils;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
//Main activity class start here
public class MainActivity extends Activity {
//Define layout
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.ib1).setOnClickListener(new View.OnClickListener() {
if(TextUtils.isEmpty(str1)){
e1.setError("Please enter your weight");
e1.requestFocus();
return;
}
if(TextUtils.isEmpty(str2)){
e2.setError("Please enter your height");
e2.requestFocus();
return;
}
}
});
//Calculate BMI
private float calculateBMI (float weight, float height) {
return (float) (weight / (height * height));
}
return "Underweight";
} else if (bmiValue < 25) {
return "Normal";
} else if (bmiValue < 30) {
return "Overweight";
} else {
return "Obese";
}
}
}
Conclusion
Hope you had fun developing this app and seeing it running on your virtual device or
phone. This is just our first android application. Hope you explore more and develop
more android apps.For more updates and to learn more appliaction keep visiting our
website.