package droid.fuelConverter;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
EditText gallons;
EditText pounds;
Button clear;
Button convert;
Button setDefaultFuelGrade;
Button viewDefaultFuelGrade;
Spinner fuelGrade;
RadioButton gallonsToPounds;
RadioButton poundsToGallons;
TextView prefResult;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
prefResult = (TextView)findViewById(R.id.tvPreferenceResult);
gallons = (EditText)findViewById(R.id.etGallons);
pounds = (EditText)findViewById(R.id.etPounds);
clear = (Button)findViewById(R.id.btnClear);
clear.setOnClickListener(new OnClickListener() {
// Clear button
public void onClick(View v) {
clear();
}
});
convert = (Button)findViewById(R.id.btnConvert);
convert.setOnClickListener(new OnClickListener() {
// Convert button
public void onClick(View v) {
convert();
}
});
setDefaultFuelGrade = (Button)findViewById(R.id.btnSetDefaultFuelGrade);
setDefaultFuelGrade.setOnClickListener(new OnClickListener() {
// Set default fuel grade button
public void onClick(View v) {
switch(v.getId()) {
case R.id.btnSetDefaultFuelGrade:
Intent i = new Intent(MainActivity.this, PrefActivity.class);
startActivity(i);
break;
default:
break;
}
}
});
// View default fuel grade button
viewDefaultFuelGrade = (Button)findViewById(R.id.btnViewDefaultFuelGrade);
viewDefaultFuelGrade.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
setDefaultFuelGrade();
}
});
fuelGrade = (Spinner)findViewById(R.id.spinnerFuelType);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource
(this, R.array.FuelType, android.R.layout.simple_spinner_dropdown_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
fuelGrade.setAdapter(adapter);
gallonsToPounds = (RadioButton)findViewById(R.id.radioGallonsToPounds);
gallonsToPounds.setChecked(true);
poundsToGallons = (RadioButton)findViewById(R.id.radioPoundsToGallons);
poundsToGallons.setChecked(false);
}
private void setDefaultFuelGrade() {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
String listPrefs = prefs.getString("fuelGradePreference", "");
StringBuilder builder = new StringBuilder();
builder.append("List perference: " + listPrefs);
prefResult.setText(builder.toString());
}
/**
* Method to clear and reset the converter
*/
protected void clear() {
gallons.setText("");
pounds.setText("");
gallonsToPounds.setChecked(true);
poundsToGallons.setChecked(false);
}
/**
* Method to assign spinner value to corresponding array value and convert
*/
protected void convert() {
double fuelWeightArray[] = {6.4, 6.8, 6.7, 6.7, 6.8, 6.4, 6.7, 6.0};
int fuelWeightValue = fuelGrade.getSelectedItemPosition();
if(gallonsToPounds.isChecked() && gallons.getText().toString().equals("")) {
Toast.makeText(getBaseContext(), "Enter gallon amount", Toast.LENGTH_SHORT).show();
} else if(poundsToGallons.isChecked() && pounds.getText().toString().equals("")) {
Toast.makeText(getBaseContext(), "Enter pound amount", Toast.LENGTH_SHORT).show();
} else {
if(gallonsToPounds.isChecked()) {
double value = Double.parseDouble(gallons.getText().toString());
pounds.setText(Double.toString(value * fuelWeightArray[fuelWeightValue]));
}
if(poundsToGallons.isChecked()) {
double value = Double.parseDouble(pounds.getText().toString());
gallons.setText(Double.toString(value / fuelWeightArray[fuelWeightValue]));
}
}
}
/**
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.meunAbout:
return true;
case R.id.menuSettings:
return true;
}
return false;
}
*/
}