I just started learning Java/Android programming.
This is my first basic program and it is working fine now. Thanks to the Stack overflow folks for helping me out. Now it would be really helpful if you can help me in tuning this program, it will give me better understanding of the basic structure of Android programming.
Here is the code:
(Brief: It will convert Celsius to Fahrenheit and vice versa as you type..)
package mag.com.myhelloworldapp;
import android.os.Bundle;
import android.app.Activity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.widget.EditText;
public class MainActivity extends Activity {
private EditText celsiusText;
private EditText farenheitText;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
celsiusText = (EditText) findViewById(R.id.editText1);
farenheitText = (EditText) findViewById(R.id.editText2);
celsiusText.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
((EditText)findViewById(R.id.editText2)).setText("");
((EditText)findViewById(R.id.editText1)).setText("");
}
});
farenheitText.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
((EditText)findViewById(R.id.editText2)).setText("");
((EditText)findViewById(R.id.editText1)).setText("");
}
});
farenheitText.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence S, int start, int before, int count) {
if (farenheitText.hasFocus()) {
float inputValue;
if (!S.toString().equals("")) {
inputValue = Float.parseFloat(S.toString());
((EditText)findViewById(R.id.editText1)).setText(String.valueOf(convertFahrenheitToCelsius(inputValue)));
} else {
((EditText)findViewById(R.id.editText1)).setText("");
return;
}
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
celsiusText.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) {
if (celsiusText.hasFocus()) {
float inputValue;
if (!S.toString().equals("")) {
inputValue = Float.parseFloat(S.toString());
((EditText)findViewById(R.id.editText2)).setText(String.valueOf(convertCelsiusToFahrenheit(inputValue)));
} else {
((EditText)findViewById(R.id.editText2)).setText("");
return;
}
}
}
});
}
//Converts to celsius
private float convertFahrenheitToCelsius(float fahrenheit) {
return ((fahrenheit - 32) * 5 / 9);
}
// Converts to fahrenheit
private float convertCelsiusToFahrenheit(float celsius) {
return ((celsius * 9) / 5) + 32;
}
}
This is activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp" >
<EditText
android:id="@+id/editText1"
android:layout_width="128dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="62dp"
android:ems="10"
android:inputType="numberSigned" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/editText2"
android:layout_width="128dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/editText1"
android:layout_alignBottom="@+id/editText1"
android:layout_alignParentRight="true"
android:ems="10"
android:inputType="numberSigned" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_below="@+id/editText1"
android:layout_marginTop="18dp"
android:text="@string/celsius"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView1"
android:layout_alignBottom="@+id/textView1"
android:layout_alignRight="@+id/editText2"
android:text="@string/fahrenheit"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>