Creating an Android Calculator App with Code

I created an Android App Drake Equation calculator and published it on the market few months ago.  With only a hand few of downloads it was little disappointing.  The main thing is that I finished it and for the most part works well.

Since I wanted the calculator spread across many windows I used the SharedPreferences class.  If your  not even making a calculator this is a good example of the SharedPreences class.

 

 

Activity

I will only add  the first activity for the layout  since the other two look the same.

public class Activity2 extends Values {
	private AdView adView;

    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.screen_1);

        Button next = (Button) findViewById(R.id.Button02);
        next.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {  
           	   SharedPreferences sharedPreferences = getSharedPreferences("MY_SHARED_PREF", MODE_PRIVATE);
               SharedPreferences.Editor editor = sharedPreferences.edit();
            	final EditText Ra = (EditText) findViewById(R.id.Ra);
                final EditText fpa = (EditText) findViewById(R.id.fpa);
                final EditText nea = (EditText) findViewById(R.id.nea);

                String strNickname = Ra.getText().toString();
                editor.putString("A", strNickname);
                editor.commit();

                String strNickname2 = fpa.getText().toString();
                editor.putString("B", strNickname2);
                editor.commit();

                String strNickname3 = nea.getText().toString();
                editor.putString("C", strNickname3);
                editor.commit();

                if(sharedPreferences.getString("A", "").length() == 0 || sharedPreferences.getString("A", "").length() == 0 ||
                		sharedPreferences.getString("A", "").length() == 0){
                    Builder failureDialog = new AlertDialog.Builder(Activity2.this)
                    .setTitle("Your message")
                    .setMessage("You did not fill in all the fields")
                    .setPositiveButton("OK",null)
                    ;
                    failureDialog.show();

                }
                else{

                Intent intent = new Intent();
                intent.setClass(Activity2.this, screen_2.class);
                startActivity(intent);
                }
            }
        });
    }
}

Values Class

Used to store the values from that SharedPreferences class.

import android.content.SharedPreferences;

public class Values extends Activity1 {
	SharedPreferences sharedPreferences;

    //  values
    public static final String A = "";
    public static final String B = ""; // String
    public static final String C = ""; // String
    public static final String D = ""; // String
    public static final String E = ""; // 
    public static final String F = "";  // 
    public static final String G = "";
    public static final String H = "";   
}

Gathering all the data for output

public class answer extends Values {
	private AdView adView;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.answer);
        // Look up the AdView as a resource and load a request.
        AdView adView = (AdView)this.findViewById(R.id.adView);
        adView.loadAd(new AdRequest());
        SharedPreferences sharedPreferences = getSharedPreferences("MY_SHARED_PREF", MODE_PRIVATE);
       final TextView cherry = (TextView) findViewById(R.id.textView1);
       Float A = new Float(sharedPreferences.getString("A", ""));
       Float B = new Float(sharedPreferences.getString("B", ""));
       Float C = new Float (sharedPreferences.getString("C", ""));
       Float D = new Float(sharedPreferences.getString("D", ""));
       Float E = new Float(sharedPreferences.getString("E", ""));
       Float F = new Float (sharedPreferences.getString("F", ""));
       Float G = new Float(sharedPreferences.getString("G", ""));

       float hh =  A.floatValue() * B.floatValue() * C.floatValue() * 
       D.floatValue() * E.floatValue() * F.floatValue() *
       G.floatValue();
       cherry.setText(String.valueOf(hh));
        Button home = (Button) findViewById(R.id.button2);
        Button restart = (Button) findViewById(R.id.button1);
        Button history = (Button) findViewById(R.id.button3);
        home.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Intent myIntent = new Intent(view.getContext(), Activity1.class);
                startActivityForResult(myIntent, 0);
            }

        });
        restart.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Intent myIntent = new Intent(view.getContext(), Activity2.class);
                startActivityForResult(myIntent, 0);
            }

        });
        history.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Intent myIntent = new Intent(view.getContext(), History.class);
                startActivityForResult(myIntent, 0);
            }
        }); 

    }
}

Graveyard Database Logo

Has everything you want to know about grave yards. Plus has many celebrites and world leaders graves. Pssst it is my other site

Google + Profile
Check out Android Photo Mapping, its a app/site I been working on

  • wholesale nfl jerseys
  • wholesale nfl jerseys china
  • wholesale sports jerseys