Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8883315
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:47:03+00:00 2026-06-14T20:47:03+00:00

I am trying to calculate a field named lblAnswer by adding values txtA +

  • 0

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>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T20:47:04+00:00Added an answer on June 14, 2026 at 8:47 pm

    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;
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to calculate the sub_total field based on: total_full + total_half. however
I am trying to calculate sum of 'price' field of an 'observableArray'. I have
I'm trying to write a simple function in C that would calculate the difference
Am trying to calculate the number of rows in a table depending on a
am trying to calculate mean and variance using 3X3 window over image(hXw) in opencv...here
I am trying to calculate number of users, cumulatively for the dellstore2 database. Looking
I'm trying to calculate the time it takes to receive all data from a
I am trying to calculate a ranking of a team in an ordered MySQL
I am trying to calculate a position to place label on the screen. The
I'm trying to calculate elapsed hours between two times. When calculating PM to AM

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.