Android app: Android Studio - formula calculator - android

I want to make a simple Android app for min.-version 2 for local use. The problem is I have never made Android apps. I've installed Android Studio and SDK Tools now, but I get errors when creating a blank app project. Actually I am a VB.NET programmer, I also know C#, PHP, but I've never done anything for smartphones before.
What I need, is a simple calculator with two text-boxes (e.g. txt1 (a) and txt2 (b) which are inputs as doubles), a calculate button, and a result-field (as double).. see an example-picture below.
When focusing a text-box a numeric keyboard must appear (like for a calc), with a decimal point button. And when I click on result-button then output must be calculated and printed following the formula below:
Can anybody give me a tip how to make this? Or can anybody tell where to write the code to make this app work?
Thank you.

So first you need to learn how android Layout XML files work. Then add 4 items: 2 EditTexts at the beginning, then one Button with the text "Calculate" and finaly one more EditText. Set for all of them some IDs (i.e. text1, text2, button, output).
Also don't forget to set all the items to have "match_parent" at "layout_width" and "wrap_content" at "layout_height" (for displaying as you want).
Then in your main activity class, in your on onCreate(Bundle savedInstanceState):
final EditText text1 = (EditText) findViewById(R.id.text1);
final EditText text2 = (EditText) findViewById(R.id.text2);
final EditText output = (EditText) findViewById(R.id.output);
text1.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
text2.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
output.setEditable(false);
Button calculate = (Button) findViewById(R.id.button);
calculate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (text1.getText().isEmpty() || text2.getText().isEmpty()) return;
double text1Value = Double.valueOf(text1.getText().toString());
double text2Value = Double.valueOf(text2.getText().toString());
double a, b;
if (text1Value > text2Value) {
a = text1Value;
b = text2Value;
} else {
a = text2Value;
b = text1Value;
}
double result = (a*a - b*b) / 4;
result = Math.sqrt(result);
output.setText(String.valueOf(result));
}
});
Hope I helped, announce me if there are any errors!

Related

How to use the Edittext in android

So I have 6 edit texts and a button as shown below:
My question is how do I use the input from the EditTexts (which I have stored in content_main.xml) to do mathematical operations like calculating an average which I want to show up in a toast when the calculate button is pressed. I have already written some code in the MainActivity.java file that brings up a toast when the calculate button is pressed (also in content_main.xml), I just need to figure out how to use the inputs from the EditTexts in the toast.
EditText myText // = findViewById...
String text = myText.getText().toString();
What you should do first is to give each of its elements ID to also recognize from the Activity.
Then you should use the click event of the button
//Here it is referring to the id that gave his element in its layout
Button button = (Button) findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
}
});
And finally, like the button, get their input values EditText
//Here it is referring to the id that gave his element in its layout
EditText text = (EditText)findViewById(R.id.editText01);
And in order to do math, parse the string value remaining on a double (double for decimals can give the exact calculation if you want something, if you want to be an int approximately)
try{
Double value = Double.parseDouble(text);
}catch(NumberFormatException e){
//Message for error parse
}

Displaying a result after receiving user input

I'm developing an app in Android and need to implement a common app function- accepting numeric input from the user via a EditText. After typing in this value, I want the user to be able to be select a 'go' button. A result should then appear below the first TextView. What should I use to display the result (TextView etc?) and how can I go about implementing this process. Thanks
You are new to this so First check d link http://www.tutorialspoint.com/android/android_event_handling.htm .
Here You have to define 1 TextView , 1 EditText and 1 Button
like
Button _go = (Button) findViewById(R.id._btngo);
than
_go.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
TextView _getdata= (TextView) findViewById(R.id._tvresult);
EditText _result= (EditText) findViewById(R.id._etgetinput);
// get the input value and store as a string
String result = _result.getText().toString();
// display the result in textview
getdata.setText(result);
}
});
Hope you will find the solution .. :)

Calculator button (Android) - Distinguishing between normal click and longClick (for sine and arc sine functions)

Below I've listed two functions. I have a button for "sine" function - which works perfectly and on LongClick I want to add the sine inverse function or arc sine.
***NOTE : The difference between the two functions below is just the one line where I assign afterSin variable, in one I use Math.sin in the other - Math.asin, that's it no more difference.
The problem is , normal click or long Click , I'm getting the "Sine" function only, even though I've coded asin function, I don't why but it seems to ignore it and gives the normal click function. (Again, on longClicking it doesn't give the arc sine function as I want it to, but simply gives the normal function "sine" , that is the same as normal OnClick function.)
Here's the normal OnClick :
public void onClickListener_sin(View v) {
vibrator.vibrate(30);
EditTextMsg = editText.getText().toString();
doubleEditTextMsg = Double.parseDouble(EditTextMsg);// degree
toRadian_doubleEditTextMsg = Math.toRadians(doubleEditTextMsg);
afterSin = Math.sin(toRadian_doubleEditTextMsg);
////////Only different line is the above one (Sin, here and ASin below )
editText.setText(afterSin.toString());
EditTextMsg = editText.getText().toString();
result = Float.parseFloat(EditTextMsg);
result_mul = Float.parseFloat(EditTextMsg);
result_div = Float.parseFloat(EditTextMsg);
sum = "";
}
AND
Here's the LongClick function (I want it to give asin, but it ignores the function and continues with the normal(above function and gives me "Sine" )
button_sin.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
vibrator.vibrate(30);
EditTextMsg = editText.getText().toString();
doubleEditTextMsg = Double.parseDouble(EditTextMsg);// degree
toRadian_doubleEditTextMsg = Math.toRadians(doubleEditTextMsg);
afterSin = Math.asin(toRadian_doubleEditTextMsg);
///////////////The above line is the only difference between this and the above ///////////function
editText.setText(afterSin.toString());
EditTextMsg = editText.getText().toString();
result = Float.parseFloat(EditTextMsg);
result_mul = Float.parseFloat(EditTextMsg);
result_div = Float.parseFloat(EditTextMsg);
sum = "";
return true;
}
});
I'm showing the results and inputs in a EditText box ...
(The result, result_mul, sum variables should not be relevant to this question, I copied them anyways because they are used for the respective functions in my calculators specific code... It shouldn't be related to the current problem)
So, Can you spot anything wrong ?
(It ignores the onLongClick....and simply carries out the normal Onclick function (The same function whether the click is normal or long)
Thank you.
It may help to enable the button's long-clickable attribute
button_sin.setLongClickable(true); // CODE
OR
android:longClickable="true" // XML
Then as pointed out by #Opiatefuchs, you should really be calling .setOnLongClickListener() in the same way as you provide the .setOnClickListener() for neatness' sake.
Hope it helps!

How to update just a part of the activity

Well, the title says everything, but there's a little problem, I can't use a fragment or a loader, because I'm developing for android 2.3, so, how can I update just a part of the activity?
You can still use a fragment even in 2.3 by using the android-support-v4 jar
You can reference the views by ID. As an example to change a EditText you can:
EditText text = (EditText) findViewById(R.id.input);
test.setText("New Text");
Just change the cast to the proper type, so for a button:
Button btn = (Button) findViewById(R.id.button);
So for your code if you have a button with the xml element android:onclick="increment" you can have a function such as:
public void increment(View view) {
TextView number = (TextView) findViewById(R.id.number);
number.setText(Integer.toString(Integer.parseInt(number.getText()) + 1));
}

Make a custom keyboard in Android having the same behavior as the soft keyboard

So, today I decided to try out Android, so please understand that I am a beginner in it.
What I want to achieve right now is to have a EditText, and a set of buttons to be used to enter data into the EditText.
What I've done currently is stick a set of button widgets in the XML layout, and I use this code to make the buttons insert stuff into the EditText:
final EditText inputline = (EditText) findViewById(R.id.textentry);
final Button my_button = (Button) findViewById(R.id.my_btn);
my_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
inputline.append("a");
}
});
This kind of works, but I need help with a few issues:
it always appends the character at the end of the string, not at the current cursor position
similarly, when I call inputline.selectAll() and press my button, it inserts the text at the end of the string again; whereas I want it to delete the text first (as it's selected) and then insert the character
it seems tedious to write all that code for each of the buttons I have. Is there a better way to do this altogether?
Thanks for your help!
I have now pretty much solved by replacing inputline.append("a"); etc. with my custom function, lineInsert(), which you can see below.
public void lineInsert(CharSequence text) {
final EditText inputline = (EditText) findViewById(R.id.textentry);
int start = inputline.getSelectionStart();
int end = inputline.getSelectionEnd();
inputline.getText().replace(Math.min(start,end), Math.max(start,end), text, 0, text.length());
inputline.setSelection(inputline.getSelectionEnd());
}
This has the same behavior as the soft keyboard.

Categories

Resources