PDA

View Full Version : Calculator App Problem :)



0Ajax0
02-26-2012, 12:25 PM
Hello all http://www.androidforums.com/images/smilies/smile.gif I am new here and also new to Android ..
I want to build a simple calculator app..
I have finished the display http://www.androidforums.com/images/smilies/smile.gif in an xml file..
But i do not know how to make the text appear on the Editor when i press the corresponding button http://www.androidforums.com/images/smilies/frown.gif
Any Help will be amazing.. i have been sittting with this all day..


also.. the moment I connect the editor to the activity file.. the calculator crashed.. any EditText variable or a reference to the EditText Id in XML also crashes..
this is for 1.5 android..

I will enter the source code.
This is the XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >



<TextView android:id="@+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Calculator"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"/>


<View android:id="@+id/alignhelper"
android:layout_width="fill_parent"
android:layout_height="4dp"
android:layout_alignParentBottom="true" />


<EditText
android:id="@+id/entry"
android:hint="Perform Calculation..."
android:layout_below="@id/label"
android:layout_width="fill_parent"
android:layout_height="80dip"


android:background="@android:drawable/editbox_background"
android:layout_alignParentTop="false" />

<TableLayout android:id="@+id/row1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/entry"

android:layout_above="@+id/alignhelper" >

<TableRow android:layout_weight="1">

<Button android:id="@+id/plus"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:text="+"
android:layout_weight="1"
/>


<Button android:id="@+id/minus"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:text="-"
android:layout_weight="1" />

<Button android:id="@+id/mul"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:text="*"
android:layout_weight="1" />

<Button android:id="@+id/div"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:text="/"
android:layout_weight="1" />
</TableRow>
<TableRow android:layout_weight="1">


<Button android:id="@+id/n7"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:text="7"
android:layout_weight="1" />

<Button android:id="@+id/n8"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:text="8"
android:layout_weight="1" />

<Button android:id="@+id/n9"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:text="9"
android:layout_weight="1" />

<Button android:id="@+id/perc"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:text="%"
android:layout_weight="1" />
</TableRow>
<TableRow android:layout_weight="1">

<Button android:id="@+id/n4"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:text="4"
android:layout_weight="1" />


<Button android:id="@+id/n5"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:text="5"
android:layout_weight="1" />

<Button android:id="@+id/n6"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:text="6"
android:layout_weight="1" />

<Button android:id="@+id/equal"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:text="="
android:layout_weight="1" />
</TableRow>
<TableRow android:layout_weight="1">

<Button android:id="@+id/n1"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:text="1"

android:layout_weight="1" />


<Button android:id="@+id/n2"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:text="2"
android:layout_weight="1" />

<Button android:id="@+id/n3"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:text="3"
android:layout_weight="1" />

<Button android:id="@+id/n0"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:text="0"
android:layout_weight="1" />
</TableRow>

</TableLayout>



</RelativeLayout>



This is the Activity :)


package com.calc.proj;


import android.app.Activity;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


public class CalculatorActivity extends Activity{
/** Called when the activity is first created. */

private TextView inpview1;
/* private EditText input2;
private EditText input3;
private EditText input4;
private EditText input5;
private EditText input6;
private EditText input7;
private EditText input8;
private EditText input9;
private EditText input0;
private EditText solution;*/
// private CalculatorActivity mContext;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// mContext=this;
setContentView(R.layout.main);


// inpview1= (TextView) findViewById(R.id.n1);

final Button inp1 =(Button)findViewById(R.id.n1);
/* input2=(EditText)findViewById(R.id.n2);
input3=(EditText)findViewById(R.id.n3);
input4=(EditText)findViewById(R.id.n4);
input5=(EditText)findViewById(R.id.n5);
input6=(EditText)findViewById(R.id.n6);
input7=(EditText)findViewById(R.id.n7);
input8=(EditText)findViewById(R.id.n8);
input9=(EditText)findViewById(R.id.n9);
input0=(EditText)findViewById(R.id.n0);


solution = (EditText) findViewById(R.id.equal);

Button plusB= (Button) findViewById(R.id.plus);
Button plusS= (Button) findViewById(R.id.minus);
Button plusM= (Button) findViewById(R.id.mul);
Button plusD= (Button) findViewById(R.id.div);
*/



//what do i enter here.. to get the input displayed on my editor ? :)
//thanks.. i do not expect spoon feeding..even a point in the right direction would help:)
//also.. I tried a OnClickListener.. nothing happens.. :(


}


}

furbearingmammal
02-26-2012, 12:40 PM
Thread moved to appropriate forum.

:welcome1: to the forum!