----------------------------------------------------------------
XML CODE
----------------------------------------------------------------
<?xml version="1.0"
encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Total is : 0"
android:textSize="45dp"
android:layout_gravity="center"
android:gravity="center"
android:id="@+id/tvDisplay"
/>
<Button
android:layout_width="250dp"
android:layout_height="wrap_content"
android:id="@+id/bAdd"
android:text="Add"
android:layout_gravity="center"
android:textSize="20dp"
android:onClick="perfrom"
/>
<Button
android:layout_width="250dp"
android:layout_height="wrap_content"
android:id="@+id/bSub"
android:layout_gravity="center"
android:text="Sub"
android:textSize="20dp"
android:onClick="perfrom"
/>
</LinearLayout>
----------------------------------------------------------------
JAVA CODE
----------------------------------------------------------------
package com.example.newapp;
import android.os.Bundle;
import
android.app.Activity;
import android.view.Menu;
import android.view.View;
import
android.widget.Button;
import
android.widget.TextView;
public class MainActivity extends Activity {
int counter;
Button
add ,sub;
TextView
display;
@Override
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
counter=0;
add = (Button) findViewById(R.id.bAdd);
sub = (Button) findViewById(R.id.bSub);
display = (TextView) findViewById(R.id.tvDisplay);
add.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated
method stub
counter++;
display.setText("Total :
"+counter);
}
});
sub.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated
method stub
counter--;
display.setText("Total :
"+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;
}
}
----------------------------------------------------------------
OUTPUT
----------------------------------------------------------------
No comments:
Post a Comment