Android regain focus on EditText when they are created by code - android

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

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

Fired OnClick event in a LinearLayout is restricted to a very small zone

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

How to hide a dynamically created EditText box in android?

I want to make an attendance plus tasking system. For this, I have used check-boxes and a linear layout. Linear layout is for adding edit-text box dynamically. When a check-box is checked, a new text field is to be visible and when it is unchecked, the text field has to be hidden. I have used part of code something like this:
public void onCheckBoxClicked(View v) {
boolean checked = ((CheckBox) v).isChecked();
EditText tv;
tv = new EditText(this);
tv.setLayoutParams(new RadioGroup.LayoutParams(RadioGroup.LayoutParams.WRAP_CONTENT,
RadioGroup.LayoutParams.WRAP_CONTENT));
tv.setText(one.getText().toString()+ "'s task today?");
tv.setBackgroundColor(0xff66ff66); // hex color 0xAARRGGBB
tv.setPadding(20, 20, 20, 20);// in pixels (left, top, right, bottom)
switch (v.getId()) {
case R.id.checkBox1:
if(checked) {
list.addView(tv);
Toast.makeText(getApplicationContext(), one.getText().toString() + " is present", Toast.LENGTH_SHORT).show();
}
if(!checked) {
tv.setVisibility(EditText.GONE); //problem exists here--is not executing
Toast.makeText(getApplicationContext(), one.getText().toString() + " is absent", Toast.LENGTH_SHORT).show();
}
break;
Now, I can easily get a edit-text field when I check the box but, on unchecking it, the edit-text field doesn't hide. How can I solve it?
This is because you are creating new EditText each time some checkBox clicked. You are trying to hide EditText which isn't even shown/added yet. Got it!!
Try making your EditText tv = new EditText(this); editText as class variable , create it in onCreate. And then you can hide/show when you need it.
it would look something like below code.
EditText tv;
onCreate(.......){
..............
tv = new EditText(this);
..............
}
and then use this tv in your onCheckBoxClicked method instead of creating one in this method.

how to set OnEditorActionListener

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.

Retrieve values of a dynamic form in 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.

Categories

Resources