Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Sayed Ahmad Behbehani
3,360 PointsCreating TextViews Dynamically
Hello there,
I'm trying to create textViews dynamically when the user submit a number in the previous screen. I got it working except I'm not sure how to get the data from the views to make the equations. I need to get ID for each view so I can write the equation and sum them all some how. Is there any suggestion?
Intent getIntOfMainScrn = getIntent();
int numOfLoads = Integer.parseInt(getIntOfMainScrn.getStringExtra("NofL"));
for(int i =numOfLoads;i>0;i--) {
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
linearLayout.setLayoutParams(layoutParams);
EditText a = new EditText(this);
EditText b = new EditText(this);
EditText p = new EditText(this);
a.setHint("a"); a.setTextSize(100); a.setGravity(Gravity.LEFT);
b.setHint("b"); b.setTextSize(20);; b.setGravity(Gravity.CENTER);
p.setHint("p"); p.setTextSize(20); p.setGravity(Gravity.RIGHT);
linearLayout.addView(a); linearLayout.addView(b); linearLayout.addView(p);
mainInputLL.addView(linearLayout);
}
1 Answer
Ben Deitch
Treehouse TeacherWe usually need the ID of the View so we can create a variable for that View, but you've already got variables for your EditTexts.
Sayed Ahmad Behbehani
3,360 PointsSayed Ahmad Behbehani
3,360 PointsI need to get variable for each loop to write the equation and sum the results. How would I do it while the variables are used again for each loop.
Thanks, Sayed
Ben Deitch
Treehouse TeacherBen Deitch
Treehouse TeacherStore the EditTexts in an array.
Something like: