Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.wolska.application1;
- import android.net.Uri;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.text.TextUtils;
- import android.util.Log;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.TextView;
- import android.widget.Toast;
- import com.google.android.gms.appindexing.Action;
- import com.google.android.gms.appindexing.AppIndex;
- import com.google.android.gms.appindexing.Thing;
- import com.google.android.gms.common.api.GoogleApiClient;
- import java.util.Locale;
- import butterknife.BindView;
- import butterknife.ButterKnife;
- import butterknife.OnClick;
- public class MainActivity extends AppCompatActivity {
- boolean isKg = true;
- final float weightConversion = 2.2046f, heightConversion = 3.2808f;
- private GoogleApiClient client;
- @BindView(R.id.massTV) TextView massTV;
- @BindView(R.id.heightTV) TextView heightTV;
- @BindView(R.id.unitMass) TextView unitMass;
- @BindView(R.id.unitHeight) TextView unitHeight;
- @BindView(R.id.bmi) TextView bmi;
- @BindView(R.id.bmiCounted) TextView bmiCounted;
- @BindView(R.id.units) Button units;
- @BindView(R.id.count) Button count;
- @BindView(R.id.massET) EditText massET;
- @BindView(R.id.heightET) EditText heightET;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- ButterKnife.bind(this);
- // ATTENTION: This was auto-generated to implement the App Indexing API.
- // See https://g.co/AppIndexing/AndroidStudio for more information.
- client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
- }
- public void changeValueFromKgMtoLbFt(String m, String h) {
- float mass, height;
- if (massET.getText().length() != 0) {
- mass = Float.parseFloat(m.toString()) * weightConversion;
- String massS = String.format(Locale.US, "%.2f", mass);
- massET.setText(massS);
- }
- if (heightET.getText().length() != 0) {
- height = Float.parseFloat(h.toString()) * heightConversion;
- String heightS = String.format(Locale.US, "%.2f", height);
- heightET.setText(heightS);
- }
- }
- public void changeValueFromLbFttoKgM(String m, String h) {
- float mass, height;
- if (massET.getText().length() != 0) {
- mass = Float.parseFloat(m.toString()) / weightConversion;
- String massS = String.format(Locale.US, "%.2f", mass);
- massET.setText(massS);
- }
- if (heightET.getText().length() != 0) {
- height = Float.parseFloat(h.toString()) / heightConversion;
- String heightS = String.format(Locale.US, "%.2f", height);
- heightET.setText(heightS);
- }
- }
- @OnClick(R.id.units)
- public void changeUnits(){
- if (isKg) {
- unitMass.setText("lb");
- unitHeight.setText("ft");
- units.setText("kg/m");
- changeValueFromKgMtoLbFt(massET.getText().toString(), heightET.getText().toString());
- isKg = false;
- } else {
- unitMass.setText("kg");
- unitHeight.setText("m");
- units.setText("lb/ft");
- changeValueFromLbFttoKgM(massET.getText().toString(), heightET.getText().toString());
- isKg = true;
- }
- }
- @OnClick(R.id.count)
- public void countBMI(){
- float mass = 0, height = 0;
- CountBMIforKgM countBMI = new CountBMIforKgM();
- if (TextUtils.isEmpty(massET.getText()) || TextUtils.isEmpty(heightET.getText())) {
- Toast.makeText(getApplication(), "Enter all data", Toast.LENGTH_SHORT).show();
- } else {
- if (isKg) {
- mass = Float.parseFloat(massET.getText().toString());
- height = Float.parseFloat(heightET.getText().toString());
- } else {
- mass = Float.parseFloat(massET.getText().toString()) / weightConversion;
- height = Float.parseFloat(heightET.getText().toString()) / heightConversion;
- }
- if (countBMI.isValidMass(mass) && countBMI.isValidHeight(height)) {
- float result = countBMI.countBMI(mass, height);
- String bmi = String.format(Locale.US, "%.2f", result);
- bmiCounted.setText(bmi);
- setBMIresultColor(result);
- } else {
- if (!countBMI.isValidMass(mass))
- massET.setError("Wrong mass");
- if (!countBMI.isValidHeight(height))
- heightET.setError("Wrong height");
- }
- }
- }
- public void setBMIresultColor(float result) {
- if (result >= 18.5f && result < 25)
- bmiCounted.setTextColor(0xff00ff00);
- else if ((result >= 17 && result < 18.5f) || (result >= 25 && result < 30))
- bmiCounted.setTextColor(0xffffff00);
- else
- bmiCounted.setTextColor(0xffff0000);
- }
- /**
- * ATTENTION: This was auto-generated to implement the App Indexing API.
- * See https://g.co/AppIndexing/AndroidStudio for more information.
- */
- public Action getIndexApiAction() {
- Thing object = new Thing.Builder()
- .setName("Main Page") // TODO: Define a title for the content shown.
- // TODO: Make sure this auto-generated URL is correct.
- .setUrl(Uri.parse("http://[ENTER-YOUR-URL-HERE]"))
- .build();
- return new Action.Builder(Action.TYPE_VIEW)
- .setObject(object)
- .setActionStatus(Action.STATUS_TYPE_COMPLETED)
- .build();
- }
- @Override
- public void onStart() {
- super.onStart();
- // ATTENTION: This was auto-generated to implement the App Indexing API.
- // See https://g.co/AppIndexing/AndroidStudio for more information.
- client.connect();
- AppIndex.AppIndexApi.start(client, getIndexApiAction());
- }
- @Override
- public void onStop() {
- super.onStop();
- // ATTENTION: This was auto-generated to implement the App Indexing API.
- // See https://g.co/AppIndexing/AndroidStudio for more information.
- AppIndex.AppIndexApi.end(client, getIndexApiAction());
- client.disconnect();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement