Build an Android Project using NetBeans (1) After install ...

ohiofulvousSoftware and s/w Development

Aug 15, 2012 (4 years and 11 months ago)

292 views

B
uild an Android
P
roject

using Net
B
eans



(1) After install and configure the
Android SDK and NetBeans, we can build an
Android Project. Open NetBeans, create a new project( choose Android, Android
Project)




Type in the following informations, click

Manage Android SDK



Type in your own SDK

s address, and click OK.


Then choose Android 2.3.3, click finish.


First, right click on the project
-
> click

build

, you will see the errors are gone and a
new file named

R.java


was generated.
This step must

be the first!




Put the following code into Resources
-
>values
-
> strings.xml


<?xml version="1.0" encoding="utf
-
8"?>

<resources>


<string name="hello">Hello World, Convert!</string>


<string name="app_name">Temperature Converter</string>


<color
name="myColor">#3399CC</color>


<string name="myClickHandler">myClickHandler</string>


<string name="celsius">to Celsius</string>


<string name="fahrenheit">to Fahrenheit</string>


<string name="calc">Calculate</string>

</resources>


Put the following code

into Resources
-
>layout
-
>main.xml



<?xml version="1.0" encoding="utf
-
8"?>

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"


android:orientation="vertical"
android:layout_width="fill_parent"


android:layout_height="fill_parent"
andr
oid:background="@color/myColor">


<EditText android:layout_height="wrap_content"
android:id="@+id/editText1"



android:layout_width="match_parent"
android:inputType="numberDecimal|numberSigned"></EditText>


<RadioGroup android:layout_height="wrap_content"
android:id="@+id/radioGroup1"



android:layout_width="match_parent">



<RadioButton android:layout_width="wrap_content"




android:id="@+id/radio0"
android:layout_height="wrap_content"




android:text="@string/celsius"
android:checked="true"></RadioButton>



<RadioButton android:layout_width="wrap_content"




android:id="@+id/radio1"
android:layout_height="wrap_content"




android:text="@string/fahrenheit"></RadioButton>


</RadioGroup>


<Button android:id="@+id/button1"
android:layout_width="wrap_content"



android:layout_height="wrap_content"
android:text="@string/calc"



android:onClick="myClickHandler"></Button>

</LinearLayout>



Put the following code into Convert.java


package com.temperature;


import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.widget.EditText;

import android.widget.RadioButton;

import android.widget.Toast;


public class Convert extends Activity {


private EditText text;



@Override


public void onCreate(Bundle savedInstanceState) {



supe
r.onCreate(savedInstanceState);



setContentView(R.layout.main);



text = (EditText) findViewById(R.id.editText1);



}



// This method is called at button click because we assigned the name to the


// "On Click property" of the button


public void myClick
Handler(View view) {



switch (view.getId()) {



case R.id.button1:




RadioButton celsiusButton = (RadioButton) findViewById(R.id.radio0);




RadioButton fahrenheitButton = (RadioButton)
findViewById(R.id.radio1);




if (text.getText().length() == 0) {





Toast.makeText(this, "Please enter a valid number",







Toast.LENGTH_LONG).show();





return;




}





float inputValue = Float.parseFloat(text.getText().toString());




if (celsiusButton.isChecked()) {





text.setText(String







.valueOf(convertFa
hrenheitToCelsius(inputValue)));




} else {





text.setText(String







.valueOf(convertCelsiusToFahrenheit(inputValue)));




}




// Switch to the other button




if (fahrenheitButton.isChecked()) {





fahrenheitButton.setChecked(false);





celsiusBu
tton.setChecked(true);




} else {





fahrenheitButton.setChecked(true);





celsiusButton.setChecked(false);




}




break;



}


}



// 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;


}

}



Then build the project and run

If you see the screen to be black for a long time, please close the
emulator

and run it
a
gain.






Then you will see the following window. After seeing that, you need to click the run
button in NetBeans again to load the project into emulator.

Then use the emulator
phone to a
ccess the Temperature software.


It will transfer temperature
value either from C to F, or F to C