Advertisement
Guest User

Untitled

a guest
Mar 27th, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.56 KB | None | 0 0
  1. package com.wolska.application1;
  2.  
  3. import android.net.Uri;
  4. import android.support.v7.app.AppCompatActivity;
  5. import android.os.Bundle;
  6. import android.text.TextUtils;
  7. import android.util.Log;
  8. import android.view.LayoutInflater;
  9. import android.view.View;
  10. import android.view.ViewGroup;
  11. import android.widget.Button;
  12. import android.widget.EditText;
  13. import android.widget.TextView;
  14. import android.widget.Toast;
  15.  
  16.  
  17. import com.google.android.gms.appindexing.Action;
  18. import com.google.android.gms.appindexing.AppIndex;
  19. import com.google.android.gms.appindexing.Thing;
  20. import com.google.android.gms.common.api.GoogleApiClient;
  21.  
  22. import java.util.Locale;
  23.  
  24. import butterknife.BindView;
  25. import butterknife.ButterKnife;
  26. import butterknife.OnClick;
  27.  
  28. public class MainActivity extends AppCompatActivity {
  29.  
  30. boolean isKg = true;
  31. final float weightConversion = 2.2046f, heightConversion = 3.2808f;
  32.  
  33. private GoogleApiClient client;
  34.  
  35.  
  36. @BindView(R.id.massTV) TextView massTV;
  37. @BindView(R.id.heightTV) TextView heightTV;
  38. @BindView(R.id.unitMass) TextView unitMass;
  39. @BindView(R.id.unitHeight) TextView unitHeight;
  40. @BindView(R.id.bmi) TextView bmi;
  41. @BindView(R.id.bmiCounted) TextView bmiCounted;
  42. @BindView(R.id.units) Button units;
  43. @BindView(R.id.count) Button count;
  44. @BindView(R.id.massET) EditText massET;
  45. @BindView(R.id.heightET) EditText heightET;
  46.  
  47.  
  48.  
  49. @Override
  50. protected void onCreate(Bundle savedInstanceState) {
  51. super.onCreate(savedInstanceState);
  52. setContentView(R.layout.activity_main);
  53.  
  54. ButterKnife.bind(this);
  55.  
  56.  
  57.  
  58.  
  59.  
  60. // ATTENTION: This was auto-generated to implement the App Indexing API.
  61. // See https://g.co/AppIndexing/AndroidStudio for more information.
  62. client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
  63. }
  64.  
  65.  
  66. public void changeValueFromKgMtoLbFt(String m, String h) {
  67. float mass, height;
  68. if (massET.getText().length() != 0) {
  69. mass = Float.parseFloat(m.toString()) * weightConversion;
  70. String massS = String.format(Locale.US, "%.2f", mass);
  71. massET.setText(massS);
  72. }
  73. if (heightET.getText().length() != 0) {
  74. height = Float.parseFloat(h.toString()) * heightConversion;
  75. String heightS = String.format(Locale.US, "%.2f", height);
  76. heightET.setText(heightS);
  77. }
  78. }
  79.  
  80. public void changeValueFromLbFttoKgM(String m, String h) {
  81. float mass, height;
  82. if (massET.getText().length() != 0) {
  83. mass = Float.parseFloat(m.toString()) / weightConversion;
  84. String massS = String.format(Locale.US, "%.2f", mass);
  85. massET.setText(massS);
  86. }
  87. if (heightET.getText().length() != 0) {
  88. height = Float.parseFloat(h.toString()) / heightConversion;
  89. String heightS = String.format(Locale.US, "%.2f", height);
  90. heightET.setText(heightS);
  91. }
  92. }
  93.  
  94.  
  95. @OnClick(R.id.units)
  96. public void changeUnits(){
  97. if (isKg) {
  98. unitMass.setText("lb");
  99. unitHeight.setText("ft");
  100. units.setText("kg/m");
  101. changeValueFromKgMtoLbFt(massET.getText().toString(), heightET.getText().toString());
  102. isKg = false;
  103. } else {
  104. unitMass.setText("kg");
  105. unitHeight.setText("m");
  106. units.setText("lb/ft");
  107. changeValueFromLbFttoKgM(massET.getText().toString(), heightET.getText().toString());
  108. isKg = true;
  109. }
  110. }
  111.  
  112. @OnClick(R.id.count)
  113. public void countBMI(){
  114. float mass = 0, height = 0;
  115. CountBMIforKgM countBMI = new CountBMIforKgM();
  116. if (TextUtils.isEmpty(massET.getText()) || TextUtils.isEmpty(heightET.getText())) {
  117. Toast.makeText(getApplication(), "Enter all data", Toast.LENGTH_SHORT).show();
  118. } else {
  119. if (isKg) {
  120. mass = Float.parseFloat(massET.getText().toString());
  121. height = Float.parseFloat(heightET.getText().toString());
  122. } else {
  123. mass = Float.parseFloat(massET.getText().toString()) / weightConversion;
  124. height = Float.parseFloat(heightET.getText().toString()) / heightConversion;
  125. }
  126. if (countBMI.isValidMass(mass) && countBMI.isValidHeight(height)) {
  127. float result = countBMI.countBMI(mass, height);
  128. String bmi = String.format(Locale.US, "%.2f", result);
  129. bmiCounted.setText(bmi);
  130. setBMIresultColor(result);
  131. } else {
  132. if (!countBMI.isValidMass(mass))
  133. massET.setError("Wrong mass");
  134. if (!countBMI.isValidHeight(height))
  135. heightET.setError("Wrong height");
  136. }
  137.  
  138. }
  139. }
  140.  
  141.  
  142.  
  143.  
  144.  
  145.  
  146.  
  147.  
  148. public void setBMIresultColor(float result) {
  149. if (result >= 18.5f && result < 25)
  150. bmiCounted.setTextColor(0xff00ff00);
  151. else if ((result >= 17 && result < 18.5f) || (result >= 25 && result < 30))
  152. bmiCounted.setTextColor(0xffffff00);
  153. else
  154. bmiCounted.setTextColor(0xffff0000);
  155. }
  156.  
  157.  
  158. /**
  159. * ATTENTION: This was auto-generated to implement the App Indexing API.
  160. * See https://g.co/AppIndexing/AndroidStudio for more information.
  161. */
  162. public Action getIndexApiAction() {
  163. Thing object = new Thing.Builder()
  164. .setName("Main Page") // TODO: Define a title for the content shown.
  165. // TODO: Make sure this auto-generated URL is correct.
  166. .setUrl(Uri.parse("http://[ENTER-YOUR-URL-HERE]"))
  167. .build();
  168. return new Action.Builder(Action.TYPE_VIEW)
  169. .setObject(object)
  170. .setActionStatus(Action.STATUS_TYPE_COMPLETED)
  171. .build();
  172. }
  173.  
  174. @Override
  175. public void onStart() {
  176. super.onStart();
  177.  
  178. // ATTENTION: This was auto-generated to implement the App Indexing API.
  179. // See https://g.co/AppIndexing/AndroidStudio for more information.
  180. client.connect();
  181. AppIndex.AppIndexApi.start(client, getIndexApiAction());
  182. }
  183.  
  184. @Override
  185. public void onStop() {
  186. super.onStop();
  187.  
  188. // ATTENTION: This was auto-generated to implement the App Indexing API.
  189. // See https://g.co/AppIndexing/AndroidStudio for more information.
  190. AppIndex.AppIndexApi.end(client, getIndexApiAction());
  191. client.disconnect();
  192. }
  193.  
  194.  
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement