how can i getText() From edittext programmatically created - android

i create two edittext programmatically and i need to get text from him
this is my code for create edittext
layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(10, 10, 10, 10);
//First EditText
edText = new EditText(getActivity());
edText.setId(0);
edText.setLayoutParams(new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT,
1f));
edText.setBackgroundResource(R.drawable.edittext);
edText.setPadding(5, 5, 5, 5);
linear=(LinearLayout)rootView.findViewById(R.id.edittextpanel);
linear.addView(edText ,layoutParams);
nb++;
//Second EditText
edText = new EditText(getActivity());
edText.setId(1);
edText.setLayoutParams(new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT,
1f));
edText.setPadding(5, 5, 5, 5);
edText.setBackgroundResource(R.drawable.edittext);
linear.addView(edText ,layoutParams);
Help please i'm blocked

edText = new EditText(getActivity());
edText.setId(id);
String stringAnswer = edText.getText().toString();
If you want to create and reference more than one edit text you'll have to create an EditText array
List<EditText> allEDs = new ArrayList<EditText>();
Then create the Edit Texts
edText = new EditText(getActivity());
edText.setId(id);
Push them into the array
allEDs.add(edText);
Create a string array same size as the EditText array
String[] ETResults = new String[allEDs.size()];
Loop through the EditText array and put converted results into String array
for(int i=0; i < allEDs.size(); i++){
ETResults[i] = allEDs.get(i).getText().toString();
}
And then assign the desired string to whatever you want
String stringAnswer = ETResults[i];

try {
final ViewGroup viewGroup = (ViewGroup) ((ViewGroup) context.findViewById(android.R.id.content)).getChildAt(0);
for(int i = 0; i <viewGroup.getChildCount(); i++){
if(viewGroup.getChildAt(i) instanceof EditText) {
EditText view = (EditText)viewGroup.getChildAt(i);
Log.i("val " , " val " + ((EditText) viewGroup.getChildAt(i)).getText() + " view "+ view.getText() );
}
}
}catch (Exception ex){
Log.e("Error in Bind layout", ex.toString());
}

Maintain a reference to both EditTexts. Put the following in the main body of your Fragment to make them globally accessible..
EditText edText1;
EditText edText2;
Then in the method you've shown you can use...
// Do some stuff
edText1 = new EditText(getActivity());
// Do some more stuff
edText2 = new EditText(getActivity());
...
Later when you need to get the text you can use...
String text1 = edText1.getText().toString();
String text2 = edText2.getText().toString();
Alternatively you can find them later in your Fragment using their ids...
LinearLayout linear = (LinearLayout) findViewById(R.id.edittextpanel);
EditText et = (EditText) linear.findViewById(0);
String text1 = et.getText().toString();
et = (EditText) linear.findViewById(1);
String text2 = et.getText().toString();

Related

Android Studio: getting an input from user

I'm new to Android Studio. I would like to read input from user from a dynamically created elements. I've already managed to create the elements. Now I do not know at which place should I place the reading element. Can someone give me a MWE here ? I do not know also if I should use getText(this) or editText(this) or something else.
public void click(View view) {
String col1;
String col2;
TableLayout tl = (TableLayout)findViewById(R.id.tableLayout1);
TableRow row = new TableRow(this);
TextView tv = new TextView(this);
TextView c = new TextView(this);
EditText etUserInfoNewValue = (EditText)findViewById(R.id.simpleEditText);
tv.setText("This is text");
tl.addView(row);
row.addView(tv);
for (int x = 0; x < 5; x++) {
col1 = "(" + x + ")"+Integer.toBinaryString(x);
col2 = "1";
TableRow newRow = new TableRow(this);
TextView column1 = new TextView(this);
TextView column2 = new TextView(this);
EditText editText1 = new EditText(this);
TextView column3 = new TextView(this);
column1.setText(col1);
column1.setText(col1);
column2.setText(col2);
newRow.addView(column1);
newRow.addView(editText1);
newRow.addView(column3);
tl.addView(newRow, new TableLayout.LayoutParams());
}
}

How to get ID of multiple EditText dynamically added?

I've added multiple EditText and TextViews dynamically like:
final String[] meses = new String[]{"Janeiro", "Fevereiro", "Março", "Abril", "Maio", "Junho", "Julho", "Agosto", "Setembro", "Outubro", "Novembro", "Dezembro"};
for(int i=0; i<meses.length; i++){
LinearLayout layout = new LinearLayout(getContext());
layout.setOrientation(LinearLayout.HORIZONTAL);
layout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
TextView textview = new TextView(getContext());
ViewGroup.LayoutParams lparams = new TableLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, 1f);
textview.setLayoutParams(lparams);
textview.setText(meses[i]);
layout.addView(textview);
EditText ed = new EditText(getContext());
ed.setLayoutParams(lparams);
ed.setId(i);
layout.addView(ed);
llMes.addView(layout);
}
But now I need to retrieve the values of each EditText, how can I retrieve it, I have this:
for(int i=0; i < meses.length; i++){
EditText et = null;
et.getId();
pago = quotas - Integer.parseInt(et.getText().toString());
}
And also tried with findViewById(i) with for but also failed.
Thanks for helping me!
Add EditTexts to an ArrayList then retrieve it by index.
//Arraylist that will contain EditTexts
ArrayList<EditText> list = new ArrayList<>();
// for each EditText add it to the ArrayList
for(int i=0; i < 10; i++){
EditText et = new EditText();
list.add(et);
}
//To retrieve EditText
EditText et = list.get(0);

anonymous android.view.View.onclicklistener error

I am trying to get the input of user from edit text, and then create more Edittext boxes into my linear layout depending on user input, but I am having this error on my code.
EditText (android.content.Context)in EditText cannot be applied
to(anonymous android.view.View.OnClickListener)
Here's some of my code.
linear = (LinearLayout) findViewById(R.id.linear);
I have error on this line final EditText ed = new EditText(this);
try{
int noofstud = Integer.parseInt(numberofstudents.getText().toString()); //get number
final EditText[] myEditText = new EditText[noofstud];
for(int i = 0; i < noofstud; i++){
final EditText ed = new EditText(this);
linear.addView(ed);
myEditText[i] = ed;
}
}catch(Exception e){
e.printStackTrace();
}
Use your ClassName.this in new EditText(this);
suppose your class name is MainActivity
then use this as follows
final EditText ed = new EditText(MainActivity.this);

Creating TableLayout From Class

Icreate this post, because i am new at this, and i need a little help. I am doing a little exercise about a application you that with some values you can make a tablelayout from class. But after i push the button i get the error "The specified child already has a parent. You must call removeView() on the child's parent first"
public class TabelFlat extends Activity{
//deklarasi variabel
String jumlah,jasa,jangkawaktu;
int jum = 0, jsa = 0, jkwaktu = 0;
int a = 0, b = 0, c = 0, d = 0, i = 0;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle extras = getIntent().getExtras();
//mengambil value yg diimport dari class lain
if (extras != null) {
jumlah = extras.getString("jumlah");
jasa = extras.getString("jasa");
jangkawaktu = extras.getString("jangkawaktu");
}
//konversi string menjadi int
jum = Integer.parseInt(jumlah);
jsa = Integer.parseInt(jasa);
jkwaktu = Integer.parseInt(jangkawaktu);
//membuat tabel
TableLayout MainLayout = new TableLayout(this);
MainLayout.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.FILL_PARENT));
MainLayout.setStretchAllColumns(true);
//Baris Judul
TableRow row1 = new TableRow(this);
TextView text1 = new TextView(this);
text1.setText("Bulan");
TextView text2 = new TextView(this);
text2.setText("Total Angsuran");
TextView text3 = new TextView(this);
text2.setText("Angsuran");
TextView text4 = new TextView(this);
text2.setText("Bunga");
TextView text5 = new TextView(this);
text2.setText("Sisa Saldo");
text2.setGravity(android.view.Gravity.RIGHT);
//memasukkan nilai text ke row
row1.addView(text1);
row1.addView(text2);
row1.addView(text3);
row1.addView(text4);
row1.addView(text5);
//menambah row ke tabel
MainLayout.addView(row1);
//kalkulasi
TextView tangsuran = new TextView(this);
a = (jum * 120/100);
tangsuran.setText(""+String.valueOf(a));
TextView angsuran = new TextView(this);
b = jum / jkwaktu;
angsuran.setText(""+String.valueOf(b));
TextView bunga = new TextView(this);
c = jum * 20 / 100 / jkwaktu;
bunga.setText(""+String.valueOf(c));
//loop
d = jum;
for (i = 1;i<=jkwaktu;i++){
TableRow barisloop = new TableRow(this);
//membuat string kosong untuk setiap kolom
TextView bulan = new TextView(this);
bulan.setText(""+i);
TextView sisa = new TextView(this);
d = d - b;
sisa.setText(""+String.valueOf(d));
barisloop.addView(bulan);
barisloop.addView(tangsuran);
barisloop.addView(angsuran);
barisloop.addView(bunga);
barisloop.addView(sisa);
MainLayout.addView(barisloop);
}
setContentView(MainLayout);
}
}
here you add the tangsuran, angsuran and bunga text view to new row, but they are already added to another one.You must create new objects for the rows in the loop.
for (i = 1;i<=jkwaktu;i++){
TableRow barisloop = new TableRow(this);
//membuat string kosong untuk setiap kolom
TextView bulan = new TextView(this);
bulan.setText(""+i);
TextView sisa = new TextView(this);
d = d - b;
sisa.setText(""+String.valueOf(d));
barisloop.addView(bulan);
//barisloop.addView(tangsuran);
//barisloop.addView(angsuran); CREATE NEW TEXTVIEWS
//barisloop.addView(bunga);
barisloop.addView(sisa);
MainLayout.addView(barisloop);
}

How to get the dynamically created form values in android?

I have developed a form in dynamically using some edit text,check boxes,radio buttons and buttons.I have created form successfully ,but how i can get the values from that and stored in database.Please can any one help me.
Thanking in Advance.
public void textView() {
idText++;
i++;
LinearLayout linearLayoutHorizantal1 = new LinearLayout(
getApplicationContext());
linearLayoutHorizantal1.setOrientation(LinearLayout.HORIZONTAL);
/*
* LinearLayout.LayoutParams params = new
* LinearLayout.LayoutParams(LayoutParams
* .WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
* linearLayoutHorizantal1.setGravity(Gravity.RIGHT);
*/
txtQuetion = new TextView(getApplicationContext());
txtQuetion.setText(i + ". " + strQuestionText);
txtQuetion.setTextColor(Color.BLACK);
txtQuetion.setTextSize(20);
txtQuetion.setId(idText);
linearLayoutHorizantal1.addView(txtQuetion);
linearLayoutDynamicAdd.addView(linearLayoutHorizantal1);
}
public void RadioButtons() {
radiogroup = new RadioGroup(getApplicationContext());
radiogroup.setOrientation(RadioGroup.HORIZONTAL);
RadioGroup.LayoutParams params = new RadioGroup.LayoutParams(
SurveyFillActivity.this, null);
params.setMargins(10, 10, 10, 0);
linearLayoutDynamicAdd.addView(radiogroup);
for (int j = 0; j < answersList.length; j++) {
if (answersList[j] != null) {
idRadio++;
rb = new RadioButton(getApplicationContext());
rb.setId(idRadio);
rb.setText(answersList[j]);
rb.setTextColor(Color.BLACK);
rb.setButtonDrawable(R.drawable.radio_custom);
// rb.setChecked(true);
rb.setLayoutParams(params);
radiogroup.addView(rb);
}
}
}
private EditText editText(int _intID) {
idEditBox++;
final LayoutParams lparams = new LayoutParams(350, 50);
final EditText et = new EditText(this);
lparams.leftMargin = 20;
lparams.topMargin = 10;
et.setLayoutParams(lparams);
et.setWidth(32);
et.setEms(50);
et.setBackgroundResource(R.drawable.customborder_backbutton);
/*
* EditText et = new EditText(getApplicationContext());
* et.setId(idEditBox); et.setHeight(60); et.setWidth(50);
*/
//et.setBackgroundColor(Color.WHITE);
String s1 = et.getText().toString();
linearLayoutDynamicAdd.addView(et, lparams);
return et;
}
The above code for creating forms and i need to get the values form it and store in Database.
Create the arrays for texviews and radiobuttons ...what ever you need in your form.
take this as reference
TextView tv[] = new TextView[HowManyYouNeed];
TextView tvsa = new TextView(YourContax);
tv[position]=tvsa;//position like o ..1..2
tv[0].getText();
May be help full..

Categories

Resources