Calculator on android
Tutorials and code examples for creating apps on the android platform.
2 posts
Page 1 of 1
Here's how you make your own calculator for android:
first make a new project
App name: Calculator
Package: com.calculator (or any)
SDK Level : 10 (if you have installed it)
1.Now open the res/layout/main.xml
modify it like this:
2.Open src/com/sudoku (THIS IS THE CURRENT PROJECT PACKAGE NAME)
modify it become like this:
first make a new project
App name: Calculator
Package: com.calculator (or any)
SDK Level : 10 (if you have installed it)
1.Now open the res/layout/main.xml
modify it like this:
Code: Select all
<?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"
>
<WebView
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="50dip"/>
<LinearLayout android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button android:layout_height="wrap_content"
android:id="@+id/buttonLeftParen"
android:text="("
android:layout_weight="1"
android:layout_width="fill_parent"/>
<Button android:layout_height="wrap_content"
android:id="@+id/buttonRightParen"
android:text=")"
android:layout_weight="1"
android:layout_width="fill_parent"/>
<Button android:layout_height="wrap_content"
android:id="@+id/buttonBackspace"
android:layout_weight="1"
android:text="B"
android:layout_width="fill_parent"/>
<Button android:layout_height="wrap_content"
android:id="@+id/buttonClear"
android:text="C"
android:layout_weight="1"
android:layout_width="fill_parent"/>
</LinearLayout>
<LinearLayout android:id="@+id/linearLayout2" android:layout_width="fill_parent" android:layout_height="wrap_content">
<Button android:layout_height="wrap_content" android:id="@+id/button7" android:text="7" android:layout_weight="1" android:layout_width="fill_parent"></Button>
<Button android:layout_height="wrap_content" android:id="@+id/button8" android:text="8" android:layout_weight="1" android:layout_width="fill_parent"></Button>
<Button android:layout_height="wrap_content" android:id="@+id/button9" android:text="9" android:layout_weight="1" android:layout_width="fill_parent"></Button>
<Button android:layout_height="wrap_content" android:id="@+id/buttonPlus" android:text="+" android:layout_weight="1" android:layout_width="fill_parent"></Button>
</LinearLayout>
<LinearLayout android:id="@+id/linearLayout3" android:layout_width="fill_parent" android:layout_height="wrap_content">
<Button android:layout_height="wrap_content" android:id="@+id/button4" android:text="4" android:layout_weight="1" android:layout_width="fill_parent"></Button>
<Button android:layout_height="wrap_content" android:id="@+id/button5" android:text="5" android:layout_weight="1" android:layout_width="fill_parent"></Button>
<Button android:layout_height="wrap_content" android:id="@+id/button6" android:text="6" android:layout_weight="1" android:layout_width="fill_parent"></Button>
<Button android:layout_height="wrap_content" android:id="@+id/buttonMinus" android:text="-" android:layout_weight="1" android:layout_width="fill_parent"/></Button>
</LinearLayout>
<LinearLayout android:id="@+id/linearLayout4"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button android:layout_height="wrap_content"
android:id="@+id/button1"
android:text="1"
android:layout_weight="1"
android:layout_width="fill_parent"/>
<Button android:layout_height="wrap_content"
android:id="@+id/button2"
android:text="2"
android:layout_weight="1"
android:layout_width="fill_parent"/>
<Button android:layout_height="wrap_content"
android:id="@+id/button3"
android:text="3"
android:layout_weight="1"
android:layout_width="fill_parent"/>
<Button android:layout_height="wrap_content"
android:id="@+id/buttonTimes"
android:text="*"
android:layout_weight="1"
android:layout_width="fill_parent"/>
</LinearLayout>
<LinearLayout android:id="@+id/linearLayout5"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_weight="1"
android:id="@+id/button0"
android:text="0"/>
<Button android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_weight="1"
android:id="@+id/buttonDecimal"
android:text="."/>
<Button android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_weight="1"
android:id="@+id/buttonEquals"
android:text="="/>
<Button android:layout_height="wrap_content"
android:id="@+id/buttonDivide"
android:text="/"
android:layout_width="fill_parent"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
2.Open src/com/sudoku (THIS IS THE CURRENT PROJECT PACKAGE NAME)
modify it become like this:
Code: Select all
3.Debug it and the result:
package com.calculator;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.Button;
public class CalculatorActivity extends Activity {
private WebView mWebView;
private StringBuilder mMathString;
private ButtonClickListener mClickListener;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Create the math string
mMathString = new StringBuilder();
// Enable javascript for the view
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
// Set the listener for all the buttons
mClickListener = new ButtonClickListener();
int idList[] = { R.id.button0, R.id.button1, R.id.button2,
R.id.button3, R.id.button4, R.id.button5, R.id.button6,
R.id.button7, R.id.button8, R.id.button9, R.id.buttonLeftParen,
R.id.buttonRightParen, R.id.buttonPlus, R.id.buttonPlus,
R.id.buttonMinus, R.id.buttonDivide, R.id.buttonTimes,
R.id.buttonDecimal, R.id.buttonBackspace, R.id.buttonClear };
for(int id : idList) {
View v = findViewById(id);
v.setOnClickListener(mClickListener);
}
}
private void updateWebView() {
StringBuilder builder = new StringBuilder();
builder.append("<html><body>");
builder.append("<script type=\"text/javascript\">document.write('");
builder.append(mMathString.toString());
builder.append("');");
builder.append("document.write('<br />=' + eval(\"");
builder.append(mMathString.toString());
builder.append("\"));</script>");
builder.append("</body></html>");
mWebView.loadData(builder.toString(), "application/xhtml", "UTF-8");
}
private class ButtonClickListener implements OnClickListener {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.buttonBackspace:
if(mMathString.length() > 0)
mMathString.deleteCharAt(mMathString.length()-1);
break;
case R.id.buttonClear:
if(mMathString.length() > 0)
mMathString.delete(0, mMathString.length());
break;
default:
mMathString.append(((Button) v).getText());
}
updateWebView();
}
}
You do not have the required permissions to view the files attached to this post.
Could you explain everything? Please. A tutorial explains what code do.
LMAOSHMSFOAIDMT
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;
Over 30 projects with source code!
Please give reputation to helpful members!
![Image]()
![Image]()
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;
Over 30 projects with source code!
Please give reputation to helpful members!

2 posts
Page 1 of 1
Copyright Information
Copyright © Codenstuff.com 2020 - 2023