-2

Using Android Studio 1.1.0. Here is what I'm trying to accomplish...

I have a screen set up that will collect the # of players for a game. Based on the value from that spinner, I want to display X amount of text boxes on my next activity to capture the player names.

How can I set this up?

sshashank124
  • 31,495
  • 9
  • 67
  • 76
  • So is the part you are unsure of how to dynamically generate text boxes? – Mark Balhoff Apr 13 '15 at 20:04
  • Post the code you have already put together. What I am assuming your question to be. You have a spinner containing the number of players. When selecting the number you want to start another activity containing x amount of text fields for the number selected within spinner? – Eugene H Apr 13 '15 at 20:07
  • I'm voting to close this question as off-topic because it's not constructive. – EJoshuaS - Stand with Ukraine Aug 28 '18 at 00:22

2 Answers2

0

You can pass Data between Activitys by setting them as an Extra in the Intent. This Question explains how: How do I create an android Intent that carries data?

If you have a List in which you store Strings for the Player Names you could do that for example like the following:

    Intent intent = new Intent();
    intent.putStringArrayListExtra("playerNames", yourList);

In your next Activity you could then create a ListView (or GridView or whatever you think is suitable for this task) in which you show all the Playernames.

Community
  • 1
  • 1
rubengees
  • 1,841
  • 2
  • 16
  • 31
0

First pass this information from your first Activity:

 int numberOfPlayers;

Get the number of players from your spinner, however you currently do that set it to numberOfPlayers

Then pass this when you start your new Activity

 Intent getPlayerNamesIntent = new Intent(MainActivity.this, PLayerNamesActivity.class);
 getPlayerNamesIntent.putExtra("NUM_PLAYERS", numberOfPlayers); 
 startActivity(getPlayerNamesIntent);

Then in the next Activity onCreate get your Extras

 @Override
 public void onCreate(Bundle savedInstanceState){
      super.onCreate(savedInstanceState);
      // Layout should create have something like LinearLAyout with orientation vertical with name android:id="@+id/linearLayoutParent"
      setContentView(R.layout.base_layout);


      LinearLayout layout = (LinearLayout) findViewById(R.id.linearLayoutParent);

     // get the NUmber from extras
     int numberOfPlayers = getIntent().getExtras().getInt("NUM_PLAYERS");

    if(numberOfPlayers > 0){
        for(int i = 0; i < numberOfPlayers; i++){
           EditText editText = new EditText(this);
           LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
          editText.setLayoutParams(params);
          // Set Tag here, so you can use tag to get the right player later
          editText.setTag("PlayerNumber_" + Integer.toString(i)); 
          layout.addView(editText);

        }
    }
 }
kandroidj
  • 13,784
  • 5
  • 64
  • 76