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

3
  • 1
    What problem are your facing?You want to display some specific text on particular button click.Is this what you want? Commented Nov 7, 2013 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. Commented Nov 7, 2013 at 18:35
  • 1
    Possible duplicate of How to change a TextView's text by pressing a Button Commented Feb 3, 2016 at 14:39

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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); } 

Comments

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; } } 

1 Comment

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?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.