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..
Related
In this code i am able to create gridlayout. but the first item is in default view and the rest is like what i want. I try debug and I couldn't see what is wrong. Would you help me to solve this.
question="this is a sentence";
String sLetter;
String question = question.replace(" ", "");
//char[] charAr = question.toCharArray();
final ArrayList<String> letters = new ArrayList<>();
for (int k = 0; k < question.length(); k++) {
sLetter = Character.toString(question.charAt(k));
letters.add(sLetter);
}
Collections.sort(letters);
int i;
for (i = 0; i < letters.size();) {
int count = recursiveMethod(letters,i,1);
if (count>0) {
//region text add
View main_view = new View(context);
main_view.setLayoutParams(new LinearLayout.LayoutParams(100, ViewGroup.LayoutParams.WRAP_CONTENT));
gridLayout.setColumnCount(7);
if (question.length()<=7){
gridLayout.setRowCount(1);
}else if (question.length()<12){
gridLayout.setRowCount(2);
}else {
gridLayout.setRowCount(3);
}
GridLayout.Spec colSpan = GridLayout.spec(GridLayout.UNDEFINED,1);
GridLayout.Spec rowSpan = GridLayout.spec(GridLayout.UNDEFINED, 1);
GridLayout.LayoutParams gridParam = new GridLayout.LayoutParams(
rowSpan, colSpan);
gridParam.setGravity(Gravity.FILL_HORIZONTAL);
gridParam.setMargins(5, 10, 5, 10);
final TextView tv = new TextView(context);
tv.setText(letters.get(i).toUpperCase());
tv.setId(i);
tv.setPadding(15, 15, 15, 15);
tv.setTextColor(getResources().getColor(R.color.colorPrimaryLight));
RelativeLayout.LayoutParams relTv = new RelativeLayout.LayoutParams(80, ViewGroup.LayoutParams.WRAP_CONTENT);
relTv.addRule( RelativeLayout.CENTER_HORIZONTAL);
relTv.addRule( RelativeLayout.CENTER_VERTICAL);
tv.setLinksClickable(true);
tv.setTextSize(0, 60);
//tv.setLayoutParams(relTv);
CardView cardView = new CardView(context);
cardView.setCardBackgroundColor(getResources().getColor(R.color.colorPrimaryDark));
cardView.setRadius(10);
cardView.setCardElevation(5);
RelativeLayout relativeLayout = new RelativeLayout(context);
relativeLayout.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
final TextView textCount = new TextView(context);
textCount.setText(Integer.toString(count));
RelativeLayout.LayoutParams relParam = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
relParam.addRule(RelativeLayout.ALIGN_BOTTOM, i);
relParam.addRule(RelativeLayout.RIGHT_OF, i);
textCount.setTypeface(Typeface.DEFAULT_BOLD);
textCount.setTextColor(getResources().getColor(R.color.colorPrimaryLight));
textCount.setTextSize(0, 30);
textCount.setPadding(0,5,5,0);
//textCount.setLayoutParams(relParam);
relativeLayout.addView(tv,relTv);
relativeLayout.addView(textCount,relParam);
cardView.addView(relativeLayout);
//llLetters.addView(tv, lp);
gridLayout.addView(cardView,gridParam);
cardView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(context, tv.getText()+ "" + textCount.getText(), Toast.LENGTH_SHORT).show();
}
});
//llLetters.addView(gridLayout);
//endregion
i = i + count;
}
else {
i++;
}
}
The first letter of image is at the top left. I want to show that at the bottom right side of the letter. Would you help me to solve problem? Thanks =)
The problem lies here:
relParam.addRule(RelativeLayout.ALIGN_BOTTOM, i);
relParam.addRule(RelativeLayout.RIGHT_OF, i);
If you pass a zero to addRule, it is interpreted as false. Therefore you say, that ALIGN_BOTTOM should be false in your first letter, when i is zero.
See the documentation:
parameter: subject, int: the ID of another view to use as an anchor, or a boolean value (represented as RelativeLayout.TRUE for true or 0 for false).
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);
i mean editText created programmatically. I want to get the data inputted from those editText i created.
These are i've done so far.
VARIABLES
private GridLayout gridLayout;
//for tasks
int rowIndex = 2;
int colIndex = 1;
int rowIndex2 = 2;
int colIndex2 = 0;
int i=0;
int j=0;
//database variables
MyDBAdapter dbhandler;
To be able to create an editText in a gridlayout by a button click.
When "add new Task" button is clicked:
public void addTask(View view) {
i++;
Map<String, Integer> idsMap = new HashMap<String, Integer>();
String tname = "task" + Integer.toString(i);
EditText editText = new EditText(this);
GridLayout.LayoutParams param = new GridLayout.LayoutParams();
param.height = ViewGroup.LayoutParams.WRAP_CONTENT;
param.width = GridLayout.LayoutParams.MATCH_PARENT;
param.rowSpec = GridLayout.spec(rowIndex);
param.columnSpec = GridLayout.spec(colIndex);
editText.setTextColor(Color.BLACK);
editText.setBackgroundResource(android.R.drawable.edit_text);
editText.setText(tname);
editText.setLayoutParams(param);
TextView textView = new TextView(this);
GridLayout.LayoutParams param2 = new GridLayout.LayoutParams();
param2.rowSpec = GridLayout.spec(rowIndex2);
param2.columnSpec = GridLayout.spec(colIndex2);
textView.setPadding(30, 0, 0, 0);
textView.setTextColor(Color.BLACK);
textView.setTypeface(Typeface.DEFAULT_BOLD);
textView.setLayoutParams(param2);
if (rowIndex > 1) {
textView.setText("TASK "+Integer.toString(i)+": ");
editText.setId(i);
idsMap.put(tname, i);
}
gridLayout.addView(editText);
gridLayout.addView(textView);
rowIndex++;
rowIndex2++;
this.j = 0;
}
When "Add new Subtask" button is clicked:
public void addSubtask(View view) {
j++;
Map<String, Integer> idsMap = new HashMap<String, Integer>();
String taskno = "task" + Integer.toString(i) + "subtask" + Integer.toString(j);
EditText editText = new EditText(this);
GridLayout.LayoutParams param = new GridLayout.LayoutParams();
param.height = ViewGroup.LayoutParams.WRAP_CONTENT;
param.width = GridLayout.LayoutParams.MATCH_PARENT;
param.rowSpec = GridLayout.spec(rowIndex);
param.columnSpec = GridLayout.spec(colIndex);
editText.setTextColor(Color.BLACK);
editText.setBackgroundResource(android.R.drawable.edit_text);
editText.setText(taskno);
editText.setLayoutParams(param);
TextView textView = new TextView(this);
GridLayout.LayoutParams param2 = new GridLayout.LayoutParams();
param2.rowSpec = GridLayout.spec(rowIndex2);
param2.columnSpec = GridLayout.spec(colIndex2);
textView.setPadding(30,0,0,0);
textView.setTextColor(Color.BLACK);
textView.setLayoutParams(param2);
if (rowIndex > 1) {
textView.setText("Subtask "+Integer.toString(j)+": ");
editText.setId(j);
idsMap.put(taskno, j);
}
gridLayout.addView(editText);
gridLayout.addView(textView);
rowIndex++;
rowIndex2++;
}
IT WOULD LOOK LIKE THIS FOR EXAMPLE:
TASK 1: ___________
Subtask 1: ________
Subtask 2: ________
TASK 2: ___________
Subtask 1: ________
TASK 3: ___________
Assuming the user is done inputting the necessary info and clicks the submit button. THE PROBLEM COMES IN. I DONT KNOW HOW TO GET ALL THE VALUES BASED on the number of editText created by the user.
public void submit(){
submit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//implementation
String task1 = task1.getText().toString(); // It would only get task1
String subtask1 = task1subtask1.getText().toString(); // It would only get subtask1 of task 1
}
}
That kind of implementation is not usable since i dont know how many editTexts of tasks and subtasks that the user created. HELPPPP T___T
Every time you create an Edit Text use View.generateId() to generate an ID, store that ID in an HashMap or loop your container Views with getChildAt() and then, if its an EditText get its value.
Maybe you can set a custom tag for each edittext that you create.
You can determinate a specific generic tag for "Task" and an other for "Subtask" and after each creation, you set the tag to your edittext like that :
edittext.setTag(KEY, "task_X") where X is a incremental id.
After that, you can enumerate all children of your main container and check tags'children
List<EditText> allEdittext = new ArrayList<EditText>();
public void addSubtask(View view) {
j++;
Map<String, Integer> idsMap = new HashMap<String, Integer>();
String taskno = "task" + Integer.toString(i) + "subtask" + Integer.toString(j);
EditText editText = new EditText(this);
GridLayout.LayoutParams param = new GridLayout.LayoutParams();
param.height = ViewGroup.LayoutParams.WRAP_CONTENT;
param.width = GridLayout.LayoutParams.MATCH_PARENT;
param.rowSpec = GridLayout.spec(rowIndex);
param.columnSpec = GridLayout.spec(colIndex);
editText.setTextColor(Color.BLACK);
editText.setBackgroundResource(android.R.drawable.edit_text);
editText.setText(taskno);
editText.setLayoutParams(param);
TextView textView = new TextView(this);
GridLayout.LayoutParams param2 = new GridLayout.LayoutParams();
param2.rowSpec = GridLayout.spec(rowIndex2);
param2.columnSpec = GridLayout.spec(colIndex2);
textView.setPadding(30,0,0,0);
textView.setTextColor(Color.BLACK);
textView.setLayoutParams(param2);
if (rowIndex > 1) {
textView.setText("Subtask "+Integer.toString(j)+": ");
editText.setId(j);
idsMap.put(taskno, j);
}
allEdittext.add(editText);
gridLayout.addView(editText);
gridLayout.addView(textView);
rowIndex++;
rowIndex2++;
}
Then your get the values from the edit text
public void submit(){
submit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//implementation
String task1 = task1.getText().toString(); // It would only get task1
//i will be the position
String subtask1 = allEdittext.get(i).getText().toString();// It would only get subtask1 of task 1
}
}
Like this you can modify the code for addtask() function
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();
I have a TableRow that is loaded dynamically. It contains multiple EditTexts and CheckBoxes. How do I get the user input from these EditTexts and CheckBoxes?
This my Code
public void cargarProductos(SQLiteDatabase db){
TableLayout tabla = (TableLayout) findViewById(R.id.Detalle);
Cursor cProd = db.rawQuery("SELECT * FROM vstEncProductos WHERE IdCliente="+idCliente+" AND Idcategoria="+IdCategoria, null);
int ValorChk = 0;
if (cProd.moveToFirst()) {
do {
TableRow row = new TableRow(this);
row.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
row.setId(cProd.getInt(0));
ImageButton btnBarCode = new ImageButton(this);
btnBarCode.setBackgroundResource(R.drawable.barcodereader48);
btnBarCode.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
intent.setPackage("com.google.zxing.client.android");
startActivityForResult(intent, 0);
};
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
String contents = intent.getStringExtra("SCAN_RESULT");
String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
showDialog(R.string.result_succeeded, "Format: " + format + "\nContents: " + contents);
// Handle successful scan
} else if (resultCode == RESULT_CANCELED) {
// Handle cancel
}
}
}
});
row.addView(btnBarCode, new TableRow.LayoutParams(0));
TextView txtProducto = new TextView(this);
txtProducto.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
txtProducto.setText(cProd.getString(1));
txtProducto.setWidth(230);
row.addView(txtProducto, new TableRow.LayoutParams(1));
EditText txtPrecio = new EditText(this);
txtPrecio.setWidth(130);
txtPrecio.setInputType(InputType.TYPE_CLASS_NUMBER);
row.addView(txtPrecio, new TableRow.LayoutParams(2));
TextView txtPrecioAnt = new TextView(this);
txtPrecioAnt.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
txtPrecioAnt.setText(cProd.getString(7));
txtPrecioAnt.setWidth(115);
txtPrecioAnt.setGravity(Gravity.RIGHT);
row.addView(txtPrecioAnt, new TableRow.LayoutParams(3));
CheckBox chkPresencia = new CheckBox(this);
ValorChk=cProd.getInt(4);
chkPresencia.setChecked(ValorChk==-1);
chkPresencia.setWidth(50);
row.addView(chkPresencia, new TableRow.LayoutParams(4));
CheckBox chkPrecioPromo = new CheckBox(this);
ValorChk=cProd.getInt(4);
chkPrecioPromo.setChecked(ValorChk==-1);
chkPrecioPromo.setWidth(50);
row.addView(chkPrecioPromo, new TableRow.LayoutParams(4));
CheckBox chkAlerta = new CheckBox(this);
ValorChk=cProd.getInt(4);
chkAlerta.setChecked(ValorChk==-1);
chkAlerta.setWidth(50);
row.addView(chkAlerta, new TableRow.LayoutParams(4));
tabla.addView(row, new TableLayout.LayoutParams());
} while (cProd.moveToNext());
}
cProd.close();
I'm assuming you're using some sort of adapter class to load the data into your table. When you create a view, simply assign an OnClickListener to the checkboxes and a TextWatcher to the EditTexts. Use the callbacks from these classes to do what ever it is you want with the users input. Am I understanding your question correctly?
when you are creating new tablerows you can save each file in a multi vector of, for example, strings. Then you only must get size of each vector inside theparent vector. My example with your cody modified:
Vector<Vector<String>> result = new Vector<Vector<String>>();
Vector<String> vecAux = new Vector<String>();
TextView txtProducto = new TextView(this);
txtProducto.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
txtProducto.setText(cProd.getString(1));
txtProducto.setWidth(230);
row.addView(txtProducto, new TableRow.LayoutParams(1));
vecAux.add(cProd.getString(1));
EditText txtPrecio = new EditText(this);
txtPrecio.setWidth(130);
txtPrecio.setInputType(InputType.TYPE_CLASS_NUMBER);
row.addView(txtPrecio, new TableRow.LayoutParams(2));
TextView txtPrecioAnt = new TextView(this);
txtPrecioAnt.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
txtPrecioAnt.setText(cProd.getString(7));
txtPrecioAnt.setWidth(115);
txtPrecioAnt.setGravity(Gravity.RIGHT);
row.addView(txtPrecioAnt, new TableRow.LayoutParams(3));
vecAux.add(cProd.getString(7));
CheckBox chkPresencia = new CheckBox(this);
ValorChk=cProd.getInt(4);
chkPresencia.setChecked(ValorChk==-1);
chkPresencia.setWidth(50);
row.addView(chkPresencia, new TableRow.LayoutParams(4));
CheckBox chkPrecioPromo = new CheckBox(this);
ValorChk=cProd.getInt(4);
chkPrecioPromo.setChecked(ValorChk==-1);
chkPrecioPromo.setWidth(50);
row.addView(chkPrecioPromo, new TableRow.LayoutParams(4));
CheckBox chkAlerta = new CheckBox(this);
ValorChk=cProd.getInt(4);
chkAlerta.setChecked(ValorChk==-1);
chkAlerta.setWidth(50);
row.addView(chkAlerta, new TableRow.LayoutParams(4));
tabla.addView(row, new TableLayout.LayoutParams());
result.add(vecAux);
And get results looking for all position of the vector:
String texto = "";
for (int i = 0; i < result.size(); i++) {
for (int j = 0; j < result.elementAt(i).size(); j++) {
texto += result.elementAt(i).elementAt(j) + "\t";
}
}
For get if checkbox is checked or not you can get it by ID (findViewById).
I hope help you, bye bye ;-)