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);
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 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 i can dynamically add new EditText by the method onClick from Button? I have already an EditTextbut i'd like to add other EditTexton the same layout. Also, the EditText must have the same name with counter++
You have to use LayoutInflater. It's the way on Android SDK to do that things...
See this complete tutorial to understand how to use LayoutInflater: http://myandroidsolutions.blogspot.com.br/2012/06/android-layoutinflater-turorial.html
EditText edText = new EditText(this);
edText .setId("");
edText .setLayoutParams(new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,
1f));
#Override
public void onClick(View v) {
EditText edtNew = new EditText(this);
edtNew.setId("edtNew"+"parentLayoutName".getChildCount()+1); // "edtNew" is prefix name EditText id , "parentLayoutName" Where you add EitText
edtNew.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT));
"parentLayoutName".addView(edtNew);
}
How can I create a TextView (with Java code, not xml) with layout_width=fill_parent ?
The TextView should be editable.
If i don't understand question wrongly,you need to create an EditText programatically.then,Try using:
EditText et=new EditText(context);
et.setLayoutParams(new Gallery.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
EDIT :
Import for LayoutParams:
import android.view.ViewGroup.LayoutParams;
Its not a TextBox, its EditText in Android.
Anyway, you can create it run time using:
EditText ed = new EditText(this); // Create a new EditText
// Setting the type of input that you want
ed.setInputType(InputType.TYPE_CLASS_TEXT);
// setting height/width for your editText
ed.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
EditText et = new EditText(context);// this gets you a new textbox
//assuming you're in eclipse when you hit the . after et you'll see all its various properties that you can set as you like.
then when you're ready you either set it as the setContentView(et);// here it's the only thing on the screen
or
add it to whatever layout you have already set.
Create layoutParams with appropriate width and height by
LayoutParams params = //choose appropriate constructor
//beware about right import .
now Create textBox and setLayoutParams
EditText t = new EditText(this);
t.setLayoutParams(params);
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);