Sunday, 2 November 2014

Creating a Calculator Android app



I'm learning android programing and i just dont know how turn my xml into a working calculator.


here is my xml, i removed the styling for better reading



<RelativeLayout xmlns:android="http://ift.tt/nIICcg"
xmlns:tools="http://ift.tt/LrGmb4"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.afm.calculator.MainActivity" >

<TextView
android:id="@+id/tvresult"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
android:textSize="40dp" />

<Button
android:id="@+id/times"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="x"
android:textSize="40sp" />

<Button
android:id="@+id/plus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+"
android:textSize="40dp" />

<Button
android:id="@+id/divide"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="÷"
android:textSize="40sp" />

<Button
android:id="@+id/mines"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-"
android:textSize="40sp" />

<Button
android:id="@+id/equal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="="
android:textSize="40sp" />

<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="numberDecimal" >

</EditText>

<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="numberDecimal" />

</RelativeLayout>


here is my java:



package com.afm.cal;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity implements android.view.View.OnClickListener {

Button mines, plus, divide, times, equal;
EditText eto, ett;
TextView tv;

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mines = (Button) findViewById(R.id.mines);
plus = (Button) findViewById(R.id.plus);
divide = (Button) findViewById(R.id.divide);
times = (Button) findViewById(R.id.times);
equal = (Button) findViewById(R.id.equal);
eto = (EditText) findViewById(R.id.editText1);
ett = (EditText) findViewById(R.id.editText2);
tv = (TextView) findViewById(R.id.tvresult);



}


public void onClick(View arg0) {

}

}


please help me finish it and thx in advance


No comments:

Post a Comment