Hi all,
I'm trying to write an app that converts celsius to farenheit and vice versa.
I'm stuck on the input value for data validation. eg. test if field is empty or invalid char.
then catch the error, but when using eclipse i was trying to use .isempty() but it crashes on my phone when i deploy. so i have removed that and would appreciate some assistance.
It did say api level 9 when i was trying to call the above method but it's still not working even though eclipse said it was depricated.
i think i had it at 2.2 in eclipse.
i studied java years ago and it is fairly straight forward but i really am stuck on the data validation. i've searched this but not found anything that will help.
any ideas would be greatly appreciated. thank you
EditText i'm using has this param:
android:inputType="numberDecimal|numberSigned"
ACTIVITY_MAIN.XML IS THIS:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<TableRow>
<ImageView android:background="@drawable/launcher"/>
<TextView
android:text="Android C2F Converter" />
</TableRow>
<TableRow>
<EditText
android:id="@+id/editTextInputValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="Input value to convert here..."
android:inputType="numberDecimal|numberSigned"
android:imeOptions="actionDone">
<requestFocus />
</EditText>
</TableRow>
<TableRow>
<RadioGroup
android:id="@+id/radioGroupCelsiusFarenheit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="38dp"
android:layout_below="@id/editTextInputValue"
android:contentDescription="Convert to..."
android

rientation="horizontal">
<RadioButton
android:id="@+id/radioCelsius"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android

nClick="onRadioButtonClicked"
android:text="Celsius" />
<RadioButton
android:id="@+id/radioFarenheit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android

nClick="onRadioButtonClicked"
android:text="Farenheit" />
</RadioGroup>
</TableRow>
<TableRow>
<TextView
android:id="@+id/editTextResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editTextInputValue"
android:layout_marginTop="24dp"
android:ems="10"
android:layout_below="@id/radioGroupCelsiusFarenheit"
android:text="Result:" />
</TableRow>
<TableRow>
<Button
android:id="@+id/btnResetUI"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editTextResult"
android:layout_centerHorizontal="true"
android:layout_marginTop="46dp"
android:text="Reset" />
</TableRow>
</TableLayout>
END ACTIVITY_MAIN.XML
MAINACTIVITY.JAVA IS THIS:
package is something...
import android.os.Bundle;
import android.app.Activity;
//import android.text.TextUtils;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
//import android.widget.Toast;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button button = (Button) findViewById(R.id.btnResetUI);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
resetUI(view);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public void onRadioButtonClicked(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch(view.getId()) {
case R.id.radioCelsius:
if (checked)
convertFarenheitToCelsius(view);
break;
case R.id.radioFarenheit:
if (checked)
convertCelsiusToFarenheit(view);
break;
}
}
public void convertCelsiusToFarenheit(View view)
{
EditText number1Text = (EditText) findViewById(R.id.editTextInputValue);
String number1String = number1Text.getText().toString();
double number1D = Double.parseDouble(number1String);
if(number1D <= -273.16)
{
Toast.makeText(this, "Absolute zero. Invalid C value.", Toast.LENGTH_SHORT).show();
resetUI(view);
}
else
{
double result = number1D * 1.8;
result = result + 32;
String resultString = "Result: " + result + " " + "\u2109";
TextView resultText = (TextView) findViewById(R.id.editTextResult);
resultText.setText(resultString);
}
}
public void convertFarenheitToCelsius(View view) {
EditText number2Text = (EditText) findViewById(R.id.editTextInputValue);
String number2String = number2Text.getText().toString();
Toast.makeText(this, number2String, Toast.LENGTH_SHORT).show();
double number2D = Double.parseDouble(number2String);
if(number2D <= -459.68)
{
Toast.makeText(this, "Absolute zero. Invalid F value.", Toast.LENGTH_SHORT).show();
resetUI(view);
}
else
{
double resultCelcius = number2D - 32;
resultCelcius = resultCelcius / 1.8;
String resultCelciusString = "Result: " + resultCelcius + " " + "\u2103";
TextView resultCelciusText = (TextView) findViewById(R.id.editTextResult);
resultCelciusText.setText(resultCelciusString);
}
}
public void resetUI(View view) {
RadioGroup radioGroup = (RadioGroup)findViewById(R.id.radioGroupCelsiusFarenheit);
radioGroup.clearCheck();
EditText etInput = (EditText)findViewById(R.id.editTextInputValue);
etInput.setText("");
TextView resultText = (TextView) findViewById(R.id.editTextResult);
resultText.setText("Result:");
}
}
END MAINACTIVITY.JAVA
Anyway i would really appreciate some help from the global community please.