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 731707
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T07:03:28+00:00 2026-05-14T07:03:28+00:00

I am trying to get my first button to update a display number in

  • 0

I am trying to get my first button to update a display number in my view when clicked. This view will have several buttons and “outputs” displayed. After reading examples and Q’s here, I finally put something together that runs, but my first button is still not working;

   public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ship_layout);
        mSwitcher = (TextSwitcher) findViewById(R.id.eng_val);
        }

    private TextSwitcher mSwitcher;

        // Will be connected with the buttons via XML
        void onClick(View v){

            switch (v.getId()) {
            case R.id.engplus:
                engcounter++;
                updateCounter();
                break;
            case R.id.engneg:
                engcounter--;
                updateCounter();
                break;
            }
        }
    private void updateCounter() {
        mSwitcher.setText(String.valueOf(engcounter));
    }

The .xml for this button is;

     <TextSwitcher
    android:id="@+id/eng_val"
    android:visibility="visible"
    android:paddingTop="9px"
    android:paddingLeft="50px"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/build"
    android:layout_toRightOf="@+id/engeq"
    android:textColor="#DD00ff00"
    android:textSize="24sp"/>       

This is within a Relative Layout that appears otherwise OK. When I had set the view to have a TextView with the number set as a string , the number displayed, but I could not figure out how to update the text with a numerical field. That may be my real problem.

I have gone through many examples generally referenced from the dev. site (UI, Common Tasks, various samples), and I am still not seeing the connection here…

Again, this is simply a try at getting variables to respond to buttons and update on the view.

So, a few Q’s for anyone that can help;

1) Is there any easier way of doing this (ie. send numerical value to View) ?
2) Why isn’t my TextSwitcher displaying the number?
3) Should I be using a TextSwitcher here?
4) Any examples of this you can point me to?

UPDATE!!!: Buttons with feedback are now working. Here is my fix, based on feedback, research and luck;

public class Myview extends Myapp implements ViewSwitcher.ViewFactory,
View.OnClickListener {

public int engcounter = 1;
private TextSwitcher mSwitcher;

@Override
public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_layout);
        mSwitcher = (TextSwitcher) findViewById(R.id.eng_val);
        mSwitcher.setFactory(this);


    Button engp = (Button) findViewById(R.id.engplus);
    engp.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view){
        engcounter++;
        updateCounter();
        }
    });

    Button engn = (Button) findViewById(R.id.engneg);
    engn.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view){
        engcounter--;
        updateCounter();       
        }   
    });

   }
    private void updateCounter() {
        mSwitcher.setText(String.valueOf(engcounter));
    }
    public View makeView() {
        TextView t = new TextView(this);
        t.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);
        t.setTextSize(36);
        return t;
    }
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

}

Some of this appears unnecessary at the moment (ie. Gravity on makeView), but it works! And should work for the 10-20 buttons I want to do misc things with.

The key was getting listeners right, and the whole set-up for TextSwitcher was abit awkward…still learning.

Any other final comments welcomed!

UPDATE UPDATE!!
I found this also;

http://developer.android.com/resources/articles/ui-1.6.html

  • 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-05-14T07:03:28+00:00Added an answer on May 14, 2026 at 7:03 am

    Your TextSwitcher isn’t displaying the numbers because you need to set the OnClickListener of your buttons in your onCreate() method. Currently your onClick() method is never getting called.

    Add this to your onCreate:

    Button plus = (Button) findViewById(R.id.engplus);
    plus.setOnClickListener(this);
    Button neg = (Button) findViewById(R.id.engneg);
    neg.setOnClickListener(this);
    

    And make sure your class implements View.OnClickListener

    As far as their being an easier way to show a numeric value in a TextView the way you are doing it is fine. You have to pass a String to setText(), and the way you are converting the number to a string is fine.

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

Sidebar

Related Questions

I am trying to get PhysX working using Ubuntu. First, I downloaded the SDK
Trying to get my css / C# functions to look like this: body {
Trying to get this example working from http://www.munna.shatkotha.com/blog/post/2008/10/26/Light-box-effect-with-WPF.aspx However, I can't seem to get
Just trying to get my head around Generics by reading this enlightening article by
I'm trying to get the following code to run (it's from the Heads First
I'm trying to get the value of two radio button groups using the jQuery
I am trying to get this Javascript in my application working. function validateQuantity(field) {
I was trying to update the contents of a JList when a button was
I'm trying get values from a GridView using the following code: foreach (GridViewRow row
Trying to get an ASP application deployed; it worked for a while but then

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.