How to get all text from dynamically added edittext in android? - android

I am new to android. Now i am need to know how to get text values of all edittext which has added dynamically when user press add button.
add.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
table = (TableLayout) findViewById(R.id.table);
row = new TableRow(Material.this);
table.setGravity(Gravity.CENTER);
edit = new EditText(Material.this);
edit.setWidth(135);
edit.setHeight(35);
edit.setBackgroundColor(Color.WHITE);
row.addView(edit);
table.addView(row);
Here how to get text values of edittext? User may add more than one edittext. At that case, how to get all text?

If EditTexts are dynamically added maintain the references of EditText in an array and when ever you want text from them iterate the array.

EditTexts are created dynamically with a reference use that reference to get the value of it.
For Example
I have created an editText.
EditText myTextBox = new EditText(getBaseContext());
containerLayout.addView(myTextBox);
Here, myTextBox would be the variable which refers to that editText, you could use getText() to get the value of that box or you could use setText() to set the value to that box.
String value = myTextBox.getText().toString();
myTextBox.setText("this is the setted text");`
EDIT
If there are more than one editText then add all the references in an array and iterate over them.
List<EditText> myArray = new ArrayList();
EditText editText1 = new EditText(getBaseContext());
containerLayout.addView(editText1);
myArray.add(editText1);
EditText editText2 = new EditText(getBaseContext());
containerLayout.addView(editText2);
myArray.add(editText2);
EditText editText3 = new EditText(getBaseContext());
containerLayout.addView(editText3);
myArray.add(editText3);
int i = 0;
while (i < myArray.size()) {
Log.i(TAG,myArray.get(i).getText());
i++;
}

If you have created edit text and text is set in edit text then:
String text = edittext.getText().toString();
would be okay.

You will need to keep reference of that dynamically added EditText.
And call getText().toString(); on that reference.
One other approach is to set some ids using setId(); keep record of those ids and whenever required.
((EditText)findViewById(edittextidyouset)).getText().toString();
Take look at this thread setting ids dynamically : How can I assign an ID to a view programmatically?

Hope this can help you-
ArrayList<EditText> editTexts = new ArrayList<>();
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.container);
for (int i = 0; i < 20; i++) {
EditText editText = new EditText(this);
editTexts.add(editText);
editText.setText("I am at " + i);
linearLayout.addView(editText);
}
for (EditText editText : editTexts) {
Log.d("Texts", editText.getText().toString());
}
Your xml file can be like-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
android:id="#+id/container"
android:orientation="vertical"
tools:context="com.hestabit.activity.Home">
</LinearLayout>

Related

AndroidStudio: Display input field on click

I am building an android app where a user can create, view, delete, and update his cooking recipes.
If a user is creating a recipe I want him to be able to add steps like: step1:, step2:, etc.
So if a user needs another step he simply tabs on a button "add step" and an input field should appear below the previous step.
I tried searching for this but didn't find anything. Could someone help me into the right direction?
With the following piece of code I am able to create new EditText fields on button click.
private int hint = 0;
public void addStep(View view) {
//Get the linearLayout where the EditText fields will appear.
LinearLayout container = findViewById(R.id.linearLayout);
//Increment id by 1.
hint++;
//Create a new EditText field.
EditText step = new EditText(this); //Creating new TextView
//Add attributes to EditText
step.setId(hint);
step.setText("step");
step.setMaxLines(1);
step.setWidth(160);
//Finally Display the EditText field in the LinearLayout
container.addView(step);
}
Suppose you are using constraintlayout
EditText step1 = findViewById(R.id.step1);
EditText step2 = new EditText(this);
step2.setId(R.id.step2);
step2.setLayoutParams(new ConstraintLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
ConstraintSet constraintSet = new ConstraintSet();
ConstraintLayout constraintLayout = findViewById(R.id.parent);
constraintLayout.addView(step2);
constraintSet.clone(constraintLayout);
constraintSet.connect(step2.getId(),ConstraintSet.START,ConstraintSet.PARENT_ID,ConstraintSet.START,0);
constraintSet.connect(step2.getId(),ConstraintSet.END,ConstraintSet.PARENT_ID,ConstraintSet.END,0);
constraintSet.connect(step2.getId(),ConstraintSet.TOP,step1.getId(),ConstraintSet.BOTTOM,0);
constraintSet.constrainWidth(R.id.step2,ConstraintSet.MATCH_CONSTRAINT);
constraintSet.constrainHeight(R.id.step2,ConstraintSet.MATCH_CONSTRAINT);
constraintSet.applyTo((ConstraintLayout)findViewById(R.id.parent));

Assign ID to added EditText Android

Hy, I wanna ask something about dynamic EditText that's added by button click.
I have another scanner Button to scan qr code, get the value and set the value in each EditText added. To set the text I sure need to know the id of each EditText. So how do I assign id to each EditText or there's another way to work around with it.
Inside my add-button onclick
public void addView(View view){
LinearLayout li = (LinearLayout) findViewById(R.id.etMsisdn);
edt = new EditText(this);
edt.setId(0);
li.addView(edt);
}
Thank you very much
Hope to helpful
String strname = "task" + Integer.toString(i);
EditText editText = new EditText(this);
editTextsMap.put(strname, editText);
You can set a tag for your dynamically created EditText with calling setTag(Object tag) method. Then, you can simply find it with calling findViewWithTag() method.
// Dynamically create EditText
EditText editText = new EditText(this);
editText.setTag("editText");
// Find it via tag
EditText editText = (EditText) findViewWithTag("editText");

Android get data from LayoutInflater EditText

In my Activity, dynamically insert a layout that contains an EditText. The layout can have between one and N EditText. How can I get the text of all EditText ?
LinearLayout item = (LinearLayout) findViewById(R.id.rl);
final View child = getLayoutInflater().inflate(R.layout.inflate_ta, null);
item.addView(child);
MaterialEditText ed = (MaterialEditText) child.findViewById(R.id.ed12);
ed.setFloatingLabelText(edNomeCampo.getText().toString());
The best approach would be to go through each view within whatever parent view you're dynamically adding to.
so...
<LinearLayout
android:id="#+id/wrapper"..>
<EditText ... />
<EditText... />
...
...
<!--Nth EditText-->
<EditText... />
</LinearLayout>
Then once you want to get the values in each dynamically added EditText you would iterate through all views within that wrapping LinearLayout
LinearLayout yourLinearLayoutView = (LinearLayout)getView().findViewById(R.id.wrapper);
for(int i=0; i<yourLinearLayoutView.getChildCount(); i++) {
View editText= yourLinearLayoutView.getChildAt(i);
if(editText instanceof EditText){
String str = ((EditText)editText).getText();
//from here you can store str in whatever structure you wish (AWrrayList, etc.)
}
}

List Of Text And Clickable

I want to ask how to make a list of text that we can tap in each of the text and then get the selected text to editText.
I just added the screenshot
http://i.stack.imgur.com/ddZSg.png
I have been searching it since yesterday, but I can not find exact solution. I also tried with listview, but I don't know how it's possible with horizontal, and flow list item.
I am also new in android. But I can think of the logic what you want. You can try this out if you want.
First of all, you can make your list of texts EditText by list of buttons Buttons
You can dynamically add as many Buttons with their respective text as you want to show.
set their corresponding onClickListener
In their onClickListener , create an object of the EditText you are using to add texts.
Store the value of EditText into a String Variable first.
Add the text of the clicked Button to the variable.
and now again set the text in EditText with the variable you created to store the values.
Your task will be done.
Try referring this code.
and change the code accordingly as your needs.
// Adding EditText and a button in a new linear layout and then adding
// the new linearLayout to the main layout
String[] valuesToBeAdded={"A","B","C","D"};
String selectedValues=null;
LinearLayout mainLayout=(LinearLayout) findViewById(R.id.mainLayout);
LinearLayout localLayout = new LinearLayout(context);
localLayout.setOrientation(LinearLayout.VERTICAL);
localLayout.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
EditText editText=new EditText(context);
editText.setText(selectedValues);
editText.setId(5000);
localLayout.addView(editText);
for(int i=0;i<valuesToBeAdded.length();i++){
Button button = new Button(context);
button.setText(R.string.scanDocument);
button.setId(i);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
EditText ed=(EditText) findViewById(5000);
selectedValues=ed.getText();
selectedValues=selectedValues +" " + this.getText();
ed.setText(selectedValues);
}
});
localLayout.addView(button);
}
mainLayout.addView(localLayout);
Thank You
Could you not make as many buttons as you need and then in the button_Click method for all buttons add:
Buttonwithtext_Click(object sender, EventArgs e)
{
editTextBox.Text = editTextBox.Text + Buttonwithtext.text + ", ":
}

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