A COMPREHENSIVE GUIDE FOR DESIGNING, DEVELOPING, DEBUGGING, AND DISTRIBUTING ANDROID APPLICATIONS.
Redirect Call in Android
In this tutorial, I will show you the code on how to redirect from your android apps to mobile call. Just see the code below.
String phoneCallUri = "tel:911";
Intent phoneCallIntent = new Intent(Intent.ACTION_DIAL);
phoneCallIntent.setData(Uri.parse(phoneCallUri));
startActivity(phoneCallIntent);
That's it, Happy Coding.
Crop Image
In this tutorial i will show you how to crop an image in your android apps. I just refer this tutorial from this site. Just see the code below.
First thing you need to do is to create an Android Project. Second is create a two layouts named crop_selector.xml and main.xml.
crop_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:gravity="center_vertical">
<ImageView
android:id="@+id/iv_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""/>
</LinearLayout>
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:gravity="center"
android:textStyle="bold"
android:textSize="17sp"
android:text="How to select and crop image"/>
<Button
android:id="@+id/btn_crop"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Select image" />
<ImageView
android:id="@+id/iv_photo"
android:layout_marginTop="10dp"
android:layout_gravity="center_horizontal"
android:layout_width="fill_parent"
android:layout_height="200dp"/>
</LinearLayout>
After you create the two layouts, you need to create 3 classes namely MainActivity.java, CropOption.java, and CropOptionAdapter.
MainActivity.java
package net.londatiga.android;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ActivityNotFoundException;
import android.content.ComponentName;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.graphics.Bitmap;
import android.provider.MediaStore;
import android.view.View;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.widget.Button;
import android.widget.ArrayAdapter;
import android.widget.Toast;
import android.widget.ImageView;
public class MainActivity extends Activity {
private Uri mImageCaptureUri;
private ImageView mImageView;
private static final int PICK_FROM_CAMERA = 1;
private static final int CROP_FROM_CAMERA = 2;
private static final int PICK_FROM_FILE = 3;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final String [] items = new String [] {"Take from camera", "Select from gallery"};
ArrayAdapter<String> adapter = new ArrayAdapter<String> (this, android.R.layout.select_dialog_item,items);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Select Image");
builder.setAdapter( adapter, new DialogInterface.OnClickListener() {
public void onClick( DialogInterface dialog, int item ) { //pick from camera
if (item == 0) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
mImageCaptureUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(),
"tmp_avatar_" + String.valueOf(System.currentTimeMillis()) + ".jpg"));
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mImageCaptureUri);
try {
intent.putExtra("return-data", true);
startActivityForResult(intent, PICK_FROM_CAMERA);
} catch (ActivityNotFoundException e) {
e.printStackTrace();
}
} else { //pick from file
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Complete action using"), PICK_FROM_FILE);
}
}
} );
final AlertDialog dialog = builder.create();
Button button = (Button) findViewById(R.id.btn_crop);
mImageView = (ImageView) findViewById(R.id.iv_photo);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.show();
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != RESULT_OK) return;
switch (requestCode) {
case PICK_FROM_CAMERA:
doCrop();
break;
case PICK_FROM_FILE:
mImageCaptureUri = data.getData();
doCrop();
break;
case CROP_FROM_CAMERA:
Bundle extras = data.getExtras();
if (extras != null) {
Bitmap photo = extras.getParcelable("data");
mImageView.setImageBitmap(photo);
}
File f = new File(mImageCaptureUri.getPath());
if (f.exists()) f.delete();
break;
}
}
private void doCrop() {
final ArrayList<CropOption> cropOptions = new ArrayList<CropOption>();
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setType("image/*");
List<ResolveInfo> list = getPackageManager().queryIntentActivities( intent, 0 );
int size = list.size();
if (size == 0) {
Toast.makeText(this, "Can not find image crop app", Toast.LENGTH_SHORT).show();
return;
} else {
intent.setData(mImageCaptureUri);
intent.putExtra("outputX", 200);
intent.putExtra("outputY", 200);
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("scale", true);
intent.putExtra("return-data", true);
if (size == 1) {
Intent i = new Intent(intent);
ResolveInfo res = list.get(0);
i.setComponent( new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
startActivityForResult(i, CROP_FROM_CAMERA);
} else {
for (ResolveInfo res : list) {
final CropOption co = new CropOption();
co.title = getPackageManager().getApplicationLabel(res.activityInfo.applicationInfo);
co.icon = getPackageManager().getApplicationIcon(res.activityInfo.applicationInfo);
co.appIntent= new Intent(intent);
co.appIntent.setComponent( new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
cropOptions.add(co);
}
CropOptionAdapter adapter = new CropOptionAdapter(getApplicationContext(), cropOptions);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Choose Crop App");
builder.setAdapter( adapter, new DialogInterface.OnClickListener() {
public void onClick( DialogInterface dialog, int item ) {
startActivityForResult( cropOptions.get(item).appIntent, CROP_FROM_CAMERA);
}
});
builder.setOnCancelListener( new DialogInterface.OnCancelListener() {
@Override
public void onCancel( DialogInterface dialog ) {
if (mImageCaptureUri != null ) {
getContentResolver().delete(mImageCaptureUri, null, null );
mImageCaptureUri = null;
}
}
} );
AlertDialog alert = builder.create();
alert.show();
}
}
}
}
CropOption.java
package net.londatiga.android;
import android.content.Intent;
import android.graphics.drawable.Drawable;
public class CropOption {
public CharSequence title;
public Drawable icon;
public Intent appIntent;
}
CropOptionAdapter
package net.londatiga.android;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.content.Context;
import java.util.ArrayList;
/**
* Adapter for crop option list.
*
* @author Lorensius W. L. T <lorenz@londatiga.net>
*
*/
public class CropOptionAdapter extends ArrayAdapter<CropOption> {
private ArrayList<CropOption> mOptions;
private LayoutInflater mInflater;
public CropOptionAdapter(Context context, ArrayList<CropOption> options) {
super(context, R.layout.crop_selector, options);
mOptions = options;
mInflater = LayoutInflater.from(context);
}
@Override
public View getView(int position, View convertView, ViewGroup group) {
if (convertView == null)
convertView = mInflater.inflate(R.layout.crop_selector, null);
CropOption item = mOptions.get(position);
if (item != null) {
((ImageView) convertView.findViewById(R.id.iv_icon)).setImageDrawable(item.icon);
((TextView) convertView.findViewById(R.id.tv_name)).setText(item.title);
return convertView;
}
return null;
}
}
Change dot to Asterisk in Password
In this tutorial, I will show you how to change the default value for password which is dot(.) to asterisk(*). See the code below.
//create a child class of your main class
class AsteriskPasswordTransformationMethod extends PasswordTransformationMethod {
@Override
public CharSequence getTransformation(CharSequence source, View view) {
return new PasswordCharSequence(source);
}
private class PasswordCharSequence implements CharSequence {
private CharSequence mSource;
public PasswordCharSequence(CharSequence source) {
mSource = source; // Store char sequence
}
public char charAt(int index) {
return '*'; // This is the important part
}
public int length() {
return mSource.length(); // Return default
}
public CharSequence subSequence(int start, int end) {
return mSource.subSequence(start, end); // Return default
}
}
};
//then call this class wherever you need to change the dot to asterisk.
final EditText et_pin1 = (EditText)dl.findViewById(R.id.et_verify1);et_pin1.setTransformationMethod(new AsteriskPasswordTransformationMethod());
That's it, hope this simple tutorial will help you something. Happy Coding
Customized dialog within the Activity
In this post, I will show you how to create a customized dialog in Android. I can't show you the output because I can't upload image at this time. I will just show you some codes.
//declare a dl variable for dialog at the top
private Dialog dl;
//user this code whereever you need it.
dl = new Dialog(Send_Money.this);
dl.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
dl.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
dl.setContentView(getLayoutInflater().inflate(R.layout.send_money_option_dialog, null));
dl.getWindow().getAttributes().windowAnimations = R.style.dialog_animation;
TextView tv_msg = (TextView)dl.findViewById(R.id.tv_message);
TextView tv_title = (TextView)dl.findViewById(R.id.tv_dialog_title);
tv_title.setText("TITLE");
tv_msg.setText("Message");
Button btn_one = (Button) dl.findViewById(R.id.btn_dialog_btn_one);
Button btn_two = (Button) dl.findViewById(R.id.btn_dialog_btn_two );
Button btn_three = (Button) dl.findViewById(R.id.btn_dialog_btn_three);
btn_one .setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//do some code for button one
}
});
btn_two .setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//do some code for button two
}
});
btn_three .setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//do some code for button three
}
});
dl.show();
}
That's it, hope this simple tutorial will help youo. Happy Coding.
Redirect to SMS
In this tutorial, I will show you some tutorial that redirecting or passing message to built in Messages Apps in your Android phone. From your Android Apps, you pass a message to it messages field. See the code below.
Intent smsIntent = new Intent(Intent.ACTION_VIEW);
smsIntent.setData(Uri.parse("sms:"));
smsIntent.putExtra("sms_body", "Your message"));
startActivity(smsIntent);
To only send a number, use this code.
startActivity(new Intent(Intent.ACTION_VIEW, Uri.fromParts("sms", "number", null)));
That's it, hope this simple tutorial will help you. Happy Coding...
Login Validation
In this tutorial, I will show you how to validate login using static input. See output below.
Here's the view of my project.
Here's the code of LoginModel.java
package com.example.model;
public class LoginModel {
public String m_email, m_password;
//constructor
public LoginModel(String email, String password){
this.m_email = email;
this.m_password = password;
}
}
Here's the code of Controller.java
package com.example.controller;
import java.util.regex.Pattern;
import android.util.Patterns;
import com.example.model.LoginModel;
public class Controller{
//static variables
private static final String EMAIL = "gaudicos61@gmail.com";
private static final String PASSWORD = "gaudicos";
@SuppressWarnings("unused")
public boolean isCheckEmail(LoginModel model) {
Pattern pattern = Patterns.EMAIL_ADDRESS;
if(pattern.matcher(model.m_email).matches() == true && model.m_email.equals(EMAIL)){
return true;
}else{
return false;
}
}
public boolean isCheckPassword(LoginModel model){
if(PASSWORD.equals(model.m_password)){
return true;
}else{
return false;
}
}
}
Here's the code of MainActivity.java
package com.example.menudesign;
import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.example.controller.Controller;
import com.example.model.LoginModel;
public class MainActivity extends Activity implements OnClickListener{
private EditText et_email,et_password;
private TextView tv_error_email, tv_error_pass;
private Button btn_login;
LoginModel model;
Controller c_controller;
private int email_indicator = 0, password_indicator = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initializeVars();
}
public void initializeVars(){
et_email = (EditText)findViewById(R.id.et_sample1);
et_password = (EditText)findViewById(R.id.et_sample2);
tv_error_email = (TextView)findViewById(R.id.tv_validate_email);
tv_error_pass = (TextView)findViewById(R.id.tv_validate_password);
btn_login = (Button)findViewById(R.id.btn_login);
tv_error_email.setVisibility(View.INVISIBLE);
tv_error_pass.setVisibility(View.INVISIBLE);
btn_login.setEnabled(false);
btn_login.setOnClickListener(this);
//listen the the editText when it's value changed
TextWatcher watcher = new TextWatcher() {
public void afterTextChanged(Editable s) {
model = new LoginModel(et_email.getText().toString(), et_password.getText().toString());
c_controller = new Controller();
try{
if(c_controller.isCheckEmail(model) == false && c_controller.isCheckPassword(model) == false){
tv_error_email.setVisibility(View.VISIBLE);
tv_error_email.setText("Invalid email address or doesnt match.");
tv_error_pass.setVisibility(View.VISIBLE);
tv_error_pass.setText("Invalid password or doesnt match.");
btn_login.setEnabled(false);
email_indicator = 0;
password_indicator = 0;
}else if(c_controller.isCheckEmail(model) == true && c_controller.isCheckPassword(model) == false){
tv_error_pass.setVisibility(View.VISIBLE);
tv_error_pass.setText("Invalid password or doesnt match.");
btn_login.setEnabled(false);
email_indicator = 1;
password_indicator = 0;
}else if(c_controller.isCheckEmail(model) == false && c_controller.isCheckPassword(model) == true){
tv_error_email.setVisibility(View.VISIBLE);
tv_error_email.setText("Invalid email address or doesnt match.");
btn_login.setEnabled(false);
email_indicator = 0;
password_indicator = 1;
}else{
//means all input are valid, code here to login...
email_indicator = 1;
password_indicator = 1;
btn_login.setEnabled(true);
}
}catch(StringIndexOutOfBoundsException e){
e.printStackTrace();
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
tv_error_email.setVisibility(View.INVISIBLE);
tv_error_pass.setVisibility(View.INVISIBLE);
};
};
et_email.addTextChangedListener(watcher);
et_password.addTextChangedListener(watcher);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.btn_login:
if(email_indicator == 1 && password_indicator == 1){
Toast.makeText(getApplicationContext(), "You can now log in here!", Toast.LENGTH_SHORT).show();
}
}
}
}
Here's the code of activity_main.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: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" >
<EditText
android:id="@+id/et_sample1"
android:layout_width="500dp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="46dp" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/et_sample2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/et_sample1"
android:layout_alignRight="@+id/et_sample1"
android:layout_below="@+id/et_sample1"
android:layout_marginTop="10dp" />
<TextView
android:id="@+id/tv_validate_email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/et_sample2"
android:layout_below="@+id/et_sample1"
android:textSize="12sp"
android:visibility="gone"
android:textColor="#FFFFFF"
android:background="@drawable/ic_error_bg" />
<TextView
android:id="@+id/tv_validate_password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/et_sample2"
android:layout_below="@+id/et_sample2"
android:textSize="12sp"
android:visibility="gone"
android:textColor="#FFFFFF"
android:background="@drawable/ic_error_bg" />
<Button
android:id="@+id/btn_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tv_validate_password"
android:layout_centerHorizontal="true"
android:layout_marginTop="31dp"
android:text="Login" />
</RelativeLayout>
That's it, hope this simple tutorial may help you. Happy Coding.
Basic Calculator
In this tutorial, I will show you how to create simple calculator in Android. See output below!
Here's the CalculatorModel.java
package com.example.calculator;
public class CalculatorModel {
double m_num1, m_num2;
//constructor
public CalculatorModel(double num1, double num2){
this.m_num1 = num1;
this.m_num2 = num2;
}
//setters
public void setNum1(double num1){
this.m_num1 = num1;
}
public void setNum2(double num2){
this.m_num2 = num2;
}
//getters
public double getNum1(){
return this.m_num1;
}
public double getNum2(){
return this.m_num2;
}
}
Here's the CalculatorController.java
package com.example.calculator;
public class CalculatorController {
public double addition(CalculatorModel cmodel){
return cmodel.m_num1 + cmodel.m_num2;
}
public double subtraction(CalculatorModel cmodel){
return cmodel.m_num1 - cmodel.m_num2;
}
public double multiplication(CalculatorModel cmodel){
return cmodel.m_num1 * cmodel.m_num2;
}
public double divistion(CalculatorModel cmodel){
return cmodel.m_num1 / cmodel.m_num2;
}
}
Here's the CalculatorActivity.java
package com.example.calculator;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class CalculatorActivity extends Activity implements OnClickListener{
EditText et_result;
Button btn_one, btn_two, btn_three, btn_four, btn_five, btn_six, btn_seven,
btn_eight, btn_nine, btn_zero, btn_point, btn_add, btn_minus, btn_times,
btn_divide, btn_equal, btn_clear;
double num1 = 0;
int indicator = 0;
CalculatorModel c_model;
CalculatorController c_controller;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calculator);
initializeVars();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.calculator, menu);
return true;
}
public void initializeVars(){
et_result = (EditText)findViewById(R.id.et_result);
btn_one = (Button)findViewById(R.id.btn_one);
btn_two = (Button)findViewById(R.id.btn_two);
btn_three = (Button)findViewById(R.id.btn_three);
btn_four = (Button)findViewById(R.id.btn_four);
btn_five = (Button)findViewById(R.id.btn_five);
btn_six = (Button)findViewById(R.id.btn_six);
btn_seven = (Button)findViewById(R.id.btn_seven);
btn_eight = (Button)findViewById(R.id.btn_eight);
btn_nine = (Button)findViewById(R.id.btn_nine);
btn_zero = (Button)findViewById(R.id.btn_zero);
btn_point = (Button)findViewById(R.id.btn_point);
btn_add = (Button)findViewById(R.id.btn_add);
btn_minus = (Button)findViewById(R.id.btn_minus);
btn_times = (Button)findViewById(R.id.btn_times);
btn_divide = (Button)findViewById(R.id.btn_divide);
btn_equal = (Button)findViewById(R.id.btn_equal);
btn_clear = (Button)findViewById(R.id.btn_clear);
btn_one.setOnClickListener(this);
btn_two.setOnClickListener(this);
btn_three.setOnClickListener(this);
btn_four.setOnClickListener(this);
btn_five.setOnClickListener(this);
btn_six.setOnClickListener(this);
btn_seven.setOnClickListener(this);
btn_eight.setOnClickListener(this);
btn_nine.setOnClickListener(this);
btn_zero.setOnClickListener(this);
btn_point.setOnClickListener(this);
btn_add.setOnClickListener(this);
btn_minus.setOnClickListener(this);
btn_times.setOnClickListener(this);
btn_divide.setOnClickListener(this);
btn_equal.setOnClickListener(this);
btn_clear.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try{
switch(v.getId()){
case R.id.btn_one:
et_result.setText(et_result.getText().toString()+btn_one.getText().toString());
break;
case R.id.btn_two:
et_result.setText(et_result.getText().toString()+btn_two.getText().toString());
break;
case R.id.btn_three:
et_result.setText(et_result.getText().toString()+btn_three.getText().toString());
break;
case R.id.btn_four:
et_result.setText(et_result.getText().toString()+btn_four.getText().toString());
break;
case R.id.btn_five:
et_result.setText(et_result.getText().toString()+btn_five.getText().toString());
break;
case R.id.btn_six:
et_result.setText(et_result.getText().toString()+btn_six.getText().toString());
break;
case R.id.btn_seven:
et_result.setText(et_result.getText().toString()+btn_seven.getText().toString());
break;
case R.id.btn_eight:
et_result.setText(et_result.getText().toString()+btn_eight.getText().toString());
break;
case R.id.btn_nine:
et_result.setText(et_result.getText().toString()+btn_nine.getText().toString());
break;
case R.id.btn_zero:
et_result.setText(et_result.getText().toString()+btn_zero.getText().toString());
break;
case R.id.btn_point:
et_result.setText(et_result.getText().toString()+btn_point.getText().toString());
break;
case R.id.btn_add:
num1 = Double.parseDouble(et_result.getText().toString());
et_result.setText("");
indicator = 1;
break;
case R.id.btn_minus:
num1 = Double.parseDouble(et_result.getText().toString());
et_result.setText("");
indicator = 2;
break;
case R.id.btn_times:
num1 = Double.parseDouble(et_result.getText().toString());
et_result.setText("");
indicator = 3;
break;
case R.id.btn_divide:
num1 = Double.parseDouble(et_result.getText().toString());
et_result.setText("");
indicator = 4;
break;
case R.id.btn_clear:
et_result.setText("");
break;
case R.id.btn_equal:
c_model = new CalculatorModel(num1, Double.parseDouble(et_result.getText().toString()));
c_controller = new CalculatorController();
switch(indicator){
case 1:
et_result.setText(Double.toString(c_controller.addition(c_model)));
break;
case 2:
et_result.setText(Double.toString(c_controller.subtraction(c_model)));
break;
case 3:
et_result.setText(Double.toString(c_controller.multiplication(c_model)));
break;
case 4:
et_result.setText(Double.toString(c_controller.divistion(c_model)));
break;
}
break;
}
}catch(Exception e){
e.printStackTrace();
}
}
}
And lastly, the activity_calculator.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: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=".CalculatorActivity" >
<RelativeLayout
android:id="@+id/rl_hold_calculator"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true">
"
<EditText
android:id="@+id/et_result"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:ems="10"
android:inputType="numberDecimal" >
<requestFocus />
</EditText>
<Button
android:id="@+id/btn_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/et_result"
android:layout_below="@+id/et_result"
android:layout_marginTop="10dp"
android:text="1" />
<Button
android:id="@+id/btn_two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/btn_one"
android:layout_alignBottom="@+id/btn_one"
android:layout_toRightOf="@+id/btn_one"
android:text="2" />
<Button
android:id="@+id/btn_three"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/btn_two"
android:layout_alignBottom="@+id/btn_two"
android:layout_toRightOf="@+id/btn_two"
android:text="3" />
<Button
android:id="@+id/btn_four"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/btn_three"
android:layout_alignBottom="@+id/btn_three"
android:layout_toRightOf="@+id/btn_three"
android:text="4" />
<Button
android:id="@+id/btn_five"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/btn_one"
android:layout_below="@+id/btn_one"
android:layout_marginTop="10dp"
android:text="5" />
<Button
android:id="@+id/btn_six"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/btn_five"
android:layout_alignBottom="@+id/btn_five"
android:layout_alignLeft="@+id/btn_two"
android:text="6" />
<Button
android:id="@+id/btn_seven"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/btn_six"
android:layout_alignBottom="@+id/btn_six"
android:layout_alignLeft="@+id/btn_three"
android:text="7" />
<Button
android:id="@+id/btn_eight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/btn_seven"
android:layout_alignBottom="@+id/btn_seven"
android:layout_toRightOf="@+id/btn_seven"
android:text="8" />
<Button
android:id="@+id/btn_nine"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/btn_five"
android:layout_below="@+id/btn_five"
android:layout_marginTop="10dp"
android:text="9" />
<Button
android:id="@+id/btn_zero"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/btn_nine"
android:layout_alignBottom="@+id/btn_nine"
android:layout_toRightOf="@+id/btn_nine"
android:text="0" />
<Button
android:id="@+id/btn_point"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/btn_zero"
android:layout_alignBottom="@+id/btn_zero"
android:layout_toRightOf="@+id/btn_zero"
android:text="." />
<Button
android:id="@+id/btn_clear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/btn_point"
android:layout_alignBottom="@+id/btn_point"
android:layout_toRightOf="@+id/btn_point"
android:text="C" />
<Button
android:id="@+id/btn_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/btn_nine"
android:layout_below="@+id/btn_nine"
android:layout_marginTop="10dp"
android:text="+" />
<Button
android:id="@+id/btn_minus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/btn_add"
android:layout_alignBottom="@+id/btn_add"
android:layout_toRightOf="@+id/btn_add"
android:text="-" />
<Button
android:id="@+id/btn_times"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/btn_minus"
android:layout_alignBottom="@+id/btn_minus"
android:layout_alignLeft="@+id/btn_point"
android:text="*" />
<Button
android:id="@+id/btn_divide"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/btn_times"
android:layout_alignBottom="@+id/btn_times"
android:layout_alignLeft="@+id/btn_clear"
android:text="/" />
<Button
android:id="@+id/btn_equal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/btn_add"
android:layout_alignParentRight="true"
android:layout_below="@+id/btn_add"
android:layout_marginTop="10dp"
android:text="=" />
</RelativeLayout>
</RelativeLayout>