I am working on temperature conversion app,
My requirement is i have two text fields Celsius and Fahrenheit 1. when i enter a value in Celsius text field it have to convert into Fahrenheit
and
- when i enter a value in Fahrenheit text field it have to convert into Celsius
i tried on a single item i.e. when i try on a single field but not both
here goes my code
My XML code.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://ift.tt/nIICcg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Celsius"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/et_cel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="numberSigned" >
<requestFocus />
</EditText>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Forenheit"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/et_for"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="numberSigned" />
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="go_reset"
android:text="Reset" />
</LinearLayout>
My java code
EditText cel, fore;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.type3);
cel = (EditText) findViewById(R.id.et_cel);
fore = (EditText) findViewById(R.id.et_for);
cel.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 = cel.getText().toString();
if (TextUtils.isEmpty(inputs)) {
cel.setText("");
} else {
try {
float i = Float.parseFloat(inputs);
i = ((i * 9) / 5) + 32;
fore.setText(String.valueOf(i));
} catch (Exception e) {
// TODO: handle exception
cel.setText("");
}
}
}
});
fore.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 = fore.getText().toString();
if (TextUtils.isEmpty(inputs)) {
cel.setText("");
} else {
try {
float i = Float.parseFloat(inputs);
i = ((i - 32) * 5 / 9);
cel.setText(String.valueOf(i));
} catch (Exception e) {
// TODO: handle exception
cel.setText("");
}
}
}
});
}
@Override
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
Intent i = new Intent(this, Home.class);
startActivity(i);
}
public void go_reset(View v) {
cel.setText("");
fore.setText("");
}
public void go() {
}
No comments:
Post a Comment