in my app i create a set of editText dynamically. how can we set values to a dynamically created editText. i create the editText and add this editText to list
the code for creating the editText is given below
LinearLayout layout = new LinearLayout(context);
layout.setId(expRow2);
layout.setOrientation(LinearLayout.HORIZONTAL);
layout.setGravity(Gravity.CENTER);
for(int i=1;i<=4;i++)
{
EditText editText = new EditText(context);
editText.setHint(exp_EditTextName[i]);
editText.setId(expRow+i);
editText.setEms(10);
editText.setLayoutParams(margins);
layout.addView(editText);
expEditTextList.add(editText);
}
and i need to setValues to editText on other part of my code
final LinearLayout exp = new LinearLayout(context);
exp.setOrientation(LinearLayout.VERTICAL);
exp.setBackgroundColor(Color.GRAY);
exp.setDescendantFocusability(LinearLayout.FOCUS_AFTER_DESCENDANTS);
Button expAdd = new Button(context);
expAdd.setId(123);
expAdd.setBackgroundResource(R.drawable.small_add);
expAdd.setLayoutParams(marginLeftElement);
exp.addView(expAdd);
exp.addView(layout);
EditText edittext = (EditText)expEditTextList.get(0);
edittext.setText("hai");
but i cant set values to the editText.
try like this..
final EditText text1 = (EditText) findViewById(R.id.text1);
final EditText text2 = (EditText) findViewById(R.id.text2);
final EditText text3 = (EditText) findViewById(R.id.text3);
text2.addTextChangedListener(new TextWatcher() {
void afterTextChanged(Editable s) {
text3.setText(text1.getText().toString() + text2.getText().toString());
};
});
for more information try the link
set text on dynamically created edittext in android?
Related
RelativeLayout blur = (RelativeLayout) findViewById(R.id.relative5);
TextView name_tv = new TextView (this);
name_tv.setText("\n\n First Name");
blur.addView(name_tv);
Typeface font=Typeface.SERIF;
name_tv.setTypeface(font);
final EditText nameedit = new EditText(this);
nameedit.setHint("First name");
blur.addView(nameedit);
Typeface font33=Typeface.SERIF;
nameedit.setTypeface(font33);
nameedit.setMaxLines(1);
nameedit.setImeOptions(EditorInfo.IME_ACTION_NEXT);
nameedit.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
Similarly i add few text view and Edit text dynamically in this class.The problem is when i add scrollview in xml it show me error.....
How can i create edittext and radiobutton dynamically so that either
i can write in edittext or can click the radiobutton but i should
not do the both, and then what ever i have selected or written the answer
i need to save.
i search for it but i did not get any appropriate answer , Please help me !
RadioButton toggleRadioButton;
RadioGroup editRadiogroup = new RadioGroup(this);
editRadiogroup.setOrientation(RadioGroup.HORIZONTAL);
LinearLayout editlayout=new LinearLayout(this);
editlayout.setOrientation(LinearLayout.HORIZONTAL);
try
{
for (int i = 0; i < answer.length; i++)
{
int id = Integer.parseInt(5+ "" + i);
// add edit text
EditText editText;
editText = new EditText(this);
editText.setMaxLines(1);
editText.setGravity(Gravity.LEFT);
editText.setInputType(InputType.TYPE_CLASS_NUMBER);
editText.setId(id);
editlayout.addView(editText, WrapWidth, WrapHeight);
TextView tv = new TextView(this);
tv.setTextColor(Color.BLACK);
tv.setTextSize(18);
tv.setText("Year" +" " +"(or)"+" ");
editlayout.addView(tv);
// add radio button
toggleRadioButton = new RadioButton(this);
toggleRadioButton.setChecked(false);
toggleRadioButton.setTextSize(15);
toggleRadioButton.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
editRadiogroup.addView(toggleRadioButton,WrapWidth1,WrapHeight);
Hi you can listen on changes on the Edit text:
How do listen EditText?
When you get text written in the edit text you can make the toggleRadiobutton dissapear with toggleRadiobutton.setVisibilty(View.INVISIBLE) or you disable it with toggleRadiobutton.setEnabled(false).
The same if you detect that there is no text in the edittext, you can re-enable the togglebutton.
With the toggleRadioButton you would take the same approach. You check the status of button in on OnClickListener and enable or disable the Edittext accordingly:
toggleRadioButton.setOnClickListener(new OnClickListener(View v){
if(((RadioButton)v).isChecked()){
edittext.setEnabled(false);
}else{
edittext.setEnabled(true);
}
});
I have a situation where there are 3 editText fields created dynamically and those values should be stored in vector and should be displayed again after i click on button "ADD" after three fields are added?
Any help will be appreciated, thanks in advance.
You should set OnClickListener
final EditText editText1 = new EditText(context);
final EditText editText2 = new EditText(context);
final EditText editText3 = new EditText(context);
final Vector<String> vector = new Vector<String>();
// Adding editTextes to layout code here....
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
vector.add(editText1.getText());
vector.add(editText2.getText());
vector.add(editText3.getText());
}
});
final LinearLayout ll=new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
scrl.addView(ll);
Button add_btn=new Button(this);
add_btn.setText("Click to add TextViiews and EditTexts");
ll.addView(add_btn);
add_btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//String str;
TextView tv=(TextView)findViewById(R.id.tv1);
EditText et2=new EditText(getApplicationContext());
String s=et2.getText().toString();
tv.setText(s);
ll.addView(et2);
I have created a button when i click on this button i will get edittext dynamically if i enter a value i should display the value I don't know how to display value and make use of it if suppose i want to add entered values in dynamically created edit text.
You cannot get text if you haven't added the view.Also you add textview on click of button. You can remove textview creation from onClick() and add it outside of it
EDIT(something like this):
final LinearLayout ll=new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
scrl.addView(ll);
Button add_btn=new Button(this);
add_btn.setText("Click to add TextViiews and EditTexts");
ll.addView(add_btn);
add_btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
String s=et2.getText().toString();
tv.setText(s);
}
});
TextView tv=(TextView)findViewById(R.id.tv1);
EditText et2=new EditText(getApplicationContext());
ll.addView(et2);
I have a view in which there is a text box where user enters data, when clicks on submit, I want to store the input and display in another box.
final EditText edittext = (EditText) findViewById(R.id.edittext);
mText = (TextView) findViewById(R.id.timepicker_input);
How can I do it, please help
final EditText edittext = (EditText) findViewById(R.id.edittext);
TextView mText = (TextView) findViewById(R.id.timepicker_input);
mText.setText(edittext.getText().toString());
String info = edittext.getText().toString();
mText.setText(info);
final EditText edittext = (EditText) findViewById(R.id.edittext);
TextView mText = (TextView) findViewById(R.id.timepicker_input);
String storedData = edittext.getText().toString();
mText.setText(storedData);
Here, you can use the string storedData in another activity or to setText for another editText (textbox)