Need help getting buttons to work...

Posted by Mike Droid on Stack Overflow See other posts from Stack Overflow or by Mike Droid
Published on 2010-04-06T19:20:47Z Indexed on 2010/04/06 19:23 UTC
Read the original article Hit count: 175

Filed under:

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?

© Stack Overflow or respective owner

Related posts about android