Reputation: 1148
I have created some dynamic EditText fileds and a button using foreach loop, code is below:
void callDynamicView(){
String configText="Building Name, S, 10; No: of Floors, I, 3; Area of Plot, D, 10; Location(X), L, 15.3; Location(Y), L, 15.3; Photo, P, 10";
String[] splits = configText.split(";");
ScrollView sv = new ScrollView(this);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
sv.addView(ll);
TextView tv = new TextView(this);
tv.setText("Survey");
tv.setTextColor(Color.rgb(200,0,0));
ll.addView(tv);
for(String splitSemi: splits){
String[] splitsCommas = splitSemi.split(",");
int len = splitsCommas[1].length();
//Toast.makeText(getBaseContext(), len, Toast.LENGTH_SHORT).show();
if(splitsCommas[1].equals("P")){
Toast.makeText(getBaseContext(), "Keri", Toast.LENGTH_SHORT).show();
Button btn = new Button(this);
btn.setText("Photo");
ll.addView(btn);
//this.setContentView(sv);
}
else{
//Toast.makeText(getBaseContext(), "else", Toast.LENGTH_SHORT).show();
TextView txtv = new TextView(this);
txtv.setText(splitsCommas[0]);
ll.addView(txtv);
EditText et = new EditText(this);
ll.addView(et);
}
}
Button btn = new Button(this);
btn.setText("Submit");
ll.addView(btn);
this.setContentView(sv);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Toast.makeText(getBaseContext(), et.getText().toString(), Toast.LENGTH_SHORT).show();
}
});
}
I want to access all entered values in button click. But I am unable to get the values. I have checked varios possibilities. I am new to creating UI dynamically . Can help me guys?
Upvotes: 2
Views: 682
Reputation: 7259
You could do this:
- On each creation of the
TextView
orEditText
, assign an id to the view.- Add the
TextView
orEditText
to a list of the same type, ie.List<EditText>
- On
Button
click, get the values of theTextView
orEditText
added into the list.
as like:
for(int i=0;i<arrayListOfViews.size();i++) {
EditText editObj = arrayList.get(i);
String val = editObj.getText().toString();
}
Hope that helps!
Upvotes: 1
Reputation: 9700
Set EditText
's id as follows...and store those ids in a List
to retrieve them later.
List<Integer> editTextIdList = new ArrayList<Integer>();
int id = 0;
for(String splitSemi: splits){
String[] splitsCommas = splitSemi.split(",");
int len = splitsCommas[1].length();
//Toast.makeText(getBaseContext(), len, Toast.LENGTH_SHORT).show();
if(splitsCommas[1].equals("P")){
.....
}
else{
//Toast.makeText(getBaseContext(), "else", Toast.LENGTH_SHORT).show();
TextView txtv = new TextView(this);
txtv.setText(splitsCommas[0]);
ll.addView(txtv);
EditText et = new EditText(this);
et.seId(id);
editTextIdList.add(id);
ll.addView(et);
id++;
}
}
In OnClickListener
...create EditText
instance using those ids and get their text as follow...
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
for(int i = 0; i < editTextIdList.size(); i++) {
EditText editText = (EditText)findViewById(editTextIdList.get(i));
String text = editText.getText().toString();
}
// Toast.makeText(getBaseContext(), et.getText().toString(), Toast.LENGTH_SHORT).show();
}
});
Upvotes: 3
Reputation: 420
setId to your editText and onClick of button do:
EditText editText =(EditText) context.findViewById(id);
String result = editText.getText().toString();
or use
ArrayList<EditText> editTextList = new ArrayList<EditText>();
in for loop add editText
editTextList.add(i,new EditText(context));
for getting values use
editTextList.get(i).getText().toString();
Upvotes: 1
Reputation: 5134
Instead of declaring your Button, TextView and EditText in your loop , declare it above your onCreate() method. And you can initialize it anywhere you want.
public class YourClass(){
TextView tv;
Button btn;
EditText edt;
/* your onCreate() and all other codes here*/
.....................
.....................
public void callDynamicView(){
/* Initialize and create your view here */
}
}
and you can directly access the TextView,Button and EditText
anywhere in your class
Upvotes: 1
Reputation: 4425
String strtextview=txt.gettext. toString() ;
String strbtnText=btn.gettext. toString();
Upvotes: 1
Reputation: 989
Just declare following variables at class level,
TextView tv;
TextView txtv;
EditText et;
Upvotes: 1