So far a couple of things learned:
- If you know java or object oriented programming, the learning curve is WAY less steep, but steep never the less.
- The icon for your app can be found in /res/drawable-ldpi (and mdpi, and hdpi)
- With out input there’s very little to do, so the basic of all basics is EditText. To get it’s contents use YourObject.getText().toString() which implements comparable so use equals() to compare.
- If you want to get numbers only, even though it seems good practice to sanitize the input, you can specify in the XML layout android:inputType=”numberDecimal” to restrict input.
- Setup layouts in XML and handle the mechanics through code. Keeps the UI separate from the code, and it is WAY easier to define.
- Dialogs can have custom setups through XML.
- Dialogs are created ONCE, so in this application, for example, you want to get the final number to make your invoice to before taxes. If you go with the google examples, chances are, the first time you do it, it’ll be fine, change your values and show the dialog again, now it reads exactly the same. If you need the SAME dialog to display different info you either create a new one, or keep a reference of the old one and modify the values of the actual Dialog object. Modifying global variables is not good enough.
Here is the code:
package com.example.helloandroid;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class HelloAndroid extends Activity implements OnClickListener {
String message;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button)findViewById(R.id.calculate);
button.setOnClickListener(this);
}
public void onClick(View v) {
EditText cashobj = (EditText)findViewById(R.id.cash);
EditText taxobj = (EditText)findViewById(R.id.tax);
if (!(cashobj.getText().toString().equals("") || taxobj.getText().toString().equals(""))) {
Float cash = new Float (cashobj.getText().toString());
Float tax = new Float (taxobj.getText().toString());
if ((cash > 0) && (tax>0) && (tax<60)) {
Float valor = ( 100 * cash ) / ( 100 + tax );
message = new String(valor.toString());
showDialog(1);
} else {
showDialog(2);
}
}
}
protected Dialog onCreateDialog(int id) {
Dialog dialog;
AlertDialog.Builder builder = new AlertDialog.Builder(this);
switch(id) {
case 1:
builder.setMessage(message)
.setCancelable(false)
.setTitle("Make Invoice for:")
.setNeutralButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
removeDialog(1);
}
});
dialog = builder.create();
break;
case 2:
builder.setMessage("Check your numbers, something is so not right")
.setCancelable(false)
.setTitle("ERROR")
.setNeutralButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
dialog = builder.create();
break;
default:
dialog = null;
}
return dialog;
}
}

@federico_jacobi