dynamic create editbox in android? - 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);

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

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.

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

How to apply style to dynamically created EditText in android?

I have one Button, and on every click of that button I want to create new EditText Dynamically.
Now my question is , I have one style in style.xml for EditText. Now how can I apply that style to this dynamically created EditText?
My Code for EditText is:
List<EditText> allEdsLabReport ;
String[] edsLabReport;
EditText LabReportNm;
AddMoreLabReport.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
LabReportNm = new EditText(CaseReport.this);
allEdsLabReport.add(LabReportNm);
TableLayout tbl2 = (TableLayout)findViewById(R.id.TableLayoutLabReport);
TableRow tr1 = new TableRow(CaseReport.this);
tr1.addView(LabReportNm);
tbl2.addView(tr1);
}
});
new EditText(new ContextThemeWrapper(CaseReport.this, R.style.my_style));
LabReportNm.setTextAppearance(getApplicationContext(), R.style.style.xml)
editText.setTextAppearance(getApplicationContext(), R.drawable.demo.xml);

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 + ", ":
}

Categories

Resources