Edit text input into an array - java

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.

Related

Dynamic variables in a for loop?

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);

How Can I setText in the EditText without knowing id?

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);
}
}

Is there a way retrieve piece of information of a control (linearlayout) created in dynamic way?

I'm working on an android project which allow the user to fill a form.
The picture below descripts my differents layouts for the form.
The black layout (LinearMain) is my principal linear layout.
The green one (LinearForm) descripts the rows of the form.
LinearMain.addView(LinearForm);
The blue one (LinearDescription) contains the description the row the form to fill in.
LinearForm.addView(LinearDescription);
And the gray linear layout descripts my controls. The control can be an EditText, a button or a CheckBox.
For example LinearForm.addView(LinearButton)
After filling in the form I want to retrieve the text in the EditText, Know if the CheckBox is Checked or not.
I already try to use an ArrayList (example ArrayList) but it's not very helpful.
Is there another way to figure this out?
Because you don't know exactly with view you are to deal with it is important here to use instance of to avoid exceptions
ViewGroup is an abstract class that extends all ViewGroups, LinearLayout for instance is a ViewGroup.
if(ViewGroup.getChildAt(int) instanceof Checkbox){
//do sommethng here
}else if(ViewGroup.getChildAt(int) instanceof Button){
//do sommethng here
}else if(ViewGroup.getChildAt(int) instanceof TextView){
//do sommethng here
}else if(ViewGroup.getChildAt(int) instanceof EditText){
//do sommethng here
}
ViewGroup.getChildAt(int) is documented here
you can create a for loop to go through LinearLayout's children, check if corresponding children is instanceof CheckBox and get the value ((CheckBox) getChildAt(i)).isChecked()
Sample code:
LinearLayout ll;
for ( int i = 0; i < ll.getChildCount(); i++ ) {
View child = ll.getChildAt(i);
if ( child instanceof CheckBox ) {
boolean checked = ((CheckBox) child).isChecked();
}
}
You could set id to your View, never mind what view it is by: LinearButton.setId();
and then get the text by using findViewById(R.id.chosenId).getText().toString();
for EditText or findViewById(R.id.chosenId).isChecked(); for CheckBox.
another way is to save your view in a arrayList of views and to access them using this array.
Have u created the layout in xml and set the id of each of the view ?
If yes then all u need to just create the object of the EditText using findViewById(R.id.edittext) and then use getText() method of the EditText to get the text from the EditText. And for CheckBox you can use findViewById(R.id.chosenId).isChecked().

Android instanceof detecting all widgets?

I'm trying to set a font across all TextView's by iterating through the LinearLayout's views and using instanceof.
The form currently consists of 4 TextView's and one Button.
The below code is detecting all Views, even the Button as a TextView?
/* Set fonts */
LinearLayout ll = (LinearLayout) findViewById(R.id.ll_screenincourse_wrapper);
for (int i = 0; i < ll.getChildCount(); i++) {
View v = ll.getChildAt(i);
if (v instanceof TextView) {
((TextView) v).setTypeface(Fonts.get3dDumbFont(this));
}
}
If I log each view's class name it returns the TextView and the Button so I know the correct controls are being picked up.
The problem is the Button and TextView's fonts are being set, and I only want the TextView's.
I have found a work around and that is to do the following but am intrigued as to why the above code does not work as expected.
/* Set fonts */
LinearLayout ll = (LinearLayout) findViewById(R.id.ll_screenincourse_wrapper);
for (int i = 0; i < ll.getChildCount(); i++) {
View v = ll.getChildAt(i);
if (v.getClass().getName().contains("TextView")) {
((TextView) v).setTypeface(Fonts.get3dDumbFont(this));
}
}
Is it because both Button and TextView are of type View? If so, what would be the correct way about doing this?
Any help appreciated, thanks. Ricky.
Actually, Button is a subclass of TextView! That's why you see it as a TextView (it is also a TextView).
http://developer.android.com/reference/android/widget/Button.html
public class Button extends TextView
You could make a second if instanceof to exclude Buttons or use
if (v.getClass() == TextView.class)
But this won't match any other subclasses of TextView (if you use them).
It is simple
Button Class extends TextView Class
Check the documentaion

Clone textview to append it to a ViewGroup

I have a ViewGroup defined in XML with a view inside, at onCreate time I'd like to have a variable of those.
I don't want to go through the hassle of using a listview+adapter cause its clearly overkill as I know the list won't change since onCreate()
This is more or less the code I'd like to have.
TextView mytextview = myViewGroup.findViewById(R.id.mytext);
for(String test : strings){
mytextview = mytextview.clone();
mytextview.setText(test);
myViewGroup.addView(mytextview);
}
But it is not working.
Maybe use an inflater, and put the textview in an external layout file:
View v = LayoutInflater.from(this).inflate(R.layout.textview_include, null);
viewGroup.addView(v);
Using code of Mathias Lin and using the hint from javahead76:
LinearLayout x = (LinearLayout) findViewById(R.id.container);
for (int i = 0; i < 5; i++) {
View c = LayoutInflater.from(this).inflate(R.layout.singlerow, x);
TextView t = ((TextView)findViewById(R.id.textView1));
t.setId(i+10000);
t.setText("text"+i);
}
TextView b = (TextView) findViewById(10003);
b.setText("10003");
If you do this, you will most likely get the exact same id for every view created this way. This means doing things like ((TextView)v).setText("some text"); will be called on every TextView previously inflated from the same layout. You can still do it this way, but you should call setId() and have some reasonable method for ensuring you do not get the same id twice in a row - incrementation or universal time, etc.
Also, I think Android reserves a certain range of id's for dynamically created id's. You might avoid ID's in this range; but honestly, I don't know the id system works so I could be wrong on this point.

Categories

Resources