0

I am making a word game in which each a user has multiple guesses, each one made up of multiple TextViews. So far my code reads:

TextView[] guess1 = new TextView[numTextViews]; guess1[0] = (TextView) findViewById(R.id.Guess1_1); guess1[1] = (TextView) findViewById(R.id.Guess1_2); guess1[2] = (TextView) findViewById(R.id.Guess1_3); guess1[3] = (TextView) findViewById(R.id.Guess1_4); guess1[4] = (TextView) findViewById(R.id.Guess1_5); 

with the xml looking like:

<TextView android:id="@+id/Guess1_1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" android:gravity="center" android:text="@string/guessChar" />... 

which repeats with android:id= changing.

I am going to be repeating myself if I type out TextView[] guess2 and all its elements.

  • What is a better way to go about this?
  • Would it be better to create all the TextViews programmatically as they are so similar?
1
  • What you have done seems fine to me. Using XML is better imo. Commented May 3, 2013 at 6:55

3 Answers 3

3

This is how you can iterate through your views without the use of ids in repetitive code:

LinearLayout ll = (LinearLayout) findViewById(R.id.layout_containing_textviews); for (int i = 0; i < ll.getChildCount(); i++) { if (ll.getChildAt(i).getClass() == TextView.class) { guess1[i] = (TextView)ll.getChildAt(i); } } 

Make sure to tweak this in case you have non-TextView views since the i index will not be consecutive in that case. You can use another counter just for the TextViews.

Now if your layout has only TextViews, you don't even need an array. You can use that layout as a container/array the way it's used in the snipped above.

Sign up to request clarification or add additional context in comments.

Comments

0

Do you know what is the amount of guesses for each text view?

I would suggest you to use reflection

Class clazz = R.id.class; // get the R class Field f = clazz.getField("Guess1_" + "1"); int id = f.getInt(null); // pass in null, since field is a static field. TextView currcell = (TextView) findViewById(id); 

in this case it will bring the Guess1_1

for you case:

 for (int i =0; i < numTextViews; i++) { Class clazz = R.id.class; Field f = clazz.getField("Guess1_" + Integer.toString(i+1)); int id = f.getInt(null); guess[i] = (TextView)findViewById(id); } 

but this only bring you the first array of Guess1 you need to convert it to generic code.. so some problems can be occur.. so read it with the xml as you have right now would be the easiest way..

Edit:

If the all textView have the same attributes you can also create it programmatically

LinearLayout view = new LinearLayout(this); // create new linear layout view.setOrientation(LinearLayout.HORIZONTAL); // optional.. so the // view will be horizontaly view.setLayoutParams(new LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)); // set the layout // height and width for (int i = 0; i < numOf ; i ++) { LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); guess[i] = new TextView(); guess[i].setLayoutParams(lp); guess[i].setID(i+1); } 

6 Comments

rather than reflection, using Resources.getIdentifier is more readable
yeah I know, but its just a suggest.. :)
given the existence of the getIdentifier in resources, I fail to see why anyone would use reflection to find an id ?
@njzk2, I'm not going to argue with you.. I already told you that is just a suggestion.. the OP can choose any answer he want
i'm not trying to argue, I'd just like to know if there is a specific reason to use this method?
|
0

You could either create the textViews programmatically (and use inflate if you wish to use some xml too), or you could use the getIdentifier method , for example:

private static final String ID_FORMAT="Guess1_%d"; ... for(int i=0;i<10;++i) { String id=String.format(FORMAT,i); TextView tv = (TextView) findViewById(getResources().getIdentifier(id, "id", getPackageName())); //... } 

same goes if you wish to do a loop within a loop.

If the layout has a lot of views, I would suggest using an adapterView (listView,gridView,...) instead, and avoid creation of so many views (either programmatically or by xml).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.