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.
Related
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));
I am new to android development.
I want to add EditText and a remove button ( X ) when they click on a button (called “contact”). So that there will be multiple contacts. On click of the remove button the corresponding added EditText with remove button should be disappeared.
It will be something like this,
————————— X
————————— X
————————— X
————————— X
———————
|contact|
———————
If I use any Adapter, how to add the empty EditText and a remove button ( X ). If not any other better options?
Finally I need to get all the values entered in the EditText.
NOTE: On click of “contact” it should be adding EditText and a remove button ( X ).
Kindly give me a clear and simple way to do this.
Thanks!
Create a LinearLayout and button. On the button click call the following method. Also, create a List<EditText> in your onCreate Method to track all added edittexts & retrieve texts later.
private void createNewEditText() {
EditText editText = new EditText(this);
editText.setMaxLines(1);
editText.setSingleLine(true);
editText.setLayoutParams(ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
editText.setCompoundDrawablesWithIntrinsicBounds(null, null, VectorDrawableCompat.create(resources, R.drawable.ic_clear_black_24dp, null), null);
editText.setOnTouchListener(new View.OnTouchListener {
#Override
Boolean onTouch(View view,MotionEvent event) {
val DRAWABLE_BOTTOM = 3
val DRAWABLE_RIGHT = 2
val DRAWABLE_LEFT = 0
val DRAWABLE_TOP = 1
if (event.getRawX() >= (editText.right - editText.getCompoundDrawables()[DRAWABLE_RIGHT].bounds.width())) {
editTextList.remove(editText);
parentLinearLayout.removeView(editText);
return true;
}
return false;
}
})
editText.requestFocus();
editText.setText(voicetext);
editTextList.add(editText);
parentLinearLayout.addView(editText);
}
You can use a LinearLayoutwith orientation set to vertical. you can then create another layout with yourEditTextand Button and add them dynamically to the LinearLayout within the onClick method for contact button.
Do not forget to generate different IDs for your newly added views to be able to find them later
The X button will then just remove its parent from the layout
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");
I am using code to create a new TextView and a new EditText whenever I click on a button. The TextView and EditText are created but the problem is that when I focus on the new EditText it is impossible to regain focus on the previous ones. Maybe the user has entered a wrong information so I would to regain access to that EditText.
I show you the code I have used:
public void anadir(View view){
TextView nuevoTexto = new TextView(this);
EditText nuevoEdito = new EditText(this);
nuevoTexto.setText("Dirección correo nº"+contador);
nuevoTexto.setTextSize(15);
nuevoTexto.setPadding(margenIzquierdoTexto, margenArribaTexto, 0, 0);
nuevoEdito.setEms(10);
nuevoEdito.setInputType(InputType.TYPE_CLASS_TEXT);
nuevoEdito.setImeOptions(EditorInfo.IME_ACTION_NEXT);
nuevoEdito.setFocusable(true);
nuevoEdito.setPadding(margenIzquierdoEdito, margenArribaEdito, 0, 0);
padreTexto.addView(nuevoTexto);
padreEdito.addView(nuevoEdito);
textos.add(nuevoTexto);
editos.add(nuevoEdito);
contador++;
margenArribaTexto += 40;
margenArribaEdito += 40;
}
I have used the following options to regain that access but it doesn´t work.
nuevoEdito.setFocusable(true);
nuevoEdito.setFocusableInTouchMode(true);
nuevoEdito.setClickable(true);
try:
nuevoEdito.requestFocus();
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);