Android: how to multiply values from EditText? - android

Ok, first thing is that I am new in Android development, please do not shoot me for the question.
So, I am developing an app yhat needs to multiply from 3 EditTexts:
resultsEditText
amountEditText
taxEditText
They are set with the same name in the R.java. what I want is the following:
amountEditText * taxEditText = resultsEditText
I have no idea in how to implement this, I have searched the internet and this site, which I use as a reference for all my Android development needs, and all the code I found doesnt work at all. I dont know what else to do. Thanks in advance!

You need to set EditText input type as number as well so user can input only numbers.
int a = Integer.parseInt(resultsEditText.getText().toString().toTrim());
int b = Integer.parseInt(amountEditText.getText().toString().toTrim());
int c = Integer.parseInt(taxEditText.getText().toString().toTrim());
int result = a * b * c;

Button btnCal,btnSub;
EditText editLen,editWid,editFeet;
int a,b,res;
btnSub=(Button)findViewById(R.id.btnSub);
btnCal=(Button)findViewById(R.id.btnCal);
editLen=(EditText)findViewById(R.id.editLen);
editWid=(EditText)findViewById(R.id.editWid);
editFeet=(EditText)findViewById(R.id.editFeet);
btnCal.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
a = Integer.parseInt(editLen.getText().toString());
b = Integer.parseInt(editWid.getText().toString());
res=a*b;
editFeet.setText(String.valueOf(res));
}
});

Related

Accessing object from inside OnClick(view v)

I am attempting to create an application in Android.
I have a written a number of classes. The code below is the simplest of them, but it is sufficient to illustrate the problem I have not been able to solve.
public class Letter
{
private char letter;
public Letter( )
{
letter = ' ';
}
public void setLetter(char c)
{
letter = c;
}
public char getLetter( )
{
return letter;
}
}
I can define an object of this class with the following code: L = new Letter( );
If I want to set the value of letter, I can use the code L.setLetter(‘a’). Similarly, if I want to access this letter I can use the code L.getLetter( );
The problem is how to gain access to the letter object from inside OnClick(view v). The situation is further complicated by the fact that I need to perform the set operation on letter with one button and the get operation by another button.
Any help you can provide me will be greatly appreaciated.
I have solved the problem I proposed.
final Letter M = new Letter();
final New_Button button_Q = new New_Button(this);
button_Q.setText("Q");
button_Q.setId('A');
button_Q.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view)
{
char c = button_Q.getText( ).charAt(0);
M.setLetter(c);
}
});
Declaring my objects final was the key to developing the solution. Thank you francis for commenting on my code. Any further points you wish to add would be appreciated.

How to total values in another Activity?

i need to sum the values in the second activity. I can not get it to total correctly. would someone be kind enough to help me?
final EditText et = (EditText)findViewById(R.id.etwalkingburned);
final EditText ed = (EditText)findViewById(R.id.etrunningburned);
mcardiototalbutton = (Button)findViewById(R.id.cardiototalbutton);
mcardiototalbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int walkingburned = Integer.parseInt(et.getText().toString());
int runningburned = Integer.parseInt(ed.getText().toString());
Intent myIntent = new Intent(getApplicationContext(),TotalActivity.class);
myIntent.putExtra("CardioTotal",walkingburned);
myIntent.putExtra("CardioTotal",runningburned);
startActivity(myIntent);
}
});
}
public class TotalActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.totalactivity);
Bundle extras = getIntent().getExtras();
int walkingburned = extras.getInt("CardioTotal");
int runningburned = extras.getInt("CardioTotal");
int cardiototal = walkingburned + runningburned;
TextView tv = (TextView)findViewById(R.id.cardiototalresult);
walkingburned = walkingburned + 0;
runningburned = runningburned + 0;
cardiototal = walkingburned + runningburned;
tv.setText("Cals.:" + cardiototal);
}
The problem is the way you are putting and getting extras to/from your intent.
myIntent.putExtra("CardioTotal",walkingburned);
myIntent.putExtra("CardioTotal",runningburned);
Think of an extra as a key-value pair. You should have a different string as the first parameter each time you call putExtra, otherwise you will overwrite the value of what you've already put in. So basically you put in the walkingburned value and then overwrote it with the runningburned value because you used the same key string.
Likewise, in your second activity you need to reference the values by their unique string "key" or you will just get the same value two times.
In this situation, there is more than one way to skin a cat. More specifically, you could do the addition in your first activity and only store one extra (the sum) to avoid the confusion of multiple extras. But, I think it would benefit you more to understand how extras and intents works.
For starters, instead of using "CardioTotal"for each extra, you can simply call the string something similar to your variable name. This is what I do and it would make your code easier to understand in my opinion.
myIntent.putExtra("Walking Burned",walkingburned);
myIntent.putExtra("Running Burned",runningburned);
It doesn't really matter what String you put in the first parameter as long as you know that they are supposed to represent different values.
The second change you would then need to make is how you get the extra out of the intent.
int walkingburned = extras.getInt("CardioTotal");
int runningburned = extras.getInt("CardioTotal");
What you are currently doing is retrieving the same value (you are using the same key string therefore it is the same value) to two separate variables. So the value you would be seeing is double the runningburned value.
Now, since you have two different Strings for the two different variables in your intent, you can just say:
int walkingburned = extras.getInt("Walking Burned");
int runningburned = extras.getInt("Running Burned");
The rest of your code is fine.
You also don't need these lines in your TotalActivity
walkingburned = walkingburned + 0;
runningburned = runningburned + 0;
Sorry this was a really long answer but I wanted to explain a few things.
Hope it helps!

Button Text and Booleans (Simple Android App)

I'm build the Fun Facts app on the Android Development Track. I decided to take a exploratory detour and try to create a very basic introductory message to the user. I changed the factTextView text to "You can click the button below to see a new fact!" and changed the showFactButton text to "Try it out!"
From there, I changed the final line onClick object (is that an object?) to the following:
public void onClick(View view) {
String fact = mFactBook.getFact();
// Update the label with our dynamic fact
factLabel.setText(fact);
// Set button text to new fact prompt
showFactButton.setText("Show another fun fact.");
This seems to work fine. However, I feel like "updating" the button text to the same new string on every press isn't always the best practice, even if it is easy and readable. I tried to add a boolean that will check the text of the button, and update it only if it has not already been updated. This is what I've come up with so far:
View.OnClickListener listener = new View.OnClickListener() {
#Override
public String launchText = getResources().getString(R.string.start_text);
public String nextText = getResources().getString(R.string.next_text);
public String buttonText = (String) showFactButton.getText();
public boolean updateLaunchText() {
if (buttonText.equals(launchText)) {
buttonText.replaceAll(launchText, nextText);
return true;
}
else {
return true;
}
}
public void onClick(View view) {
String fact = mFactBook.getFact();
// Update the label with our dynamic fact
factLabel.setText(fact);
}
};
With the following added to strings.xml:
<string name="start_text">Try it out!</string>
<string name="next_text">Show another Fun Fact!</string>
No errors, but the button text stays on "Try it out!" I'm sure that all the extra objects are totally unnecessary compared to the first, working method for the scope of this app, but I'd still like to figure it out since I don't really have any idea what I'm doing with the boolean.
Questions: 1) What am I missing in the longer boolean approach? 2) What's the actual most efficient approach to accomplish this task?
Did you connect the listener to the button object?Without that connection no logic is applied to a button click.It goes like this:
buttonName.setOnClickListener(...)
You'd have to initialize the button object first though :)
Where r u call to method updateLaunchText() ?
you should change the objects to global object (not to create the into the listener):
private String launchText = getResources().getString(R.string.start_text);
private String nextText = getResources().getString(R.string.next_text);
private String buttonText = (String) showFactButton.getText();
and take the method updateLaunchText() out of the listener too.
and then into the onClick(View view) call to updateLaunchText() like this:
public void onClick(View view) {
updateLaunchText();
String fact = mFactBook.getFact();
// Update the label with our dynamic fact
factLabel.setText(fact);
}

Basics ---> Checking strings using if else to set value of an int, possible wrong use of onResume

So I'm still working on my first little app here, new to Android and Java, so I'm stuck on a basic little problem here. Answers to my first questions were really helpful, so after researching and not coming up with anything, I thought I'd ask for some more help!
The idea is that on another screen the user makes a choice A, B, C, or D, and that choices is passed as a string through the intent. OnResume checks if the choice is not null and sets an integer that corresponds to that string. Later when the user pushes another button, some if else logic checks that int and performs and action based on which was chosen. The problem is that the App crashed at onResume.
I learned that I have to use equals(string) to compare string reference, but maybe the problem is that I am trying to compare a string in reference to a literal string? Any help would be greatly appreciated.
Thanks!
protected void onResume() {
super.onResume();
// Get the message from the intent
Intent intent = getIntent();
String choice = intent
.getStringExtra(ExtensionSetupSlector.TORQUE_SETUP);
// Create the text view
TextView displayChoice = (TextView) findViewById(R.id.displayChoice);
if (!choice.equals("")){
displayChoice.setText(choice);
if (choice.equals("A")) {
myChoice = 1;
}
if (choice.equals("B")) {
myChoice = 2;
}
if (choice.equals("C")) {
myChoice = 3;
}
if (choice.equals("D")) {
myChoice = 4;
}
}
}
myChoice is declare right after ...extends Activity{ Also I'm not quite sure If this should really be in onResume, but it was working before I started try to set myChoice in the onResume (when I was just displaying the choice). Thanks again!
Change if (!choice.equals("")) to check for null instead. Otherwise your app attempts to access an empty reference and crashes.

Divide two values obtained from EditText ad divide them by each other

Is it possible in Java code to divide two obtained values from two EditText boxes in an android app, and then divide them by each other to create a result?
Pretty much, the consumer of my application will be asked for two DIFFERENT, numerical values to be inputted into each box.
What I want to do with each value is produce a mathematical sum.
E.g.
If the first EditText box contains "22"
and the second EditText contains "11"
I want to be able to take those two values and divide them by each other to produce a value which I can further use.
In this case, that value produced would be 2.
22 / 11 = 2
I've already imported these two values into another Class (labeled DataHelper) through className.insert(stringName).
public class DataHelper extends Activity{
public static void insert(String firstFB, String firstRL) {
// TODO Auto-generated method stub
}
}
That's the Class for DataHelper with the two String values.
I have tried using IEEEremainder(x, y), but I don't know exactly how to use it since I am a novice at this language.
Can anyone give me some help with this? Any help at all will be appreciated.
Regards,
Mike.
if under standing rigtig you cut due
int one = int value1 / int vlaue2;
but then when you get the value from a edittext you get a string so you need to convert it to a int first int a = new Integer("value from edittext").intValue();
I believe you want something like this:
public class DataHelper extends Activity{
public static void insert(String firstFB, String firstRL) {
int fb = Integer.parseInt(firstFB);
int rl = Integer.parseInt(firstRL);
int division = fb / rl; // Or use double if needed
}
}

Categories

Resources