Advertisement
Kaidul

My first Android App

Dec 7th, 2012
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.66 KB | None | 0 0
  1. package convert.android;
  2.  
  3. import temparature.converter.R;
  4. import android.app.Activity;
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.widget.EditText;
  8. import android.widget.RadioButton;
  9. import android.widget.Toast;
  10.  
  11. public class temparatureActivity extends Activity {
  12.     private EditText text;
  13.     /** Called when the activity is first created. */
  14.     @Override
  15.     public void onCreate(Bundle savedInstanceState) {
  16.         super.onCreate(savedInstanceState);
  17.         setContentView(R.layout.main);
  18.         text = (EditText) findViewById(R.id.textField);
  19.     }
  20.    
  21.     public void onClick(View view) {
  22.         switch (view.getId()) {
  23.         case R.id.button1 :
  24.             RadioButton celsiusButton = (RadioButton) findViewById(R.id.radioButton1);
  25.             RadioButton fahrenheitButton = (RadioButton) findViewById(R.id.radioButton2);
  26.             if(text.getText().length() == 0) {
  27.                 Toast.makeText(this, "Please Enter a Valid Number", Toast.LENGTH_LONG).show();
  28.                 return;
  29.             }
  30.            
  31.             float inputValue = Float.parseFloat(text.getText().toString());
  32.             if (celsiusButton.isChecked()) {
  33.                 text.setText(String.valueOf(FahrenheitToCelsius(inputValue)));
  34.                 celsiusButton.setChecked(false);
  35.                 fahrenheitButton.setChecked(true);
  36.             } else {
  37.                 text.setText(String.valueOf(CelsiusToFahrenheit(inputValue)));
  38.                 fahrenheitButton.setChecked(false);
  39.                 celsiusButton.setChecked(true);
  40.             }
  41.             break;
  42.         }
  43.     }
  44.    
  45.     private float CelsiusToFahrenheit(float celsius) {
  46.         return ((celsius*9)/5) + 32;
  47.     }
  48.    
  49.     private float FahrenheitToCelsius(float fahrenheit) {
  50.         return ((fahrenheit-32)*5) / 9;
  51.     }
  52.    
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement