0

So currently I've got an activity that's to do with something called Ad-Libs, in which there is a blank space in a sentence and the user taps on it to open a popup menu to add a word into the space. Currently I have it set so that only 3 words are available and they're the same 3 words every time its pressed, however I was wondering if its possible to have those 3 words be randomly selected from a list per-say, or maybe an array-list. I'll include my code that I use for when the user taps on the empty space as well as the xml code for the popup menu, however be noted that it's basically just a simple pop up menu.

        beginningtext.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v){
            PopupMenu heroPop = new PopupMenu(AdventureActivity.this, beginningtext);
            heroPop.getMenuInflater().inflate(R.menu.heropop_menu, heroPop.getMenu());
            heroPop.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                public boolean onMenuItemClick(MenuItem item) {
                    Toast.makeText(AdventureActivity.this,"You Selected : " + item.getTitle(),Toast.LENGTH_SHORT).show();
                    beginningtext.setText(R.string.partyMembers);
                    beginningtext.append(item.getTitle());
                    return true;
                }
            });
            heroPop.show();//showing popup menu
        }
    });//closing the setOnClickListener method

And for the pop up menu xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

<item
    android:id="@+id/one"
    android:title="@string/party1"/>

<item
    android:id="@+id/two"
    android:title="@string/party2"/>

<item
    android:id="@+id/three"
    android:title="@string/party3"/>

<item
    android:id="@+id/four"
    android:title="@string/party4"/>

</menu>

Any help with this is very much appreciated, thank you very much.

Phantômaxx
  • 36,442
  • 21
  • 78
  • 108
Alex Wong
  • 13
  • 5

1 Answers1

0

You could build an array with the things you want,

String[] array = {"thing1","thing2"};

Then randomly generate a number within the range of the array length

0 - array.length - 1;

Check out this question for how to generate a random number.

If you wanted you could at runtime fill an arraylist with string assets and do the above on that arraylist.

M. Hig
  • 71
  • 10
  • Just make sure your random number doesn't go outside the bounds of your array! – M. Hig Jul 11 '18 at 16:13
  • I'll give it a shot! Thanks! – Alex Wong Jul 11 '18 at 16:48
  • Although I do have a question, on how that would work with the popup menu xml? Would i just assign something from the Arraylist into the item? I'm honestly not the greatest at android so I'm not so sure how that would be done. – Alex Wong Jul 11 '18 at 16:50
  • I've never personally used the menu but you should be able to do something to the effect of `item.setText(*what ever*)`. If you had a function that randomly gave you a string back it could be `item.setText(randomString());` Just make sure each xml item is bound to an object in your view. – M. Hig Jul 11 '18 at 16:55
  • Hm, well alright. I'll give it a try and hopefully it'll work out, thanks for the advice. – Alex Wong Jul 11 '18 at 16:56