Activity.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="fill_vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/hello_world"
android:textSize="30sp"
/>
<Button
android:id="@+id/addButtonn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="15dp"
android:text="@string/Add" />
<Button
android:id="@+id/subButtonn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/addButtonn"
android:layout_below="@+id/addButtonn"
android:layout_marginTop="18dp"
android:text="@string/Sub" />
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/subButtonn"
android:layout_below="@+id/subButtonn"
android:layout_marginTop="28dp"
android:gravity="right"
android:text="@string/present"
/>
</RelativeLayout>
Activity_main.java
package com.example.addsubtract;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
int
counter;
Button
add,sub;
TextView
tv;
@Override
protected
void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
counter=0;
tv=(TextView)findViewById(R.id.textView1);
add=(Button)findViewById(R.id.addButtonn);
sub=(Button)findViewById(R.id.subButtonn);
add.setOnClickListener(new
OnClickListener() {
@Override
public
void onClick(View arg0) {
//
TODO Auto-generated method stub
counter++;
tv.setText("Your
Result is"+counter);
}
});
sub.setOnClickListener(new
OnClickListener() {
@Override
public
void onClick(View arg0) {
//
TODO Auto-generated method stub
counter--;
tv.setText("Your
Result is "+counter);
}
});
}
@Override
public
boolean onCreateOptionsMenu(Menu menu) {
//
Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main,
menu);
return
true;
}
}
AndroidMainFest.xml
<?xml version="1.0"
encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.addsubtract"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17"
/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.addsubtract.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN"
/>
<category android:name="android.intent.category.LAUNCHER"
/>
</intent-filter>
</activity>
</application>
</manifest>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">AddSubtract</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Your Total is 0</string>
<string name="Add">Add By One</string>
<string name="Sub">Sub By One</string>
<string name="present">Presented By Ashok</string>
</resources>