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

  • Home
  • SEARCH
  • 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 750047
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T14:30:20+00:00 2026-05-14T14:30:20+00:00

i am trying to display a random string each time a button is pressed

  • 0

i am trying to display a random string each time a button is pressed from a set of strings defined in strings.xml . this is an example of the strings ID’s

<string name="q0">
    <string name="q1">
    <string name="q2">
    <string name="q3">
    <string name="q4">

java code for getting a random string.

private static final Random rgenerator = null;

    int RandomQ = R.string.q0  (rgenerator.nextInt(5) + 1);
    String q = getString(RandomQ);

if i try to use this java code i get an error at “q0” in R.string.q0 which is The method q0(int) is undefined for the type R.string if i try to use the quick fix and create a method, it works. but it wont let me save or run the app because it replaces my create method and shows this message

R.java was modified manually! Reverting to generated version!

thanks for reading.

  • 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-05-14T14:30:20+00:00Added an answer on May 14, 2026 at 2:30 pm

    You can define your strings in an array which will help simplify this task (res/values/array.xml):

    <string-array name="myArray"> 
        <item>string 1</item> 
        <item>string 2</item> 
        <item>string 3</item> 
        <item>string 4</item> 
        <item>string 5</item>
    </string-array> 
    

    You can then create an array to hold the strings and select a random string from the array to use:

    private String[] myString; 
    
    myString = res.getStringArray(R.array.myArray); 
    
    String q = myString[rgenerator.nextInt(myString.length)];
    

    Example code:

    package com.test.test200;
    
    import java.util.Random;
    
    import android.app.Activity;
    import android.content.res.Resources;
    import android.os.Bundle;
    import android.widget.TextView;
    
    public class Test extends Activity {
    /** Called when the activity is first created. */
    
        private String[] myString;
        private static final Random rgenerator = new Random();
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
    
        Resources res = getResources();
    
        myString = res.getStringArray(R.array.myArray); 
    
        String q = myString[rgenerator.nextInt(myString.length)];
    
        TextView tv = (TextView) findViewById(R.id.text1);
        tv.setText(q);
    }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 509k
  • Answers 509k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer After testing, I believe the answer to this is yes.… May 16, 2026 at 4:32 pm
  • Editorial Team
    Editorial Team added an answer It is quite explicit in the MSDN Library docs for… May 16, 2026 at 4:32 pm
  • Editorial Team
    Editorial Team added an answer There isn't an implicit cast of Int to String in… May 16, 2026 at 4:32 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I am trying to display random images heres my code private void Page_Load(object sender,
im trying to display image when left mouse button is down. i can display
I am trying to change a certain text box message. It will display my
I am trying to change a certain text box message. It will display my
I am trying to build a program to Tolkinize a string (i'm trying to
I'm trying to design a code where one guess a number. I defined the
I'm trying to create an android application which will generate random series of values
gcc 4.4.4 c89 However, I am having a problem trying to display all the
I am trying to display a UITableView in front of an image, and here's
I am trying to display all the fonts available in the system with its

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.