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

Related Questions

I’m trying to display a list of shops each with 3 random items from
I've trying to display random data from the database example in my database i
Trying to display current time with PHP (using this ): $date = date('m/d/Y h:i:s
trying to display data only after variables have been set. $(document).ready(function () { function
I am trying to display data that I am getting from server through json.
I'm trying to display all the 'legs' for each of my 'trips' in my
I am trying to display the employee number of each employee who manages other
I'm trying to display an array in a random order in a foreach loop
I'm trying to generate an arraylist of random numbers and display it. I'm not
I have an input of a random string of twelve characters like this: ABABABABABAB

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.