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));
Related
I want to use some LinearLayouts on my app to make a certain action if the user clicks on them outside of elements they may have with "active" behaviors like EditTexts, Buttons or similar things.
In one of these LinearLayouts I've set a pair of EditText and with them being shown the OnClick event only is detected in a small area of the layout, even if the click happens far from those EditTexts. Maybe that clicks are doing a focus on one of my EditText when they are performed, but ironically the event fires in an area which is more near to the EditTexts than other ones where the click is ignored.
I've also checked that this doesn't happen with some other elements, if I press even not only near, but over a TextView or ImageView the event is fired properly.
Any idea on how to solve this issue?
PS: Per request I'm giving the code of how the linearlayout is used, though I did it programmaticaly not by xml:
protected EditText initInput(EditText selectedInput)
{
selectedInput = new EditText(this);
selectedInput.setInputType(InputType.TYPE_CLASS_TEXT);
return selectedInput;
}
...
AlertDialog dialog = null;
EditText input;
EditText input2;
LinearLayout ll = new LinearLayout(this);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(30, 20, 30, 0);
dialog = new AlertDialog.Builder(this)
.setView(ll)
.create();
dialog.setCancelable(true);
TextView tv = new TextView(this);
ll.addView(tv,layoutParams);
input = initInput(input);
ll.addView(input,layoutParams);
TextView tv2 = new TextView(this);
ll.addView(tv2,layoutParams);
input2 = initInput(input2);
ll.addView(input2,layoutParams);
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");
EditText lastedit = new EditText(this);
lastedit.setHint("Last name");
blur.addView(lastedit);
MarginLayoutParams params22 = (MarginLayoutParams) lastedit.getLayoutParams();
params22.width = 460; params22.leftMargin = 16; params22.topMargin = 265;
lastedit.setLayoutParams(params22);
Typeface font22=Typeface.SERIF;
lastedit.setTypeface(font22);
lastedit.setMaxLines(1);
lastedit.setImeOptions(EditorInfo.IME_ACTION_NEXT);
here i want that when i press enter it will automatically go to next field. I attact 4 edit text view in my code dynamically like above and then finally automatically button will press and go to next activity.
use setInputType for EditText. It will solve your problem.
How can i add a new EditText component to an Activity layout? I don't want to add the component from the GUI editor statically, but add it to the activity at run time, for example when the user clicks a button?
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.ur_layout);
EditText editTextView = new EditText(this);
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT, 1);
editTextView.setLayoutParams(params);
linearLayout.addView(editTextView);
Have the EditText in the layout XML for your activity with android:visibility="gone".
When you want to show the EditText do:
EditText ed = (EditText) findViewById(R.id.your_edit_text);
ed.setVisibility(View.VISIBLE);
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);