Add EditText by button - android

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

Related

How to remove dynamically created views on button click

public void Add_text() {
ll.setOrientation(LinearLayout.HORIZONTAL);
ll.setId(i);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
TextView product = new TextView(getActivity());
product.setText(" Product" + 5 + " ");
ll.addView(product);
EditText qty = new EditText(getActivity());
qty.setText(i + "");
qty.setId(i);
qty.setWidth(120);
ll.addView(qty);
Button btn = new Button(getActivity());
ll.addView(btn);
btn.setLayoutParams(params);
btn.setOnClickListener(o);
ly.addView(ll);
i++;
}
I wrote the above code to create the textfields and buttons dynamically; But now I need to remove 2 textfields and a button when the button is clicked. How do I do that?
Try following code.
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
LinearLayout linearParent = (LinearLayout) v.getParent().getParent();
LinearLayout linearChild = (LinearLayout) v.getParent();
linearParent.removeView(linearChild);
}
});
Explanation
Here first take "GrandParent" of any view.
Then take its "Parent" view
With reference to "GrandParent" remove that "Parent" view.
this will remove all views which that "Parent" holds. As per your code, your "ll" will be "linearChild" here. And "ly" will be "linearParent" here. So whole "ll" will be removed from "ly" which you have added dynamically.
If you want to permanently remove the views you created.
OnClick(View view){
ly.removeAllViews()
}
If you do not want to permanently remove the views you created.
OnClick(View view){
ly.setVisibility(View.GONE); //This will hide the all views
qty.setVisibility(View.GONE);//This will hide the EditText qty
product .setVisibility(View.GONE);//This will hide the TextView product
}
So use appropriate code line which you want.
EDIT:
Use this code for your situation:
public void Add_text() {
ll.setOrientation(LinearLayout.HORIZONTAL);
ll.setId(i);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
TextView product = new TextView(getActivity());
product.setText(" Product" + 5 + " ");
ll.addView(product);
EditText qty = new EditText(getActivity());
qty.setText(i + "");
qty.setId(i);
qty.setWidth(120);
ll.addView(qty);
Button btn = new Button(this);
ll.addView(btn);
btn.setLayoutParams(params);
ly.addView(ll);
i++;
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View button) {
qty.setVisibility(View.GONE);//This will hide the EditText qty
product .setVisibility(View.GONE);//This will hide the TextView product
}
});
}
i think you got to use this method on your LinearLayout :
public void removeView (View view)
first you call :
EditText et = (EditText)linearLayout.findViewById(yourEditText.getId());
then call the remove view method :
linearLayout.removeView (et) ;
and to remove all of the Views that are in the LinearLayout do the following :
public void removeAllViews ()
like the following :
linearLayout.removeAllViews()
and give me some feedback
Hope that Helps .
you can simply use qty.setVisibility(View.GONE) on the onClickListener() of the Button of your choice. like this.
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
qty.setVisibility(View.GONE); //for temporary purpose
//or u can also do this
layout.removeView(qty); //removes permanently
}
});
The benefit of using View.GONE is that you can get the View back if you want but layout.removeView(qty) will remove the view permanently and you have to re add the view again.
[EDIT] 1. changed to View.GONE instead of View.INVISIBLE because of reasons explained here
Hope I answered your question. :)
just use index for which you want to remove your view from linear layout
Linearlayout.removeViewAt();
if you want again that view then you can call addViewAt()in same way.
I hope it will help you.
Add this line
mLayout.removeViewAt(mLayout.getChildCount()-1);

Add new EditText component to an Activity at run time

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

how to get values from edit text without using xml code

I have created edittext and textview without using xml attributes i have defined in java file only but i am unable get values entered in edittext how to get values.i have 2 edittexts if user enters the values i should add the values
public class JavacodeActivity extends Activity {
LinearLayout layout;
TextView view;
EditText edit;
Button btn;
EditText edit1;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
layout=new LinearLayout(this);
layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
layout.setOrientation(LinearLayout.VERTICAL);
view=new TextView(this);
view.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
view.setText("enter the value");
view.setGravity(Gravity.CENTER);
view.setTextColor(Color.BLUE);
view.setTextSize(20);
layout.addView(view);
edit1=new EditText(this);
edit1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
edit1.setHint("enter the number");
edit1.setGravity(Gravity.CENTER);
edit1.setTextColor(Color.RED);
edit1.setTextSize(20);
edit1.setInputType(InputType.TYPE_CLASS_NUMBER);
layout.addView(edit1);
btn=new Button(this);
btn.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
btn.setText("add");
btn.setGravity(Gravity.CENTER_HORIZONTAL);
btn.setTextColor(Color.GREEN);
btn.setTextSize(20);
layout.addView(btn);
view=new TextView(this);
view.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
view.setText("addition of values");
view.setGravity(Gravity.CENTER);
view.setTextColor(Color.BLUE);
view.setTextSize(20);
layout.addView(view);
I don't know the attribute to retrieve values from edittext if the code is java I am unable to retrieve values. Please help me.
I don't know the attribute to retrieve values from edittext if the code is java I am unable to retrieve values
Read your EditText like any other EditText:
String text = edit.getText().toString();

Android: Creating a TextView programmatically

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

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

Categories

Resources