Andriod programming – Posts 6: Construction Practice Calculator app
Hello everyone, in 2 before we have gathered all the interface design is relatively more then. To reduce stress and unwind a little, then this article will guide you to make a complete application Calculator.
[qads]
We will use the code interface designed eve.
ah forget, you are prompted to do with this app can calculate quickly and do not take much effort, we will use library Balan they have built. You balan.jar file download here. Next open the Project mode and copy the file to the folder balan.jar app / libs like shape
Then Right-click the selected file balan.jar Add as Library… Near the bottom of the menu, the system asks if you want to add on modules as every app is worried ok. Wait a minute synchronous system is finished.
Now you open the file in the folder ActivityMain.java app/src/main/java/{package name} out. Currently jaw setContentViewOur drrdf remains R.layout.activity_main, you change it to R.layout.layout_calculator to connect to the file of the Calculator interface you've designed the day before.
package com.nguyenvanquan7826.tut5linearlayout; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.layout_calculator); } }
then ok, now really started offline.
First, to be able to work with computers, we need to connect the elements in the interface with Java code through the corresponding variable.
package com.nguyenvanquan7826.tut5linearlayout; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.TextView; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private TextView tvMath; private TextView tvResult; private int[] idButton = { R.id.btn0, R.id.btn1, R.id.btn2, R.id.btn3, R.id.btn4, R.id.btn5, R.id.btn6, R.id.btn7, R.id.btn8, R.id.btn9, R.id.btnDot, R.id.btnResult, R.id.btnPlus, R.id.btnSub, R.id.btnMul, R.id.btnDiv, R.id.btnC, R.id.btnOpen, R.id.btnClose }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.layout_calculator); connectView(); } private void connectView() { tvMath = (TextView) findViewById(R.id.tvMath); tvResult = (TextView) findViewById(R.id.tvResult); for (int i = 0; i < idButton.length; i++) { findViewById(idButton[i]).setOnClickListener(this); } } @Override public void onClick(View view) { } }
Have you noticed in the code on the connections and Result TextView is normal Math, Button connects individual is not normal. The reason that I realized there are so many buttons to connect and start the event they need both. So the idea is to be able to enter data loop to repeat the same work that? So they have for their id into an array idButton. Per View substantive id is converted to integer types in the system, so this array of type int. Also the connection and start the event for each button in the loop also differ. We do not cast the connection button that has just captured events immediately via the chain of command findViewById(idButton[in]).setOnClickListener(this);
The reason is that the function findViewById brings us a View (and since then usually we pressed on its true style as Button, TextView,…) Per View feature that can catch the Click event (including TextView, EditText,…) Should we call for immediate setOnClickListener then without a cast on Button.
Now the next task is handling the button click event. Here we split up to do 3 kind of button: Button number, Button Button Handle math and math.
Button numbers and button operation, when clicking any buttons, we just add the text corresponding to the expressions. Button longer handle (button C và button = ) then perform the calculation or clear the screen. When you click Button by, we call valueMath of objects in the library Balan Balan that we have added to the original, it will count for us results.
package com.nguyenvanquan7826.tut5linearlayout; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.TextView; import cachhoc.net.Balan; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private TextView tvMath; private TextView tvResult; private int[] idButton = { R.id.btn0, R.id.btn1, R.id.btn2, R.id.btn3, R.id.btn4, R.id.btn5, R.id.btn6, R.id.btn7, R.id.btn8, R.id.btn9, R.id.btnDot, R.id.btnPlus, R.id.btnSub, R.id.btnMul, R.id.btnDiv, R.id.btnOpen, R.id.btnClose, R.id.btnC, R.id.btnResult }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.layout_calculator); connectView(); } private void connectView() { tvMath = (TextView) findViewById(R.id.tvMath); tvResult = (TextView) findViewById(R.id.tvResult); for (int i = 0; i < idButton.length; i++) { findViewById(idButton[i]).setOnClickListener(this); } init(); } private void init() { tvMath.setText("|"); tvResult.setText("0"); } @Override public void onClick(View view) { int id = view.getId(); // check button number and button operator for (int i = 0; i < idButton.length - 2; i++) { if (id == idButton[i]) { String text = ((Button) findViewById(id)).getText().toString(); // clear char | on top if (tvMath.getText().toString().trim().equals("|")) { tvMath.setText(""); } tvMath.append(text); return; } } // clear screen if (id == R.id.btnC) { init(); return; } // calculation if (id == R.id.btnResult) { cal(); } } private void cal() { String math = tvMath.getText().toString().trim(); if (math.length() > 0) { Balan balan = new Balan(); String result = balan.valueMath(math) + ""; String error = balan.getError(); // check error if (error != null) { tvResult.setText(error); } else { // show result tvResult.setText(result); } } } }
That's it, Our results here offline.
darling if there are keys on sin cos tan handle sir tn
Libraries can calculate the sine đk(x), with(x), so(x). You just write normal.
The sealed when his brother ) = thi` app and press die equipment , Do not Know How A xu ly
:The
Need own algorithm for the sin cos tan k a!Star has a k
In a library you've worked out what it already.
he oi cai tao when you layout(land) just redesign to the landscape view for his post, the app is kill and this error , I would like to ask how to fix sir
08-01 01:13:12.811 15250-15250/com.example.dell.calculator E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.dell.calculator, PID: 15250
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.dell.calculator/com.example.dell.calculator.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2338)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3930)
at android.app.ActivityThread.access$900(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1327)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5292)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.dell.calculator.MainActivity.connectView(MainActivity.java:42)
at com.example.dell.calculator.MainActivity.onCreate(MainActivity.java:34)
at android.app.Activity.performCreate(Activity.java:5264)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2302)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3930)
at android.app.ActivityThread.access$900(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1327)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5292)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
at dalvik.system.NativeStart.main(Native Method)
You try to catch the event instead redirect the screen to fix offline.
Thank you sir tut
He asked me to add new, I want to catch people not to enter the computer as indeed was wrong sir, like not enter more than two signs . in 1 number, parentheses not closed before opening parenthesis, not be divided by the number 0… What they do sir
So you have to use this thing and event for onTextChange EditText is offline.
library or create your libs are available ad
Share your ZIP PES my friend.
Email: nguyenngocluh14@gmail.com
In all your code always nhé. Interface code in all 5, look right on the head is found all 5.
A faulty part oi f calculate square roots 2 and %.
– If you enter you will notice such √4 “String input math”
– And if pressing 50% the characters % not append to the TextView and pressed by only one of the other strictly alone.
So the fault of how afraid sir, expect a explanation.
Thank you!
Apartments 4 Where they have been right.
Oh let me ask a file is the file balan.java huh balan.jar ?
.jar file is build, .is java source files.
honey, can I ask what this error is and how to fix it
“C:\Program FilesAndroidAndroid Studiojrebinjava.exe” -agentlib:jdwp=transport=dt_socket,address=127.0.0.1:54743,suspend=y,server=n -javaagent:C:\UsersAdmin.AndroidStudio4.0systemgroovyHotSwapgragent.jar -javaagent:C:\UsersAdmin.AndroidStudio4.0systemcaptureAgentdebugger-agent.jar -Dfile.encoding=windows-1252 -classpath “C:\Program FilesAndroidAndroid Studiolibidea_rt.jar” -jar C:\UsersAdminAndroidStudioProjectsandroid_co_banapplibsbalan.jar
Connected to the target VM, address: ‘127.0.0.1:54743', transport: ‘socket’
no main manifest attribute, in C:\UsersAdminAndroidStudioProjectsandroid_co_banapplibsbalan.jar
Disconnected from the target VM, address: ‘127.0.0.1:54743', transport: ‘socket’
Process finished with exit code 1
You can debug, but the other one I see the socket related report