Tuesday, February 09, 2016

Addition/Substraction of two value in Android using Android Studio

There are Four main files in android when we create application using Android studio
1- AndroidManifest
2- MainActivity
3- activity_main.xml
4- content_main.xml

Lets try to digout one by one

1- AndroidManifest

This is the main file which contain information such as application label and its activity associated with it. Activity label will be displayed on the screen.
This file also shows our main java class which is going to be executed when user perform any even on the screen. i.e for us it is MainActivity



android:allowBackup=”true”
android:icon=”@mipmap/ic_launcher”
android:label=”TestAddition1?
android:supportsRtl=”true”
android:theme=”@style/AppTheme”>

android:name=”.MainActivity”
android:label=”TestAddition2?
android:theme=”@style/AppTheme.NoActionBar”>



2- MainActivity

This is main java class which contains code that handle all the ui request from the user.
package com.example.siddhu.addition;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.view.View.OnClickListener;
public class MainActivity extends AppCompatActivity {

//step1 : create all the variables.
EditText et1, et2, et3;
Button b1, b2;

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

//step2 : get all the views from xml file.
et1 = (EditText) findViewById(R.id.editText1);
et2 = (EditText) findViewById(R.id.editText2);
et3 = (EditText) findViewById(R.id.editText3);

b1 = (Button) findViewById(R.id.button1);
b2 = (Button) findViewById(R.id.button2);

//step3 : write add functionality.
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String f = et1.getText().toString();
int i = Integer.parseInt(f);
String s = et2.getText().toString();
int j = Integer.parseInt(s);

Integer result = i+j;
String res = result.toString();
et3.setText(res);
}
});
b2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String f = et1.getText().toString();
int i = Integer.parseInt(f);
String s = et2.getText().toString();
int j = Integer.parseInt(s);

Integer result = i-j;
String res = result.toString();
et3.setText(res);
}
});

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

}
3- activity_main.xml

This is base xml used by the android to map activity associated with UI Pages.

tools:context=”com.example.siddhu.addition.MainActivity”

4- content_main.xml

This is ui xml. This is render on screen of android to show different components



android:id=”@+id/editText1?
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignParentTop=”true”
android:layout_centerHorizontal=”true”
android:layout_marginTop=”24dp”
android:ems=”10?
android:inputType=”textNoSuggestions”/>

android:id=”@+id/editText2?
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignLeft=”@+id/editText1?
android:layout_below=”@+id/editText1?
android:ems=”10? />

android:id=”@+id/button1?
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignLeft=”@+id/editText2?
android:layout_below=”@+id/editText2?
android:text=”Add” />

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_alignRight=”@+id/editText2?
android:text=”Sub” />

android:id=”@+id/editText3?
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignLeft=”@+id/button1?
android:layout_below=”@+id/button1?
android:ems=”10? >





No comments: