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

  • SEARCH
  • Home
  • 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 9010071
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T02:21:46+00:00 2026-06-16T02:21:46+00:00

I am new to android programming and so please pardon if the question looks

  • 0

I am new to android programming and so please pardon if the question looks stupid.

I am creating a Calculator in Android and for the user interface I have many buttons (around 20 for 10 digits, and various operation). Now there is a string expression that I calculate once the user presses the button “=”. However if he presses any other button then the input is updated. Say he presses “1” then input =1; then he presses 2 then input becomes “12” and so on.
So I need to call the same function whenever various buttons are pressed, but the input to the function is different.
I can go-by this by making n different functions, one for each button but that is not very scalable.
So how can I go about it?

The current xml file is:

<Button
    android:id="@+id/Button01"
    android:layout_width="70dp"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/Button03"
    android:layout_alignBottom="@+id/Button03"
    android:layout_toRightOf="@+id/Button03"
    android:onClick="UpdateExpression_/"
    android:text="/" />

<Button
    android:id="@+id/Button02"
    android:layout_width="70dp"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/Button01"
    android:layout_alignBottom="@+id/Button01"
    android:layout_toRightOf="@+id/Button01"
    android:onClick="UpdateExpression_X"
    android:text="x" />

I need to update to android:onClick=”UpdateExpression” and mention some input to this function call.

Thank you.

  • 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-16T02:21:47+00:00Added an answer on June 16, 2026 at 2:21 am

    You will need a central onClick method, let’s call it updateExpression(View v) Also a note about your code: method names should start with a lowercase letter, it’s a Java naming convention.

      android:onClick="updateExpression" 
    

    Now the implementation:

    public void updateExpression (View v)
    {
      switch (v.getId())
      {
        case R.id.button1:
        //do stuff here
        break;
        case R.id.button2:
        //do other stuff here
        break;
      }
    }
    

    The reason you need v.getId() is because you’re checking the id of the button then doing something if it’s that particular id. This logic is needed since all your buttons will implement that same method.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am quite new to Android programming, so please excuse me for the stupid
I fairly new to programming for the Android platform and have some question about
I'm VERY new to android programming, so please forgive. I have an assignment in
I'm fairly new to Android programming, so this may be a simple question, but
Hey everyone. I'm kinda new to android programming so please bear with me. I'm
I'm abit new to programming Android App's, however I have come across a problem,
i am new to android programming.i looking to make a question and answer application.in
[I'm quite new with Android programming so please excuse me for my nooby questions]
I am new to Android programming. I have installed the Eclipse and Android SDK.
I am very new to Android programming, and I have read everywhere and I

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.