I am creating a quote program where the user enters values into edittext views , I need to validate all the textviews have a value. I can find each textview with
edittext1 = (EditText) findViewById(R.Layout.etEditText1)
I would like to loop thru all the edittext views with out using a bunch of if statements.is there a foreach loop or for loop that I can use here?
I have a c# sharp background and am new to Java...any help would be appreciated
You can add all editTexts to an array or a list:
EditText[] ets = new EditText[nbr];
ets[0] = edittext1;
ets[1] = edittext1;
....
and then loop over them with:
for (EditText et : ets) {
if (!et.getText().toString().isEmpty()) {
//do something
}
}
TableLayout tView = (TableLayout) findViewById (R.id.table_layout);
for(int i=0; i<((ViewGroup)tView).getChildCount(); ++i) {
View someView = ((ViewGroup)tView).getChildAt(i);
if(someView instanceof EditText){
//Your logic
}
}
Related
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().
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.
I have this function which is supposed to create an array of TextViews with unique ids.
Each TextView is supposed to perform a unique action, however when any one of them is clicked, they perform the function of the last TextView .
(ie, anyone of them appends a 9 to the last TextView the way this i set up) Do you know why it does this, and how I can fix it?
Any help would be greatly appreciated.
//Code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_what_can_imake);
int textViewCount = 10;
TextView[] textViewArray = new TextView[textViewCount];
RelativeLayout myLayout = (RelativeLayout) findViewById(R.id.myLayout);
for(int i = 0; i < textViewCount; i++) {
textViewArray[i] = new TextView(this);
textViewArray[i].setText("Title"+Integer.toString(i));
textViewArray[i].setPadding(8,8+50*i,8,0);
textViewArray[i].setId(i);
LayoutParams myTitleDimensions = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
textViewArray[i].setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
int myId = v.getId();
((TextView) v).append(Integer.toString(myId));
}
});
myLayout.addView(textViewArray[i],myTitleDimensions);
}
}
You are using different paddingTop to layout your TextViews vertically:
textViewArray[i].setPadding(8,8+50*i,8,0);
this makes the TextViews visually separate to each other, but in fact they are all overlapped, the 2nd one overlapped the 1st, the 3rd one overlapped the 2nd, etc. At last, the 9th one overlapped all, so no matter which text you clicked, you actually clicked the 9th one.
To fix this, you should change the way you layout the TextViews.
For example, use RelativeLayout.addRule(int verb, int anchor):
RelativeLayout.LayoutParams myTitleDimensions = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
if (i > 0) {
myTitleDimensions.addRule(RelativeLayout.BELOW, i - 1);
}
By the way, 0 is not a valid id, so your 1st TextView will be still overlapped by the 2nd one, just change the way to generate ids a little.
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'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);
}
}