This post is an tutorial for android development
Today we will learn how to implement "Real Time Calculator" In Android.
Sample Screen Shot -
Here is sample code -
activity_main.xml
MainActivity.java
Hope This code helps!!!!!!
Happy Coding!!!!!!!:)
Today we will learn how to implement "Real Time Calculator" In Android.
Sample Screen Shot -
Here is sample code -
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_horizontal" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Enter Width" /> <EditText android:id="@+id/edit_width" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/actionbar_search" android:paddingLeft="10dip" android:paddingRight="10dip" android:singleLine="true" android:text="0" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Enter Height" /> <EditText android:id="@+id/edit_height" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/actionbar_search" android:paddingLeft="10dip" android:paddingRight="10dip" android:singleLine="true" android:text="0" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Calculated Area" /> <EditText android:id="@+id/edit_area" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/actionbar_search" android:editable="false" android:paddingLeft="10dip" android:paddingRight="10dip" android:text="0" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Calculated Perimeter" /> <EditText android:id="@+id/edit_perimeter" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/actionbar_search" android:editable="false" android:paddingLeft="10dip" android:paddingRight="10dip" android:text="0" /> </LinearLayout>
MainActivity.java
package com.example.edittextchange; import android.support.v7.app.ActionBarActivity; import android.support.v7.app.ActionBar; import android.support.v4.app.Fragment; import android.text.Editable; import android.text.TextWatcher; import android.os.Bundle; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.EditText; import android.os.Build; public class MainActivity extends ActionBarActivity { EditText edit_width, edit_height, edit_area, edit_perimeter; double width; double height; double area; double perimeter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); edit_area = (EditText) findViewById(R.id.edit_area); edit_height = (EditText) findViewById(R.id.edit_height); edit_width = (EditText) findViewById(R.id.edit_width); edit_perimeter = (EditText) findViewById(R.id.edit_perimeter); edit_width.addTextChangedListener(new TextWatcher() { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { // TODO Auto-generated method stub } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { // TODO Auto-generated method stub } @Override public void afterTextChanged(Editable s) { // TODO Auto-generated method stub String widthString = edit_width.getText().toString(); // convert the String into a double if (widthString.length() > 0) { width = Double.parseDouble(widthString); } // read the height out of heightEdit String heightString = edit_height.getText().toString(); if (heightString.length() > 0) { height = Double.parseDouble(heightString); } // calculate area double area = calcArea(); // calculate perimeter double perim = calcPerim(); // set the label for areaText edit_area.setText(Double.toString(area)); // set the label for perimText edit_perimeter.setText(Double.toString(perim)); } }); edit_height.addTextChangedListener(new TextWatcher() { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { // TODO Auto-generated method stub } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { // TODO Auto-generated method stub } @Override public void afterTextChanged(Editable s) { // TODO Auto-generated method stub String widthString = edit_width.getText().toString(); // convert the String into a double if (widthString.length() > 0) { width = Double.parseDouble(widthString); } // read the height out of heightEdit String heightString = edit_height.getText().toString(); if (heightString.length() > 0) { height = Double.parseDouble(heightString); } // calculate area double area = calcArea(); // calculate perimeter double perim = calcPerim(); // set the label for areaText edit_area.setText(Double.toString(area)); // set the label for perimText edit_perimeter.setText(Double.toString(perim)); } }); } double calcArea() { return width * height; } double calcPerim() { return 2 * width * height; } @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; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } /** * A placeholder fragment containing a simple view. */ public static class PlaceholderFragment extends Fragment { public PlaceholderFragment() { } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_main, container, false); return rootView; } } }
Hope This code helps!!!!!!
Happy Coding!!!!!!!:)
No comments:
Post a Comment