I am trying to calculate a field named lblAnswer by adding values txtA + txtB. I am fairly new to the android development world and would like to know what is the best way of going about this. I have already added the necessarily edit fields to the GUI. I am now working in the java file to try and create the method. This method has been named doCalc. Here is what I have thus far.
public void doCalc()
{
lblAnswer = txtA + txtB;
}
It has been suggested that I add more code here is the full code. Thank you for that suggestion.
Here is the Java File.
package com.example.wattsprofessional;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public void doCalc()
{
lblAnswer = txtA + txtB;
Double.parseDouble(txtA.getText().toString());
lblAnswer.setText"t
}
and here is the xml file.
<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"
tools:context=".MainActivity" >
<EditText
android:id="#+id/txtA"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:ems="10"
android:hint="Write Here"
android:inputType="numberDecimal" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/txtB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/txtA"
android:layout_below="#+id/txtA"
android:layout_marginTop="32dp"
android:ems="10"
android:hint="Second Here"
android:inputType="numberDecimal" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#string/calculate"
android:onClick="doCalc"/>
<TextView
android:id="#+id/lblAnswer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/button1"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:text="TextView" />
</RelativeLayout>
Your code is missing a few key components. Review your code, and review the one I have prepared below.
package com.example.wattsprofessional;
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;
import android.widget.TextView;
public class MainActivity extends Activity {
private EditText txtA, txtB;
private Button button1;
// ^ we have declared these as fields up here so that we can access them throughout the page, past all the curly brackets
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtA = (EditText) findViewById(R.id.txtA);
txtB = (EditText) findViewById(R.id.txtB);
button1 = (Button) findViewById(R.id.button1);
// ^ this is where we initialize these. You did the xml correctly, but you still need to hook the java to it.
// it allows us to use any names and locations we like not just same ones.
// basically you say what it is (Button) and then use the following method to look for the id that you wrote in the xml
initButton();
// i made this listener so we'd have time. this is the oncreate method and is called instantly.
// if we called doCalc here, we'd have no time to put numbers in.
}
private void initButton() {
button1.setOnClickListener(new OnClickListener() {
// this one performs an action when our button is clicked. it performs whatever is below
#Override
public void onClick(View v) {
String strA = txtA.getText().toString();
String strB = txtB.getText().toString();
// we get our strings from our editexts. i think you know how to do this well.
Double dblAnswer = doCalc(strA, strB);
// ^we pass them to our method, it does all the heavy lifting for us. and spits an answer for us.
TextView lblAnswer = (TextView) findViewById(R.id.lblAnswer);
// this is a local variable, as opposed to a field. i made so you know that you can do it like this - with the whole line here
// the disadvantage is that we can't do anything to it outside of this curly bracket. but there are performs gains.
// in general it's wasteful to use fields when you can suffice with local variable
String answer = String.valueOf(dblAnswer);
// we get our answer and turn it to a string.
lblAnswer.setText(answer);
// finally we set our result to the textView.
}
});
}
public double doCalc(String a, String b) {
// a and b are both variables. they refer to the stuff we put in
double dblA = Double.parseDouble(a);
double dblB = Double.parseDouble(b);
// we're gonna make both of these numbers so we can add them. right now they're just text.
return dblA + dblB;
// ^ this statement means that this method will spit a number out when it's done which we can use however.
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
In order to get a Double value from an EditText, you'll need to use Double.parseDouble(txtA.getText().toString()). To set the text, you can use lblAnswer.setText("text").
In addition, the easiest way to call this from a button would be to set its android:onClick attribute in the XML, such as android:onClick="doCalc".
EDIT: You also need to create references to your objects. Before your onCreate(), put:
EditText txtA;
EditText txtB;
TextView lblAnswer;
Then inside your onCreate() you need to initialize the objects:
txtA = new (EditText)findViewById(R.Id.txtA);
txtB = new (EditText)findViewById(R.Id.txtB);
lblAnswer = new (TextView)findViewById(R.Id.lblAnswer);
Related
i am having issues getting the coding for onClick to work correctly i have a main activity that contains 8 buttons button 1 should open face book to a page 2,4,5,6 and 7 should open a web browser to defined page and button 8 to open phone dialer with preset number to call. I can not get this to work with out crashing the app i can set one onclick listener to button 1 and get it to work but when i add the next one it crashes the app i get no errors in eclipse i was told to do it with a fragment to handle the onclicks but im lost on how to do this can any one help me with this coding???
This is what i have so far just for 2 buttons still haven't got the one to open face book or the one to open phone dialer but this is as far as i get then it crashes – Jerry 33 mins ago
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
#Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addButtonClickListner();
}
public void addButtonClickListner() {
Button btnNavigator = (Button)findViewById(R.id.imageButton2);
btnNavigator.setOnClickListener(new OnClickListener(){
public void onClick(View arg) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("www.*****.com"));
startActivity(intent);
}
});
}
Do this instead
....
package com.example.testcode;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
// Test code: import android.widget.TextView;
// Let your class implement the OnClickListener interface directly. This
// will let you use the onClickListener
class MainActivity extends Activity implements OnClickListener{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// "find" our views by their id's in our activity's layout
Button button1 = (Button) findViewById(R.id.button1);
Button button2 = (Button) findViewById(R.id.button2);
//...... continue for button3 - button8
// set our "click" listeners for each of our buttons
button1.setOnClickListener(this);
button2.setOnClickListener(this);
//...... continue for button3 - button 8
}
// Because our class implements the OnClickListener interface
// it will be listening for "clicks". Because of this, we can
// override the click listener's default onClick(View v) method.
// View v is our view, or our button, that is "clicked".
#Override
public void onClick(View v) {
// Test code: TextView text = (TextView) findViewById(R.id.text);
// This is the statement that will allow each of your buttons
// to perform different processes. For my test code, I have each
// button reset the TextView I have displayed in the top of my
// layout.
switch(v.getId()){
case R.id.button1:
// Test code: text.setText("Button 1");
break;
case R.id.button2:
// Test code: text.setText("Button 2");
break;
//...... continue for button3 - button 8
default:
Log.d(getApplication().getPackageName(), "Button click error!");
break;
}
}
}
Please take a look at my comments. The portion that is commented as
// Test Code:
is code that is pretty much useless for what you are doing. The Test Code from the switch statement is what you need to replace with what you want to do with each individual button. It is also important to note that where I say "//...... continue for button3 - button8" I simply mean to repeat the pattern that I have started with the first two buttons with the remaining six.
If you would like to test it with my given test code, here is my layout (just make sure you erase "// Test Code: " so that the test code lines are no longer commented out:
<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Press a button"
android:id="#+id/text" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 1"
android:id="#+id/button1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 2"
android:id="#+id/button2" />
</LinearLayout>
NOTES:
It is important to understand that in "good" xml layouts, the "android:text=" portion will be set to 'android:text=#string/"string_id"' instead of some general string to help with localization.
Make sure you change the very top line that declares the package to whatever package you have the Activity placed in. The name of this package is found at "Application"/src/"package_name" and is usually something like com.example.applicationname
I also want to say that I have included the comments in the code so that it can be copied and pasted without having to revisit this link over and over again in case anyone wants to use the code as a guide, not because I wanted tons of code posted in the answer.
Sometime onClick listener doesn't work because of some other
transparent view is on top of your view.
First of all I'm totally new with Eclipse so my Code maybe full of errors *
Second I wrote a simple App about two buttons (Add,Sub) with One TextView , the idea is about click one of these buttons and then the TextView changes and gives a number (Add=+1 , Sub = -1) , I'm Sure Compiler gave No Errors
But I don't know why I have a lot of problems :
1-The Apk file is not generated in the bin folder .
2-I tried to Export it using Android tools , and then tried it on my mobile and it gave black Screen then stopped working.
3-I got 3 warnings in The Activity_Main.xml about buttons and textview "Hardcoded String Should use #String Resource "
I'm totally Confused , I don't know how to fix it i tried a lot and searched on the internet but i couldn't
Here is the Main_Activity.Java :
package com.example.counter;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
int Counter = 0;
Button A = (Button) findViewById(R.id.Add);
Button S = (Button) findViewById(R.id.Sub);
TextView C = (TextView) findViewById(R.id.Calculate);
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
A.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Counter ++ ;
C.setText("Your Total Counter Is " + Counter);
}
});
S.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Counter -- ;
C.setText("Your Total Counter Is " + Counter);
}
});
}}
And here is the Activity_main.xml :
<TextView
android:id="#+id/Calculate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="21dp"
android:text="Waiting To Calculate"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="#+id/Sub"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/Add"
android:layout_alignBottom="#+id/Add"
android:layout_alignRight="#+id/Calculate"
android:text="Sub" />
<Button
android:id="#+id/Add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/Calculate"
android:layout_below="#+id/Calculate"
android:layout_marginTop="66dp"
android:text="Add" />
The app suppose work even on API1 and Targeted API 17 (Jelly Bean) I tested it on Galaxy S3 Mini (have Android 4.2) and it crashed , tried simple app before with same settings and worked
*Any Help is Appreciated , thanks for your time and Sorry for make it Long *
The 2nd problem is on the View (e.g. Button, TextView) initialization.
Since they are declared and initialized at class scope, they are initialized before onCreate() (and thus setContentView()) is called. Because there is no layout inflated at the moment, the initialization will return null. Then, when setOnClickListener() is called, it will throw NullPointerException and crashes the app.
The solution is to move the initialization after setContentView() is called.
public class MainActivity extends Activity {
int Counter = 0;
Button A;
Button S;
TextView C;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
A = (Button) findViewById(R.id.Add);
S = (Button) findViewById(R.id.Sub);
C = (TextView) findViewById(R.id.Calculate);
A.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Counter++;
C.setText("Your Total Counter Is " + Counter);
}
});
S.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Counter--;
C.setText("Your Total Counter Is " + Counter);
}
});
}
}
For the 3rd problem, just as Juwee posted, you can define the String resources to be used in XML layout file (it can be used in the code too!).
Create an XML file in /values folder with <resources> root element (or just use strings.xml if it's already there). Then, you can declare a string resource with String value
Example
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="add">Add</string>
</resources>
Then, on the XML layout, you can refer it as android:text="#string/add" (Or in code as setText(R.string.add))
Since this is just only a warning, you can actually ignore it. But if you decide your app to support localization, or if you use the same String quite often, then it is better to define it as app's resource.
Third problem : About this property android:text="Sub" ,you can define the text in the resource file "string.xml",like <string name='subString'>sub</string>,then make reference to it.android:text="#string/R.string.subString" . Have a try .
I need help understanding how to accomplish math between different EditText views. I am not asking someone to write me the code but maybe explain what is involved to get this done.
I wanted to post a picture of this but as a new user I can not. Basicly I have a EditText for the following: Width, Length, Eave Height, Pitch.
I have ID's for all the TextViews I just dont know how to program the behind the scenes math involved to make them work. I do have the equations needed to perform the math just not sure where and how to put them in java.
Basicly I need the user to enter a number in each of the top 4 boxes. I need to use an equation to generate the answer that will be displayed in the "SQFT" box. The user will also input a number in a cost box which will generat a "Total" that needs to be displayed in a separate TextView.
Any help would be appreciated, even if it is to point me in a direction of a tutorial to get me started. Thanks for your help.
Just to show what type of math I need to use, below is the equation I use for excel to calulate.
(length+width)*(Eave+1)*2 + (((width/2)/12*Pitch)*(width/2)*2)
I'm not sure if you don't know how to extract the numbers entered in the EditTexts, how to actually do the math calculation, how to let the user initiate the calculate or how to present it.
I created a small demo that has 2 EditTexts, and a TextView that displays the sum of the numbers entered. The user does not need to press any buttons to perform the calculation, it is performed automatically every time the user updates the text (I assumed this is what you wanted).
Please note this code is not good code, it uses lots of internal anonymous classes etc but it supposed to demonstrate the mechanics of how to do this.
This is the main.xml layout file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<EditText
android:id="#+id/a"
android:hint="input a"
android:inputType="number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:minWidth="60dp"/>
<EditText
android:id="#+id/b"
android:hint="input b"
android:inputType="number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:minWidth="60dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="a+b = " />
<TextView
android:id="#+id/total"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp" />
</LinearLayout>
And this is the sample Activity:
package com.example;
import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.widget.EditText;
import android.widget.TextView;
public class SumActivity extends Activity
{
private int a;
private int b;
private TextView totalOutput;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
EditText inputA = (EditText) findViewById(R.id.a);
EditText inputB = (EditText) findViewById(R.id.b);
totalOutput = (TextView) findViewById(R.id.total);
inputA.addTextChangedListener(new TextChangedListener()
{
#Override
public void numberEntered(int number)
{
a = number;
updateTotal();
}
});
inputB.addTextChangedListener(new TextChangedListener()
{
#Override
public void numberEntered(int number)
{
b = number;
updateTotal();
}
});
}
private void updateTotal()
{
int total = a + b; // This is where you apply your function
totalOutput.setText("" + total); // need to do that otherwise int will
// be treated as res id.
}
private abstract class TextChangedListener implements TextWatcher
{
public abstract void numberEntered(int number);
#Override
public void afterTextChanged(Editable s)
{
String text = s.toString();
try
{
int parsedInt = Integer.parseInt(text);
numberEntered(parsedInt);
} catch (NumberFormatException e)
{
Log.w(getPackageName(), "Could not parse '" + text + "' as a number", e);
}
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after)
{
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count)
{
}
}
}
I have a EditText that I have set to invisible by default. I would like to make this box visible onclick of a ImageView, but cant find any documentation online to help me, how would I go about doing this?
In your xml
<EditText
android:id="#+id/my_edit_text"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="1"
android:singleLine="true"
android:inputType="number"
android:visibility="gone"
android:paddingRight="8dp" />
In your Activity Class
import android.widget.Button;
import android.widget.EditText;
public class MyActivity extends Activity {
private EditText editTxt = null;
private Button myBtn = null;
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.my_xml_layout);
editTxt = (EditText) findViewById(R.id.my_edit_text);
myBtn = (Button) findViewById(R.id.my_button);
myBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
editTxt.setVisibility(View.VISIBLE);
}
});
}
}
well instead of invisible you can make it disable. This way you can prevent it from user input. And it is very easy to enable or disable EditTex.
You can use JQuery to toggle display (display:none vs. display:block) on your EditText field. You probably want to start out with display:none on the EditTextBox. You will also need to have JQuery loaded in your page. Can't make it a detailed tutorial here so giving you enough to get to next level.
$jq(document).ready(function() {
$jq("#yourImageView").click(function(e) {
$jq("#yourEditTextBox").fadeToggle("slow", "linear");
});
I'm trying to create an Android app to help to calculate split the bill if let's say you're eating out in a party of 2 people or more.
You're supposed to enter the subtotal of the bill, enter the number of people in the party, enter applicable discount if any, there are 2 checkboxes for 7% tax, and 10% service charges if it hasn't been included in the bill yet. Finally you just need to click on the "calculate button" for the app to calculate how much each person has to pay.
My Questions are:
for subtotal, it's supposed to be double instead of int, but I'm not sure how to parse String into a double. Is there a way to do this?
I'm not sure if that is the best way to activate the Checkboxes for the tax and 10% tips
When I click on the calculate button, it is supposed to display the Toast message with the result of the calculation, but nothing appears. I'm not sure if the problem is with parseInteger, checkBoxes, or if the onClick method is wrong, or all of them.
Here's the code that I wrote:
package com.kevinw.BillSplitter;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class BillSplitter extends Activity implements OnClickListener {
/** Declares XML Widgets */
private EditText numberDiners;
private EditText enterAmount;
private EditText enterDiscount;
private CheckBox gst;
private CheckBox tips;
private CheckBox cess;
double result;
private Button calculate;
private TextView resultAmount;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Initialize Widgets
numberDiners = (EditText) findViewById(R.id.numberDiners);
enterAmount = (EditText) findViewById(R.id.EnterAmount);
enterDiscount = (EditText) findViewById(R.id.EnterDiscount);
calculate = (Button) findViewById(R.id.calculate);
//Initialize CheckBoxes
gst = (CheckBox) findViewById(R.id.cbCheck1);
gst.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (gst.isChecked()) {
result = result + (0.07 * result);
}
else {
result = result;
}
}
});
tips = (CheckBox) findViewById(R.id.cbCheck2);
tips.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (tips.isChecked()) {
result = result + (0.1 * result);
}
else {
result = result;
}
}
});
}
#Override
public void onClick(View v) {
//Initialize EditTexts
String amount = enterAmount.getText().toString();
int subtotal = Integer.parseInt(amount);
String diners = numberDiners.getText().toString();
int people = Integer.parseInt(diners);
String disc = enterDiscount.getText().toString();
int discount = Integer.parseInt(disc);
double discounted = discount / 100;
result = (1 - discounted) * (subtotal / people);
switch (v.getId()) {
case(R.id.calculate):
Toast.makeText(this, "The Amount a Person has to pay: $" + result, Toast.LENGTH_LONG).show();
break;
}
}
}
and if it helps, this is the XML code for the layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="#+id/dinersView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello"/>
<TextView
android:id="#+id/Enter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/dinersView"
android:layout_alignLeft="#+id/EnterAmount"
android:text="#string/enter"/>
<EditText
android:id="#+id/numberDiners"
android:layout_height="wrap_content"
android:layout_below="#+id/dinersView"
android:layout_width="100dip"/>
<EditText
android:id="#+id/EnterAmount"
android:layout_height="wrap_content"
android:layout_below="#+id/Enter"
android:layout_toRightOf="#+id/numberDiners"
android:layout_width="220dip"/>
<TextView
android:id="#+id/Discount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/EnterAmount"
android:layout_alignLeft="#+id/EnterAmount"
android:text="#string/discount"/>
<EditText
android:id="#+id/EnterDiscount"
android:layout_height="wrap_content"
android:layout_below="#+id/Discount"
android:layout_alignLeft="#+id/Discount"
android:layout_width="220dip"/>
<CheckBox
android:id="#+id/cbCheck1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/EnterDiscount" />
<CheckBox
android:id="#+id/cbCheck2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/cbCheck1" />
<TextView
android:id="#+id/gst"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/cbCheck1"
android:layout_alignTop="#+id/cbCheck1"
android:layout_alignLeft="#+id/enterDiscount"
android:layout_marginTop="10dip"
android:text="#string/GST"/>
<TextView
android:id="#+id/tips"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/cbCheck2"
android:layout_alignTop="#+id/cbCheck2"
android:layout_marginTop="10dip"
android:text="#string/tips"/>
<Button
android:id="#+id/calculate"
android:layout_below="#+id/cbCheck2"
android:layout_width="wrap_content"
android:text="#string/calculate"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"/>
</RelativeLayout>
Thank you for all of the help. Really appreciate it.
For string to double conversion you can use Double.valueOf("").
you need to add clicklistener to your calculator button, either add calculate.setOnClickListener(this);
or move your onClick code without switch case inside this block.
calculate.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
for subtotal, it's supposed to be double instead of int, but I'm not sure how to parse String into a double. Is there a way to do this?
Yes there is.
I'm not sure if that is the best way to activate the Checkboxes for the tax and 10% tips
That is not a question but, moving on, no, that is not the right way to write checkbox code.
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (gst.isChecked()) {
result = result + (0.07 * result);
} else {
result = result;
}
}
This code will not work as you expect it to. The price will just keep going up and up on each second tick of the checkbox. You may want to step through what will actually happen in your head or on paper and then try and rethink it.
When I click on the calculate button, it is supposed to display the Toast message with the result of the calculation, but nothing appears. I'm not sure if the problem is with parseInteger, checkBoxes, or if the onClick method is wrong, or all of them.
I think that you will find that the onClick function is never called because you have not called the setOnClickListener function. I think that might be the problem but I'm not sure.