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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:50:57+00:00 2026-06-13T03:50:57+00:00

I have a class called StringsA and inside it there is an array of

  • 0

I have a class called StringsA and inside it there is an array of strings:

public class StringsA {

static String Names[] = {"Larry", "Moe", "Curly", "John"};  

}

In my main class there is a Button and a TextView.
What I want is to set the text of the TextView to a different word from the strings array each time the Button is clicked.
For example; the current item is “Moe”, when the button is clicked, the TextView changes to “John” (the change is random, not by order).

My current Activity code:

    setContentView(R.layout.main);
    Button a = (Button) findViewById(R.id.button1);
    TextView b = (TextView) findViewById(R.id.tv);        

    Resources i = getResources();
    i.getResourceName(StringsA.Names);

    a.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {               

        }
    });
}

I’m getting error in i.getResourceTypeName. How should I change the TextView text to one of the strings when the Button is clicked?

  • 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-06-13T03:50:58+00:00Added an answer on June 13, 2026 at 3:50 am

    Take full advantage of Android resources, load your values from a string array defined in strings.xml. E.g.

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <string-array name="planets_array">
            <item>Mercury</item>
            <item>Venus</item>
            <item>Earth</item>
            <item>Mars</item>
        </string-array>
    </resources>
    

    Then:

      private static final Random RAND = new Random();
    
      public void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.main);
        Button myButton = (Button) findViewById(R.id.button1);
        TextView myTextField = (TextView) findViewById(R.id.tv);     
    
        final String[] values = getResources().getStringArray(R.array.planets_array);
        myButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {     
              String nextValue = values[RAND.nextInt(values.length)]          
              myTextField.setText(nextValue);
            }
        });
      }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a List of strings called Names in a class called data .
Suppose I have a class called Person: public class Person { private String name,
I have a class called: ComplexNumber and I have a string that I need
I have a class called Account public class Account { public double balance =
I have a class called Path for which there are defined about 10 methods,
I have a hashset in a class called myClass that contains 6 strings. I
I have a class called Entry. This class as a collection of strings called
In Java there is a nice class called Scanner for reading strings and streams
In C#, say I have a class called Note with three string member variables.
I have a class called Trial which has_many results. Now What I want to

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.