0

I have a sample activity to display three diagrams in an array on button click.

What I'm trying to do now is update the original TextView string with every button click according to the button click.I have an idea of how to do it with numbers like this:

 tView = (TextView) findViewById(R.id.textView1);
    clickhere = (Button) findViewById(R.id.button1);

    clickhere.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            num++;
            String display = String.format(getString(R.string.prompt), num);
            tView.setText(display);
            setContentView(tView);

        }
    });

But how would I do this in the case of a string? I'm wondering is there a way to code it as on button click "one" then show relevant text,then on button click "two" show relevant text.The original text shows to tell the user what to do,then I want the text string to change below the diagram every time the button is clicked.So there will be a matching string to each of the three diagrams.

I'm not sure if you can implement it like that by counting button clicks,or is there a better way?

Diagram activity

Brian Var
  • 6,029
  • 25
  • 114
  • 212
  • 1
    What problem are your facing?You want to display some specific text on particular button click.Is this what you want? – Manishika Nov 07 '13 at 18:31
  • Yes the original text shows to tell the user what to do,then I want the text string to change below the diagram every time the button is clicked.So there will be a matching string to each of the three diagrams. – Brian Var Nov 07 '13 at 18:35
  • 1
    Possible duplicate of [How to change a TextView's text by pressing a Button](http://stackoverflow.com/questions/6716748/how-to-change-a-textviews-text-by-pressing-a-button) – Ciro Santilli OurBigBook.com Feb 03 '16 at 14:39

3 Answers3

0

add member variable to your activity called mButtonClicks

private int mButtonClicks;

then in the onClick of the button, update the number.

clickhere.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
        mButtonClicks++;
        String display = String.format(getString(R.string.prompt), mButtonClicks);
        tView.setText(display);

    }
});

I'm guessing this code is in your onCreate() method. Make sure that mButtonClicks is defined outside of that method.

Jon F Hancock
  • 3,349
  • 3
  • 23
  • 26
0

Although your question is talking about changing a text view, it looks like you already figured it out. Just remove setContentView(textView), (because that will replace your entire view), and your method will work fine.

In case you're also asking how to convert integers into spelled-out Strings:

In your strings.xml, create all your spelled out strings. Then create a helper method to get a String from an integer:

protected String integerToSpelledOutString(int anInt){
    int resource = 0;

    switch (anInt){
    case 1:
        resource = R.string.one;
        break;
    case 2:
        resource = R.string.two;
        break;
    /*etc. for all integers you want to support*/
    }

    return getString(resource);
}
Tenfour04
  • 83,111
  • 11
  • 94
  • 154
0

In my solution the textview is handler only by NextButtone.

private int clicks = 1;

tView = (TextView) findViewById(R.id.textView1);
clickhere = (Button) findViewById(R.id.button1);

clickhere.setOnClickListener(new View.OnClickListener() {
     public void onClick(View v) {             
         tView.setText(getMyString(clicks++));             
     }
 });

 public String getMyString(int variable){
     switch(variable){
         case 1:
             return YOUR_STRING_01;
             break;
         case 2:
             return YOUR_STRING_02;
             break;
         case 3:
             return YOUR_STRING_03;
             break;
     }
 }   
Pierry
  • 979
  • 7
  • 17
  • In this case the image container is an `imageswitcher`.Would the implementation still work here? Also `YOUR_STRING_01` how would I reference the diagram strings in this class? – Brian Var Nov 07 '13 at 20:35