tags:

views:

209

answers:

3

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

A: 

I would need to see more of your code to see exactly what is going wrong but here is the api demo from the android developer site. My guess is that you need to implement ViewSwitcher ViewFactory and View OnClickListener. I see that you have on click but the modifier doesn't match and I don't see a make view method in your code.

 
public class TextSwitcher1 extends Activity implements ViewSwitcher.ViewFactory,
        View.OnClickListener {

    private TextSwitcher mSwitcher;

    private int mCounter = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.text_switcher_1);

        mSwitcher = (TextSwitcher) findViewById(R.id.switcher);
        mSwitcher.setFactory(this);

        Animation in = AnimationUtils.loadAnimation(this,
                android.R.anim.fade_in);
        Animation out = AnimationUtils.loadAnimation(this,
                android.R.anim.fade_out);
        mSwitcher.setInAnimation(in);
        mSwitcher.setOutAnimation(out);

        Button nextButton = (Button) findViewById(R.id.next);
        nextButton.setOnClickListener(this);

        updateCounter();
    }

    public void onClick(View v) {
        mCounter++;
        updateCounter();
    }

    private void updateCounter() {
        mSwitcher.setText(String.valueOf(mCounter));
    }

    public View makeView() {
        TextView t = new TextView(this);
        t.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);
        t.setTextSize(36);
        return t;
    }
}
Dave.B
Yes, this is partially what I want, without the animation, and with a switch to handle my 10-20 buttons per view. Onclicklistner appears the way to go, Factory does not appear to help....but it still is not working...(see my response above)
Mike Droid
+2  A: 

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.

mbaird
Hmm...thanks. I see what you mean. I added those to my view and now it crashes (before it did nothing, but I could click on the buttons)...will continue looking at it...
Mike Droid
To clarify, it crashes only when I click on either of these 2 buttons. Other buttons and View otherwise is OK.
Mike Droid
You should have a stacktrace in your LogCat output that lets you know why it crashed. Post that here if you can't understand it.
mbaird
Update: Yeah, I finally got around to checking it. Its getting a Null pointer exception entering updateCounter. It looks like I am not setting up the TextSwitcher correctly. I also tried breaking up each button into it's own listner (I have successfully implemented several buttons on 1 view, just never tried to update variables on a view with buttons), instead of the switch, and it's doing the same thing. Other documentation found on an android dev issues page points to 2 methods of using TextSwitcher; 1) Using Factor and 2) Using 2 TextViews. Still trying to get this working.
Mike Droid
IT'S ALIVE! Thanks for the help. I still have misc. resource errors on log, but my buttons are now working! Updated top message with code. Now just to modify and see if I can get my switch working and I'm off...one small step...
Mike Droid
A: 

Hello,

I am working with Android. Can anybody tell me how can i create something like... When i click one button once....numerical digits will show me automatically as increasing order like 1,2,3,4......so on after every 5 sec...

I tried with this

public class TextSwitcher1 extends Activity implements ViewSwitcher.ViewFactory {

private TextSwitcher mSwitcher;
private int mCounter = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.text_switcher_1);

    mSwitcher = (TextSwitcher) findViewById(R.id.switcher);
    mSwitcher.setFactory(this);


    Animation in = AnimationUtils.loadAnimation(this,
            android.R.anim.fade_in);
    Animation out = AnimationUtils.loadAnimation(this,
            android.R.anim.fade_out);
    mSwitcher.setInAnimation(in);
    mSwitcher.setOutAnimation(out);

// Button nextButton = (Button) findViewById(R.id.next); // nextButton.setOnClickListener(this);

    updateview();

}

public void hi() {
    mCounter++;
    mSwitcher.setText(String.valueOf(mCounter));
    mSwitcher.addView(makeView());
}

private void updateview() {

    int delay = 5000;   // delay for 5 sec.
    int period = 5000;  // repeat every sec.
    Timer timer = new Timer();

    timer.scheduleAtFixedRate(new TimerTask() {
            //private View v;

            public void run() {
                // Task here ...
                hi();

                Log.d("Android", "hi value : " + mCounter);
            }
        }, delay, period);      

      }


public View makeView() {
    TextView t = new TextView(this);
    t.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);
    t.setTextSize(36);
    Log.d("Android", "makeView value : " + mCounter);
    return t;
}

}

Please i need help..for my project work. Thanks in advance.

Tariq