I have a set of textviews like this..
TextView d1,d2,d3,d4,d5,d6,d7,d8.....etc
How can I set text in a loop like this by increment the number after d like this
for(int i=1;i<=8;++i)
{
d+i.setText("foo");
}
You can do like below.
Create ArrayList<TextView> and add all TextView in list.
Then after you have to do loop with List size.
and you can update text to TextView.
With this:
for(int i = 1; i <= 8; i++) {
int id = getResources().getIdentifier("d" + i, "id", getPackageName());
TextView tv = (TextView) findViewById(id);
tv.setText("foo");
}
the method getIdentifier() gets the integer id of a View by its id "name".
If this code is not inside an activity, you must provide a valid Context for the methods getResources() and getPackageName(), like:
context.getResources() and context.getPackageName().
Related
Context: I am trying to make an application that allows me to have multiple edit texts (in a to do list format). I am using a linear layout with a scroll view inside of it so that I can allow users to have as many notes as they need.
Question: How can I put each editText inside an array, furthermore, how can I put the string contents of each of the editText in an array.
Any help would be much appreciated!
You need to get text from your EditText like this
EditText et = FindViewById<EditText>(Resource.Id.et_nnnn);
string a = et.Text;
And then add to your array
You can get all child views of parent layout in an array. use something as below. pass parent layout view to follow function
SparseArray<Edittext> array = new SparseArray<Edittext>();
.
.
.
private void findAllEdittexts(ViewGroup viewGroup) {
int count = viewGroup.getChildCount();
for (int i = 0; i < count; i++) {
View view = viewGroup.getChildAt(i);
if (view instanceof ViewGroup)
findAllEdittexts((ViewGroup) view);
else if (view instanceof Edittext) {
Edittext edittext = (Edittext) view;
array.put(edittext.getId(), edittext);
}
}
}
To get all edittexts text you can loop over that array and store in different array or list using gettext on each child.
idI am developeing an app in android, In the layout i am using more than 20 EditText. To, reduce the size of my coding i was trying to implement EditText in an array .So, help me with how to implement the Edittext in an array and also how i should name the EditText in XML file to use it as an array.
I tried to implement this code but this is not working,
edittext reference in java file
EditText[] et1 = new EditText[20];
and putting this into a for loop,
for(i=0;i<20;i++)
{
et1[i]=(EditText)findViewById(R.id.edittext[i]);
}
but eclipse cannot properly resolve edittext[i].
tell me How do I properly create an array of EditText in my xml so that it is recognized
You can do something like this:
ArrayList<EditText> editTextList = new ArrayList<EditText>();
for(int i = 0; i < rootLayout.getChildCount(); i++) {
if(rootLayout.getChildAt(i) instanceof EditText) {
editTextList.add( (EditText) rootLayout.getChildAt(i));
}
}
Where rootLayout is the View containing all EditTexts (a LinearLayout, for example)
you have to create edittext programaticaly as shown below:-
private EditText[] mEditTextPlayers;
public void goButtonClicked(View view) {
maalContainer.removeAllViews();
int numPlayers = Integer.parseInt((String) numberOfPlayers.getSelectedItem());
LayoutInflater inflater = LayoutInflater.from(view.getContext());
mEditTextPlayers = new EditText[numPlayers];
for (int i = 0; i < numPlayers; i++) {
//Pass the parent as the second parameter to retain layout attributes
mEditTextPlayers[i] = inflater.inflate(R.layout.player_entry_item, maalContainer, false);
maalContainer.addView(dynamicEntryView);
}
}
Please let me know if it works for you or not :):-
I'll give a couple examples. First and foremost, this line is incorrect:
et1[i]=(EditText)findViewById(R.Layout.edittext[i]);
This is not how the findViewById() method takes a parameter. You can't append R.id to an array.
Now, you could do something like this: id[] = new id[] {R.Layout.editTextOne, R.Layout.editTextTwo}
When we sit back and think about this, we may realize it's going to take as much code (or more) as doing the following:
EditText[] editTextArray = new EditText[] {
(EditText) findViewById(R.id.oneEditText),
(EditText) findViewById(R.id.twoEditText),
(EditText) findViewById(R.id.threeEditText),
(EditText) findViewById(R.id.fourEditText),
(EditText) findViewById(R.id.fiveEditText),
(EditText) findViewById(R.id.sixEditText),
(EditText) findViewById(R.id.sevenEditText),
(EditText) findViewById(R.id.eightEditText),
(EditText) findViewById(R.id.nineEditText),
(EditText) findViewById(R.id.zeroEditText) };
for (int x = 0; x < editTextArray.length; x++) {
editTextArray[x].setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// DO stuff here........................
}
});
}
Lastly, DO NOT alter the xml file. Leave it be.
First of all encapsulate your all of your 20 EditText inside a layout and provide that layout with an id something like:
<LinearLayout
android:id="#+id/holder"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText android:id="#+id/edittext1"/>
<EditText android:id="#+id/edittext2"/>
<EditText android:id="#+id/edittext3"/>
...
<EditText android:id="#+id/edittext20"/>
</LinearLayout>
Now inside your activity do something like this:
//Global variable
int index = 0;
EditText[] editTextArray = new EditText[20];
//inside activity
LinearLayout holder = (LinearLayout) findViewById(R.id.holder)
...
for(int i=0; i<holder.getChildCount(); i++){
View view = holder.getChildAt(i);
if(view instanceof EditText){
editTextArray[index++] = view;
}
}
I have used LinearLayout for this example but you can use any layout.
Here is a solution that works. Suppose you have a layout (id = "R1") with 9 EditText widgets. The following code will set up an array allowing you to access those widgets in an array:
EditText[] etArray = new EditText[9];
int index = 0;
//store the EditText fields in an array
LinearLayout LL = findViewById(R.id.R1);
for(int i = 0; i < LL.getChildCount(); i++) {
View v = LL.getChildAt(i);
if (v instanceof EditText) {
//the following is the crucial line missing from the other answers
etArray[index++] = ((EditText) v)
}
}
Now, you can access the elements of the array as in the following example:
for(int i = 0; i < 9; i++) {
etArray[i].setText(Integer.toString(i));
}
I've got a layout which is called activity_main.xml which is my parent layout, and I am then inserting a child layout (activity_main_card.xml) within a for loop.
I also have an array, and what I am doing is using the length of the array to determine how many child elements it should create. All of my data to be used in the child element is stored in the array, so the idea is to loop through the array, create a child element for each loop and populate the data.
Instead, what is currently happening is that it is generating the 3 (length of array) child elements, but it is only populating the first one with the latest content in the array. This is because it keeps the variables the same.
What I need to do is somehow set dynamic variables that change as the loop iterates.
The code for my method is as follows:
for (int i = 0; i < cardArray.length; i++) {
LinearLayout item = (LinearLayout)findViewById(R.id.card_holder);
View child = getLayoutInflater().inflate(R.layout.activity_main_card, item, false);
item.addView(child);
//Set objects from array to variables
String cardTitle = cardArray[i][0];
String cardContent = cardArray[i][1];
String cardImage = cardArray[i][2];
//Set XML elements to variables
TextView title = (TextView) findViewById(R.id.card_title);
TextView content = (TextView) findViewById(R.id.card_content);
ImageView image = (ImageView) findViewById(R.id.card_image);
//Load variable content into card layout
title.setText(cardTitle);
content.setText(cardContent);
image.setImageDrawable(getResources().getDrawable(getResources().getIdentifier("drawable/" + cardImage, "drawable", getPackageName())));
}
What I thought I could do was to set the views like:
TextView title[i] = (TextView) findViewById(R.id.card_title);
however this doesn't work. Does anyone know how I can acheive this?
You need to get a reference to the current View you just created and then populate the TextView elements on that View. This is achievable by doing this:
TextView title = (TextView) child.findViewById(R.id.card_title);
TextView content = (TextView) child.findViewById(R.id.card_content);
ImageView image = (ImageView) child.findViewById(R.id.card_image);
You might be better off approaching this problem differently however, as Jonathan suggests a ListView and adapter might suit better for this situation.
You aren't using the View created, you are using the root layout of the activity in every loop because you apply the findViewById in the parent view:
Change:
TextView title = (TextView) findViewById(R.id.card_title);
By;
TextView title = (TextView) child.findViewById(R.id.card_title);
I have an array which contains some numbers:
int[] IDs = new int[] { 2, 5, 6, 11, 15};
There are many TextViews in Layout Resource:
TextView1;
TextView2;
TextView3;
TextView4;
TextView5;
TextView6;
and ... .
How Is it Possible to Access These in a for loop to (for example) change Their Text like this:
for(int i=0;i<IDs.length;i++){
TextView[i].setText = "something";
}
problem:Eclipse Doesn't Recognize "TextView[i]".
Note:I want to change the text of those TextViews which their name ends with one of the numbers in the IDs array.
You should be doing this, this is assuming the IDs correspond to the id's of the text views, if not you have to create an array of the id's
int[] IDs = new int[] { R.id.textView1, R.id.textView2, R.id.textView3, R.id.textView4, R.id.textView5};
TextView textView = (TextView) rootView.findViewById(IDs[i])
textView.setText = "something";
If it's an activity, then remove the rootView.
If you have a given Layout, you can iterate over the children in your layout.
Just put all TextViews in one Layout.
For example:
LinearLayout la = findViewById(R.id.myLayout);
for (int i = 0; i < IDs.length; i++){
if (la.getChildAt(i) instanceof TextView){
TextView tv = (TextView) la.getChildAt(i);
}
}
Edit:
It might be better to count the numbers of found TextViews, if you have other elements in your Layout. Otherwise it will not find all TextViews, if we only count to IDs.length.
I'd like use the setText() function for an EditText without knowing the id,
so if there are three EditText in an application, the command setText() should change the text on all three and I won't know the ids.
I was planning to use this command:
EditText ed = (EditText) findView (something);
I don't think it is that simple. Is there a way to accomplish this?
You could put the TextViews alone inside a ViewGroup (i.e. LinearLayout) and iterate along their children like this.-
for (int i = 0; i < containerView.getChildCount(); i ++) {
View view = containerView.getChildAt(i);
if (view instanceof TextView) {
TextView textView = (TextView) view;
textView.setText(yourText);
}
}