Thursday, 4 July 2013

Android simple calculator

0 comments
From this tutorial you will learn how to develop a Simple Calculator.This Calculator takes two numbers as input and performs Addition ,Subtraction , multiplication and Division, and the result is displayed. In this tutorial we are using-

3 EditText , 2 for collecting input  and the other to display output.
2 Textviews- To locate the EditText.
4 Buttons -Addition ,Subtraction , multiplication and Division.

Here is the full code for the program,

Application Name : SimpleCalculator
Project Name :  SimpleCalculator

Activity Name : MainActivity.java
Layout Name : activity_main.xml

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="21dp"
        android:text="Enter First No : "
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="22dp"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="49dp"
        android:text="Enter Second No : "
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editText1"
        android:layout_below="@+id/textView2"
        android:layout_marginTop="45dp"
        android:ems="10" />

    <EditText
        android:id="@+id/editText3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/button2"
        android:layout_alignLeft="@+id/editText2"
        android:layout_marginBottom="37dp"
        android:ems="10" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_marginBottom="17dp"
        android:text="+" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button2"
        android:layout_alignBottom="@+id/button2"
        android:layout_alignRight="@+id/textView1"
        android:layout_marginRight="14dp"
        android:text="*" />

    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button3"
        android:layout_alignBottom="@+id/button3"
        android:layout_toRightOf="@+id/button3"
        android:text="/" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button1"
        android:layout_alignBottom="@+id/button1"
        android:layout_alignLeft="@+id/textView1"
        android:text="-" />

</RelativeLayout>

MainActivity.java 


package com.example.simplecalculator;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {
Button plus,sub,mult,div;
EditText fno,sno,result;
int a,b,c;
String m,n;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
plus=(Button)findViewById(R.id.button1);
sub=(Button)findViewById(R.id.button2);
fno=(EditText)findViewById(R.id.editText1);
sno=(EditText)findViewById(R.id.editText2);
result=(EditText)findViewById(R.id.editText3);
plus.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
String m=fno.getText().toString();
String n=sno.getText().toString();
if( m.equals("")||(n.equals("")))
{
Toast.makeText(getApplicationContext(), "Enter Both Numbers",Toast.LENGTH_SHORT).show();
}
else
{
a=Integer.parseInt(m);
b=Integer.parseInt(n);
c=a+b;
result.setText(Integer.toString(c));
}
}
});
sub=(Button)findViewById(R.id.button2);
sub.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
String m=fno.getText().toString();
String n=sno.getText().toString();
if( m.equals("")||(n.equals("")))
{
Toast.makeText(getApplicationContext(), "Enter Both Numbers",Toast.LENGTH_SHORT).show();
}
else
{
a=Integer.parseInt(m);
b=Integer.parseInt(n);
c=a-b;
result.setText(Integer.toString(c));
}
}
});
mult=(Button)findViewById(R.id.button3);
mult.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
String m=fno.getText().toString();
String n=sno.getText().toString();
if( m.equals("")||(n.equals("")))
{
Toast.makeText(getApplicationContext(), "Enter Both Numbers",Toast.LENGTH_SHORT).show();
}
else
{
a=Integer.parseInt(m);
b=Integer.parseInt(n);
c=a*b;
result.setText(Integer.toString(c));
}
}
});
div=(Button)findViewById(R.id.button4);
div.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
String m=fno.getText().toString();
String n=sno.getText().toString();
if( m.equals("")||(n.equals("")))
{
Toast.makeText(getApplicationContext(), "Enter Both The Numbers",Toast.LENGTH_SHORT).show();
}
else
{
a=Integer.parseInt(m);
b=Integer.parseInt(n);
c=a/b;
result.setText(Integer.toString(c));
}
}
});
}
}

When user presses the Button without entering the values,there is a chance of getting an application error, to avoid this the following code is used above
if( m.equals("")||(n.equals("")))
{
Toast.makeText(getApplicationContext(), "Enter Both The Numbers",Toast.LENGTH_SHORT).show();
}
The Output of the application is like this :

Read full post »

Monday, 1 July 2013

Android Toast Message Notification

1 comments
Android Toast notification is a message that pops up on the surface of the window. It only fills the amount of space required for the message,the user activity remains visible and interactive . The notification remains on the screen for sometimes and fades away.

To display a simple Toast,we need to makeText for that Toast and then call show() to display it.
The code used for Toast notification is :

Toast.makeText(getApplicationContext(), "Toast Message", Toast.LENGTH_LONG).show();

Here makeText has three parameters :

1. Context - The application Context.
2. Text- The Text message that need to be Displayed.
3. Duration - The Duration of the Toast to be Displayed.

Here is the full code for Toast message :

Application Name : SimpleToast
Project Name : SimpleToast

Activity Name : MainActivity.java
Layout Name : activity_main.xml

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button1"
        android:layout_width="178dp"
        android:layout_height="wrap_content"
        android:text="Toast Button" />

</LinearLayout>

MainActivity.java 

package com.example.simpletoast;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {
Button btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn=(Button)findViewById(R.id.button1);
        btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "This is a Toast Message", Toast.LENGTH_LONG).show();
}
});
    }


   
    
}

The Output of the program will be like this :


Android Toast example, Android Toast Demo, Android Tutorial Video
Read full post »