I am working on a requirement, where i have two text fields when i enter a Celsius value in first text field automatically it converts into Fahrenheit
The problem i'm facing is when ever i entering a negative value app is crashing
Here is my code
My XML file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://ift.tt/nIICcg"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="20dp" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Celsius" />
<EditText
android:id="@+id/myTextBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberSigned" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Forenheight" />
<EditText
android:id="@+id/outputBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberSigned" />
<TextView
android:id="@+id/tv_in_for"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Forenheit" />
<EditText
android:id="@+id/et_in_for"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberSigned" />
<TextView
android:id="@+id/tv_out_cel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Celsius" />
<EditText
android:id="@+id/et_out_cel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberSigned" />
</LinearLayout>
This is my java code
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.type2);
final EditText outputTextBox = (EditText) findViewById(R.id.outputBox);
final EditText myTextBox = (EditText) findViewById(R.id.myTextBox);
myTextBox.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
String inputs = myTextBox.getText().toString();
if (TextUtils.isEmpty(inputs)) {
outputTextBox.setText("0");
} else {
float i = Float.parseFloat(inputs);
i= ((i * 9) / 5) + 32;
outputTextBox.setText(String.valueOf(i));
}
}
});
final EditText in_for = (EditText) findViewById(R.id.et_in_for);
final EditText out_cel = (EditText) findViewById(R.id.et_out_cel);
in_for.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
String inputs = in_for.getText().toString();
if (TextUtils.isEmpty(inputs)) {
in_for.setText("0");
} else {
float i = Float.parseFloat(inputs);
i= ((i - 32) * 5 / 9);
out_cel.setText(String.valueOf(i));
}
}
});
}
please Help me out in this.
No comments:
Post a Comment