Recently, I got a Motorola droid and decided to make a program for the Android Operating system. However, I'm having a bit of trouble. A while back, I made a program in Java that calculates the actual storage capacity of secondary storage devices ranging from KB to YB units and displays the results in a dialog box. I'm trying to port that program to the Android, but have the results appear in a textfield (EditText widget on the Android) that is readonly. My problem is that the program does not seem to be doing anything on the emulator provided by the Android SDK, because it does not display in the textfield. What am I doing wrong?
Here is the XML that creates the GUI:
Here is the XML to control string displays:
Here is the XML to create the list for the Spinner:
Here is the Java class that contains the necessary formulas:
And here is the main source of the Android App:
Here is the new source to work with, when I tried to fix things:
Here is the XML that creates the GUI:
Code:
<?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"
>
<!-- the following creates a textfield -->
<EditText android:id="@+id/size"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=" "
/>
<!-- the following creates a combobox -->
<Spinner android:id="@+id/spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:drawSelectorOnTop="true"
android:prompt="@string/choices"
/>
<!-- the following creates another textfield -->
<EditText android:id="@+id/result"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=" "
/>
<!-- the following creates a button -->
<Button android:id="@+id/submit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/submit_text"
/>
</LinearLayout>
Here is the XML to control string displays:
Code:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Actual Storage</string>
<string name="choices">Select a unit</string>
<string name="message">Enter a Size</string>
<string name="submit_text">Calculate</string>
</resources>
Here is the XML to create the list for the Spinner:
Code:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="choices_array">
<item>KB</item>
<item>MB</item>
<item>GB</item>
<item>TB</item>
<item>PB</item>
<item>EB</item>
<item>ZB</item>
<item>YB</item>
</string-array>
</resources>
Here is the Java class that contains the necessary formulas:
Code:
package com.Actual.android;
/**
* The Storage class contains methods
* that calculate the actual storage cacpacity
* of secondary storage devices, without taking
* Filesystems into account.
*/
public class Storage
{
double ListedStorage;
/**
* The setStorage method accepts an argument stored in the * ListedStorage field.
*/
public void setStorage(double store)
{
ListedStorage = store;
}
/**
* The getStorage method returns the value of the value stored in
* the ListedStorage field.
*/
public double getStorage()
{
return ListedStorage;
}
/**
* The getKB method calculates the actual storage
* in the Kilobytes unit by dividing the product of the
* ListedStorage field and 1,000 by 1024.
*/
public double getKB()
{
return (ListedStorage * 1000)/1024;
}
/**
* The getMB method calculates the actual storage
* in the Megabytes unit by dividing the product of the
* ListedStorage field and 1,000^2 by 1024^2.
*/
public double getMB()
{
return (ListedStorage * Math.pow(1000, 2))/Math.pow(1024, 2);
}
/**
* The getGB method calculates the actual storage
* in the Gigabytes unit by dividing the product of the
* ListedStorage field and 1,000^3 by 1024^3.
*/
public double getGB()
{
return (ListedStorage * Math.pow(1000, 3))/Math.pow(1024, 3);
}
/**
* The getTB method calculates the actual storage
* in the Terabytes unit by dividing the product of the
* ListedStorage field and 1,000^4 by 1024^4.
*/
public double getTB()
{
return (ListedStorage * Math.pow(1000, 4))/Math.pow(1024, 4);
}
/**
* The getPB method calculates the actual storage
* in the Petabytes unit by dividing the product of the
* ListedStorage field and 1,000^5 by 1024^5.
*/
public double getPB()
{
return (ListedStorage * Math.pow(1000, 5))/Math.pow(1024, 5);
}
/**
* The getEB method calculates the actual storage
* in the Exabytes unit by dividing the product of the
* ListedStorage field and 1,000^6 by 1024^6.
*/
public double getEB()
{
return (ListedStorage * Math.pow(1000, 6))/Math.pow(1024, 6);
}
/**
* The getZB method calculates the actual storage
* in the Zetabytes unit by dividing the product of the
* ListedStorage field and 1,000^7 by 1024^7.
*/
public double getZB()
{
return (ListedStorage * Math.pow(1000, 7))/Math.pow(1024, 7);
}
/**
* The getYB method calculates the actual storage
* in the Yottabytes unit by dividing the product of the
* ListedStorage field and 1,000^8 by 1024^8.
*/
public double getYB()
{
return (ListedStorage * Math.pow(1000, 8))/Math.pow(1024, 8);
}
}
And here is the main source of the Android App:
Code:
package com.Actual.android;
import android.app.Activity;
import android.os.Bundle;
import android.widget.*;
import android.view.*;
public class ActualStorageActivity extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Spinner selection = (Spinner)findViewById(R.id.spinner);
ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.choices_array, android.R.layout.simple_spinner_dropdown_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
selection.setAdapter(adapter);
EditText size = (EditText)findViewById(R.id.size);
EditText result = (EditText)findViewById(R.id.result);
result.setCursorVisible(false);
Button calculate = (Button)findViewById(R.id.submit);
calculate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
EditText size = (EditText)findViewById(R.id.size);
EditText result = (EditText)findViewById(R.id.result);
Storage capacity = new Storage();
String initial = size.getText().toString();
String unit = (String)selection.getSelectedItem();
String end;
double convert = Double.parseDouble(initial);
capacity.setStorage(convert);
if (unit == "KB")
{
end = Double.toString(capacity.getKB());
result.setText(end);
}
else if (unit == "MB")
{
end = Double.toString(capacity.getMB());
result.setText(end);
}
else if (unit == "GB")
{
end = Double.toString(capacity.getGB());
result.setText(end);
}
else if (unit == "TB")
{
end = Double.toString(capacity.getTB());
result.setText(end);
}
else if (unit == "PB")
{
end = Double.toString(capacity.getPB());
result.setText(end);
}
else if (unit == "EB")
{
end = Double.toString(capacity.getEB());
result.setText(end);
}
else if (unit == "ZB")
{
end = Double.toString(capacity.getZB());
result.setText(end);
}
else if (unit == "YB")
{
end = Double.toString(capacity.getYB());
result.setText(end);
}
}
});
}
}
Here is the new source to work with, when I tried to fix things:
Code:
package com.Actual.android;
import android.app.Activity;
import android.os.Bundle;
import android.widget.*;
import android.view.*;
public class ActualStorageActivity extends Activity
{
Spinner selection = (Spinner)findViewById(R.id.spinner); /* declare variable, in order to control spinner (ComboBox) */
ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.choices_array, android.R.layout.simple_spinner_dropdown_item); /* declare an array adapter object, in order for spinner to work */
EditText size = (EditText)findViewById(R.id.size); /* declare variable to control textfield */
EditText result = (EditText)findViewById(R.id.result); /* declare variable to control textfield */
Button calculate = (Button)findViewById(R.id.submit); /* declare variable to control button */
Storage capacity = new Storage(); /* import custom class for formulas */
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main); // load content from XML
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); /* set resource for dropdown */
selection.setAdapter(adapter); // attach adapter to spinner
result.setCursorVisible(false); // hide cursor
calculate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String initial = size.getText().toString();
String unit = (String)selection.getSelectedItem();
String end;
double convert = Double.parseDouble(initial);
capacity.setStorage(convert);
if (unit == "KB")
{
end = Double.toString(capacity.getKB());
result.setText(end);
}
else if (unit == "MB")
{
end = Double.toString(capacity.getMB());
result.setText(end);
}
else if (unit == "GB")
{
end = Double.toString(capacity.getGB());
result.setText(end);
}
else if (unit == "TB")
{
end = Double.toString(capacity.getTB());
result.setText(end);
}
else if (unit == "PB")
{
end = Double.toString(capacity.getPB());
result.setText(end);
}
else if (unit == "EB")
{
end = Double.toString(capacity.getEB());
result.setText(end);
}
else if (unit == "ZB")
{
end = Double.toString(capacity.getZB());
result.setText(end);
}
else if (unit == "YB")
{
end = Double.toString(capacity.getYB());
result.setText(end);
}
} }); /* attach button listener */
}
}