Advertisement
Guest User

MainActivity.java

a guest
Nov 1st, 2014
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.06 KB | None | 0 0
  1. package com.example.android.converter;
  2.  
  3. import android.support.v7.app.ActionBarActivity;
  4. import android.os.Bundle;
  5. import android.util.Log;
  6. import android.view.KeyEvent;
  7. import android.view.Menu;
  8. import android.view.MenuItem;
  9. import android.view.View;
  10. import android.widget.AdapterView;
  11. import android.widget.ArrayAdapter;
  12. import android.widget.EditText;
  13. import android.widget.Spinner;
  14. import android.widget.TextView;
  15.  
  16. import org.w3c.dom.Text;
  17.  
  18. import java.text.DecimalFormat;
  19.  
  20.  
  21. public class MainActivity extends ActionBarActivity {
  22.  
  23.     // Quantities representing 1 tsp (teaspoon)
  24.     private static final double tsp= 1.0d;
  25.     private static final double tbsp= 0.3333d;
  26.     private static final double cup=0.0208d;
  27.     private static final double oz=0.1666d;
  28.     private static final double pint=0.0104d;
  29.     private static final double quart=0.0052d;
  30.     private static final double gallon=0.0013d;
  31.     private static final double pound=0.0125d;
  32.     private static final double ml=4.9289d;
  33.     private static final double liter=0.0049d;
  34.     private static final double mg=5687.5d;
  35.     private static final double kg=0.0057d;
  36.  
  37.     @Override
  38.     protected void onCreate(Bundle savedInstanceState) {
  39.         super.onCreate(savedInstanceState);
  40.         setContentView(R.layout.activity_main);
  41. //
  42.         //
  43.         Spinner spinner = (Spinner) findViewById(R.id.unit_spinner);
  44.         // Create an ArrayAdapter using the string array and a default spinner layout
  45.         ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
  46.                 R.array.units_array, android.R.layout.simple_spinner_item);
  47.         // Specify the layout to use when the list of choices appears
  48.         adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  49.         // Apply the adapter to the spinner
  50.         spinner.setAdapter(adapter);
  51.  
  52.         //actions
  53.  
  54.         spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
  55.             @Override
  56.             public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
  57.                 String unitFrom = adapterView.getItemAtPosition(i ).toString();
  58.                 EditText inputValue = (EditText) findViewById(R.id.input_edit_text);
  59.                 Double val = new Double(inputValue.getText().toString());
  60.                 performCalculations(unitFrom,val);
  61.             }
  62.  
  63.  
  64.             @Override
  65.             public void onNothingSelected(AdapterView<?> adapterView) {
  66.  
  67.             }
  68.         });
  69.  
  70.         // EditText "Done" event will perform calculations as well
  71.         final EditText inputEditText = (EditText) findViewById(R.id.input_edit_text);
  72.         inputEditText.setOnKeyListener(new View.OnKeyListener() {
  73.             public boolean onKey(View v, int keyCode, KeyEvent event) {
  74.                 if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
  75.                     Spinner spinner = (Spinner) findViewById(R.id.unit_spinner);
  76.                     String unitFrom = spinner.getSelectedItem().toString();
  77.                     EditText inputValue = (EditText) findViewById(R.id.input_edit_text);
  78.                     Double val = new Double(inputValue.getText().toString());
  79.                     performCalculations(unitFrom,val );
  80.                     return true;
  81.                 }
  82.                 return false;
  83.             }
  84.         });
  85.     }
  86.  
  87.       /**
  88.      * It checks if its not teaspoon then convert the source value
  89.      * to teaspoon and forward to the method that updates all TextViews
  90.      * @param unitFrom
  91.      * @param val
  92.      */
  93.     private void performCalculations(String unitFrom,double val)
  94.     {
  95.  
  96.         if(!unitFrom.equals("tsp"))
  97.         {
  98.             if(unitFrom.equals("kg"))
  99.             {
  100.                 val=val*(1/kg);
  101.             }else if(unitFrom.equals("tbsp"))
  102.             {
  103.                 val=val*(1/tbsp);
  104.             }
  105.             else if(unitFrom.equals("pound"))
  106.             {
  107.                 val=val*(1/pound);
  108.             }
  109.             else if(unitFrom.equals("ml"))
  110.             {
  111.                 val=val*(1/ml);
  112.             }
  113.             else if(unitFrom.equals("liter"))
  114.             {
  115.                 val=val*(1/liter);
  116.             }
  117.             else if(unitFrom.equals("cup"))
  118.             {
  119.                 val=val*(1/cup);
  120.             }
  121.             else if(unitFrom.equals("oz"))
  122.             {
  123.                 val=val*(1/oz);
  124.             }
  125.             else if(unitFrom.equals("pint"))
  126.             {
  127.                 val=val*(1/pint);
  128.             }else if(unitFrom.equals("gallon"))
  129.             {
  130.                 val=val*(1/gallon);
  131.             }
  132.             else if(unitFrom.equals("pound"))
  133.             {
  134.                 val=val*(1/pound);
  135.             }
  136.             else if(unitFrom.equals("mg"))
  137.             {
  138.                 val=val*(1/mg);
  139.             }
  140.         }
  141.         // Converts all measures
  142.         convertFromTeaspoonToAll(val);
  143.     }
  144.  
  145.     /**
  146.      *  Converts a value from teaspoon to all types
  147.      * @param val value to be converted
  148.      */
  149.     private void convertFromTeaspoonToAll(double val) {
  150.         double converted;
  151.  
  152.         TextView textViewVal;
  153.  
  154.         textViewVal = (TextView) findViewById(R.id.kg_val_text_view);
  155.         converted = val*kg;
  156.         trimToDecimals(textViewVal,converted);
  157.  
  158.         textViewVal = (TextView) findViewById(R.id.tsp_val_text_view);
  159.         converted = val*tsp;
  160.         trimToDecimals(textViewVal,converted);
  161.  
  162.         textViewVal= (TextView) findViewById(R.id.tbsp_val_text_view);
  163.         converted = val*tbsp;
  164.         trimToDecimals(textViewVal,converted);
  165.  
  166.         textViewVal = (TextView) findViewById(R.id.pound_val_text_view);
  167.         converted = val*pound;
  168.         trimToDecimals(textViewVal,converted);
  169.  
  170.         textViewVal = (TextView) findViewById(R.id.gallon_val_text_view);
  171.         converted = val*gallon;
  172.         trimToDecimals(textViewVal,converted);
  173.  
  174.         textViewVal = (TextView) findViewById(R.id.quart_val_text_view);
  175.         converted = val*quart;
  176.         trimToDecimals(textViewVal,converted);
  177.  
  178.         textViewVal = (TextView) findViewById(R.id.mg_val_text_view);
  179.         converted = val*mg;
  180.         trimToDecimals(textViewVal,converted);
  181.  
  182.         textViewVal = (TextView) findViewById(R.id.liter_val_text_view);
  183.         converted = val*liter;
  184.         trimToDecimals(textViewVal,converted);
  185.  
  186.         textViewVal = (TextView) findViewById(R.id.cup_val_text_view);
  187.         converted = val*cup;
  188.         trimToDecimals(textViewVal,converted);
  189.  
  190.         textViewVal = (TextView) findViewById(R.id.oz_val_text_view);
  191.         converted = val*oz;
  192.         trimToDecimals(textViewVal,converted);
  193.  
  194.         textViewVal = (TextView) findViewById(R.id.pint_val_text_view);
  195.         converted = val*pint;
  196.         trimToDecimals(textViewVal,converted);
  197.  
  198.         textViewVal = (TextView) findViewById(R.id.ml_val_text_view);
  199.         converted = val*ml;
  200.         trimToDecimals(textViewVal,converted);
  201.     }
  202.  
  203.     /**
  204.      * Formats the value accordingly and updates the TextView
  205.      * @param tv
  206.      * @param value
  207.      */
  208.     private void trimToDecimals(TextView tv, double value)
  209.     {
  210.         String pattern = "##.###";
  211.         DecimalFormat decimalFormat = new DecimalFormat(pattern);
  212.         String finalValue = decimalFormat.format(value);
  213.         tv.setText(finalValue);
  214.     }
  215.  
  216.  
  217.     @Override
  218.     public boolean onCreateOptionsMenu(Menu menu) {
  219.         // Inflate the menu; this adds items to the action bar if it is present.
  220.         getMenuInflater().inflate(R.menu.main, menu);
  221.         return true;
  222.     }
  223.  
  224.     @Override
  225.     public boolean onOptionsItemSelected(MenuItem item) {
  226.         // Handle action bar item clicks here. The action bar will
  227.         // automatically handle clicks on the Home/Up button, so long
  228.         // as you specify a parent activity in AndroidManifest.xml.
  229.         int id = item.getItemId();
  230.         if (id == R.id.action_settings) {
  231.             return true;
  232.         }
  233.         return super.onOptionsItemSelected(item);
  234.     }
  235. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement