PavementPilot
Newbie
I have played around with App Inventer, and done quite a few tutorials, so I wanted to try writing it completely on my own.
Can you please look at my code and tell me how it looks to you, and if I could have done anything in a more efficient manner.
It is a Temperature Converter for C/F. I know alot of people have done this already
Thanks.
Can you please look at my code and tell me how it looks to you, and if I could have done anything in a more efficient manner.
It is a Temperature Converter for C/F. I know alot of people have done this already
Thanks.
<?xmlversion="1.0"encoding="utf-8"?>
<manifestxmlns:android="http://schemas.android.com/apk/res/android"
package="com.pavementpilot.tempconv"
android:versionCode="1"
android:versionName="1.0">
<uses-sdkandroid:minSdkVersion="10"/>
<applicationandroid:icon="@drawable/icon"android:label="@string/app_name">
<activityandroid:name=".TempConv"
android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar">
<intent-filter>
<actionandroid:name="android.intent.action.MAIN"/>
<categoryandroid:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
<?xmlversion="1.0"encoding="utf-8"?>
<resources>
<stringname="hello">Hello World, TempConv!</string>
<stringname="app_name">TempConv</string>
<colorname="bgColor">#0000ff</color>
<stringname="ErrorMessage">Please enter a valid value</string>
<stringname="degC">0</string>
<stringname="degF">0</string>
<stringname="ClearedTextBoxes">""</string>
</resources>
<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
androidrientation="vertical"
android:background="@color/bgColor">
<TextViewandroid:id="@+id/CelsiusLabel"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="match_parent"
android:text="Celsius">
</TextView>
<EditTextandroid:id="@+id/CelsiusTextBox"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:inputType="number">
</EditText>
<LinearLayoutandroid:id="@+id/buttonLinearLayout"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_width="wrap_content">
<Buttonandroid:id="@+id/CtoFButton"
android:layout_height="wrap_content"
android:text="C to F"
android:layout_width="wrap_content">
</Button>
<Buttonandroid:id="@+id/ResetButton"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Reset">
</Button>
<Buttonandroid:id="@+id/FtoCButton"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="F to C">
</Button>
</LinearLayout>
<TextViewandroid:id="@+id/FahrenheitLabel"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="match_parent"
android:text="Fahrenheit">
</TextView>
<EditTextandroid:id="@+id/FahrenheitTextbox"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:inputType="number">
</EditText>
</LinearLayout>
package com.pavementpilot.tempconv;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class TempConv extends Activity {
public String degC;
public String degF;
Button CtoFButton;
Button FtoCButton;
Button ResetButton;
public EditText textc;
public EditText textf;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textc = (EditText) findViewById(R.id.CelsiusTextBox);
textf = (EditText) findViewById(R.id.FahrenheitTextbox);
CtoFButton = (Button) findViewById(R.id.CtoFButton);
FtoCButton = (Button) findViewById(R.id.FtoCButton);
ResetButton = (Button) findViewById(R.id.ResetButton);
CtoFButton.setOnClickListener(myhandler1);
FtoCButton.setOnClickListener(myhandler2);
ResetButton.setOnClickListener(myhandler3);
}
View.OnClickListener myhandler1 = new View.OnClickListener() {
@Override
public void onClick(View v) {
if (textc.getText().length() == 0) {
Toast.makeText(TempConv.this, R.string.ErrorMessage, Toast.LENGTH_LONG).show();
return;
} else {
int degC = Integer.valueOf(textc.getText().toString());
int degF = (((degC * 9) / 5) + 32);
textf.setText(String.valueOf(degF));
return;
}
}
};
View.OnClickListener myhandler2 = new View.OnClickListener() {
@Override
public void onClick(View v) {
if (textf.getText().length() == 0) {
Toast.makeText(TempConv.this, R.string.ErrorMessage, Toast.LENGTH_LONG).show();
return;
} else {
int degF = Integer.valueOf(textf.getText().toString());
int degC = (((degF - 32) * 5) / 9);
textc.setText(String.valueOf(degC));
return;
}
}
};
View.OnClickListener myhandler3 = new View.OnClickListener() {
@Override
public void onClick(View v) {
textc.setText(R.string.ClearedTextBoxes);
textf.setText(R.string.ClearedTextBoxes);
return;
}
};
}
rientation