• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Wrote my first app, critique please

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.

<?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"
android:orientation="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;
}
};
}
 
Looks good. My only question:

<EditTextandroid:id="@+id/FahrenheitTextbox"

android:layout_height="wrap_content"
android:layout_width="match_parent"
android:inputType="number">
</EditText>

Does setting the inputType to number allow for decimal numbers? If so, I would recommend changing your int variables to doubles, to avoid crashes.
 
You are a good point, but I am having trouble getting the double to work. So I did this instead, only allowing for whole numbers positive and negative.

<EditTextandroid:id="@+id/FahrenheitTextbox"

android:layout_height="wrap_content"
android:layout_width="match_parent"
android:inputType="number|numberSigned">
</EditText>

Can you offer ideas how to change the code for doubles please. I tried changing the int to double in the mhandler section but got a crash.
 
android:inputType="numberDecimal|numberSigned"

If this continues to crash, what line does your logcat say is causing the crash?
 
I changed the input to number|numberSigned|numberDecimal and it let me input decimal. So I changed the code in the activity to read this
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 {
double degC = Integer.valueOf(textc.getText().toString());
double 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 {
double degF = Integer.valueOf(textf.getText().toString());
double degC = (((degF - 32) * 5) / 9);
textc.setText(String.valueOf(degC));
return;
}
}
};

Where it reads double it used to read int. I get a crash when I hit the convert button.
 
I believe the issue is that while you have declared the variable to be a double, it's still being parsed as an integer.

Try:

Code:
double degC = Double.parseDouble(textc.getText().toString());

I don't know if the .toString() is necessary, but it shouldn't hurt.
 
Thank you for your help Swizz. Btw is there a way of limiting the number of decimal places shown in the convertion. I would like to limit the placement to 1 decimal place.
 
Back
Top Bottom