Retrieve values of a dynamic form in Android - android

I created a dynamic form without much problems, but I need to recover the values from the fields (controls) of the form, but I'm not sure of how to do this.
For example, I have this piece of code:
if(tipoP.equals("TEXTAREA")){
EditText ta = new EditText(this);
ta.setId(i);
LayoutParams params3 = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, (float) 0.3);
params3.setMargins(20, 0, 20, 0);
ta.setLayoutParams(params3);
ta.setLines(3);
ta.setRawInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE);
ll.addView(ta);
}
How do I add a listener that captures the text of the EditText and put it inside a Vector variable?
I tried this:
ta.setOnClickListener(new OnClickListener(){
public void onClick(View view){
EditText t = (EditText) findViewById(i);
res.add(t.getText().toString);
}
});
But I'm not getting the id (variable i) because its in another execution environment. How do I solve this? Any help would be appreciated!!

You should not use setId for dynamically created views but setTag and findViewByTag.

You could create a button dynamically and set an onClickListener on it. Inside the listener, you can just reference the EditText directly (no need for tags or ids) as long as you have made it final.

Related

findViewById for dynamically added View

Is it possible to find the added TextView to update it with other options.
I am adding by this code new TextView with a Button. Now, I want also set a onClickListerner for the dynamically added TextViews. For this I have to find them with findViewById method. But they aren't createt yet.
Can I make this in a other way and if yes, How?
Button hinzufügenButton = (Button) findViewById(R.id.hinzufügen_Button);
hinzufügenButton.setOnClickListener(new Button.OnClickListener() {
public void onClick (View view){
EditText tischName = (EditText) findViewById(R.id.tisch_name_EditText);
TextView tisch = new TextView(getApplicationContext());
tisch.setText(tischName.getText());
tisch.setAllCaps(true);
tisch.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
tisch.setBackgroundColor(Color.parseColor("#9FA8DA"));
tisch.setTextSize(25);
tisch.setId(id);
id++;
LinearLayout tischeAnzeigen = (LinearLayout) findViewById(R.id.tische_LinearLayout);
tischeAnzeigen.addView(tisch);
}
});
Just add the onClickListener as you create your dynamic views
Example:
...
tisch.setId(id);
tisch.setOnClickListener(yourListener);
...
You can use setId to add an id. But you probably don't need to. If you're creating a view dynamically, you have a reference to it when you create it. Just set it on that reference. No need to find it.

What should I use to find my dynamically created TextViews

I has to ask you how to find my Views, I created them dynamically because all information about Texts were in my string-array, and now I would like to find it to store this in Class by getters, to use it in another classes, in feature I would like to make backend to store information thats why I chose class for temporary solution.
Thats my code
sportlist = getResources().getStringArray(R.array.btnsport);
for(j=0;j<sportlist.length;j++){
sporthash.put(j,sportlist[j]);
System.out.println(sporthash);
}
//Here I create my TableRow with TextView
TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.MATCH_PARENT);
lp.height = 120;
lp.setMargins(8,8,8,8);
for(j=0;j<sportlist.length;j++) {
TableRow row = new TableRow(getActivity());
row.setLayoutParams(lp);
final TextView t = new TextView(getActivity());
t.setPadding(10, 10, 10, 10);
t.setText(sporthash.get(j));
t.setGravity(Gravity.CENTER);
t.setClickable(true);
t.setTextSize(18);
t.setId(j);
t.setBackgroundResource(R.drawable.sport_false);
t.setTextColor(Color.BLACK);
Then after this I have saving button, which send information to my Storage.class. I try to set t.setId(j) and find it by new TextView h, h.findViewById(j) but its return strage code. I would to find Views with t.BackgroundResource(R.drawable.sport_true) and send them to my Storage.class.
Thats my button, in for I tryed to find my views, so what solution would be best ?
save.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Storage sto = new Storage();
sto.setAge(BirthDate.getText().toString());
sto.setImage(civ);
sto.setLocation(Map.getText().toString());
System.out.println(Map.getText().toString());
for(j=0;j<sportlist.length;j++) {
}
Intent newIntent = new Intent(getActivity(),ProfilActivity.class);
startActivity(newIntent);
}
});
You can't use setId here, because setId should be used with generateViewId:
From documentation of generateViewId():
generateViewId
Generate a value suitable for use in setId(int). This value will not collide with ID values generated at build time by aapt for R.id.
When you're trying to set the id with setId() there's probability your id is colliding with auto-generated id by the Android Studio. So, you need to ensure the id is unique.
You can use setTag() and getTag() to avoid the problem with setId()

Android: Dynamically create image button with click event

I'm new to android and I require to have a list of image buttons in an activity which are created based on the data in a database. I haven't created anything like this in android before and so far I've been using HTML tables to show my data in a grid view. I'm not asking for any special code, I'm just clueless on how to implement this. I wanna know what the best approach is.
One problem I've faced is with the click events(in the way I've been doing them so far) which do not take in any EventArg, so I can't get the name of the button out of them.
If you're sure that the OnClickListener instance is applied to a Button, then you could just cast the received view to a Button and get the text:
public void onClick(View v) {
// 1) Possibly check for instance of first
Button b = (Button)v;
String buttonText = b.getText().toString();
}
// create the layout params that will be used to define how your button will be displayed
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
// Create Button
final Button btn = new Button(this);
// Give button an ID
btn.setId(someinteger);
btn.setText("Add Text");
// set the layoutParams on the button
btn.setLayoutParams(params);

Define OnClickListener methods for TextViews that were created dynamically

I created new TextViews in my GUI dynamically and I collected them in an array of TextViews, but I can't set up OnClickListeners because of that (they're inside of an array)...
So what I want to do now is evaluate if the TextView is clicked and handle that event, but I'm not sure how to do so...
If I'm not being to clear, please tell so I can write down the problem with all the details...
Thanks!
Even when you are creating rows dynamically you still have a reference to the TextView. Simply add the OnClickListener to this reference, like this:
TextView tv1 = new TextView(this);
tv1.setText(string);
tv1.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
tv1.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
// Do something
}
});
row.addView(tv1);
So your TextViews are in an array like TextView[] textViews?
Just call textViews[0].setOnClickListener().
If you used an ArrayList, then it would be textViews.get(0).setOnClickListener()

dynamic create editbox in android?

I want to create a Editbox dynamically when i click a add button and i also want to get the values typed in that Editbox when i click in save button.
Please Help me. Regards. Augustine
Try out this:
LinearLayout mLinearLayout = new LinearLayout(this);
mLinearLayout = (LinearLayout)findViewById(R.id.mylinearlayout);
Button lButton = (Button)findViewById(R.id.mybtnid);
lButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
EditText lEditText = new EditText(this);
lEditText.SetText("Text Here");
mLinearLayout.addView(lEditText);
}
}
To get the values typed into the EditText, you need to additionally set an identifier for the view.
lEditText.setId(2); //you can use any integer ID
Then, you can retrieve the text inside the OnClickListener of the save button as:
EditText lEditText = (EditText)findViewById(2);
String txt = lEditText.getText().toString();
To create an edit text dynamically or programmatically:
EditText ed = new EditText(context);
Set whatever parameters you want to set for this edit text and then add this in your view:
view.addView(ed);
OR
view.addView(ed, layoutParams);
you can create EditText with the following code inside your Activity
EditText _edit = new EditText(this);
Then to add this to you activity layout you have to get the particular layout by it id
For Ex.
LinearLayout linear = (LinearLayout)findViewById(R.id.linear);
then simple add this EditText object to the LinearLauout by using the following code..
linear.addView(_edit);

Categories

Resources