android Edit Text Customization - android

for (int i = 0; i < sub.length; i++) {
tr[i] = new TableRow(this);
EditText et = new EditText(this);
et.setText(message2);
tr[i].addView(et);
EditText et1 = new EditText(this);
et1.setText(sub[i]);
tr[i].addView(et1);
EditText et2 = new EditText(this);
et2.setText(cde[i]);
tr[i].addView(et2);
EditText et3 = new EditText(this);
et3.setText(crd[i]);
tr[i].addView(et3);
ll.addView(tr[i]);
}
This is my code to create four edit text components in a row.
I need to give same text parameters to all the edittext components. But using methods to all four edittext components individually makes the code too lengthy.
Is there any solution so that I can use only one method to set text parameters for all the edittext components?

Using a method while adding your EditTexts to your TableRow can make your code easier to read.
private void addEditTextToTableRow(TableRow tableRow, String text) {
EditText editText = new EditText(this);
editText.setText(text);
tableRow.addView(editText);
}
Your for loop then becomes:
for (int i = 0; i < sub.length; i++) {
TableRow tr[i] = new TableRow(this);
addEditTextToTableRow(tr[i], message2);
addEditTextToTableRow(tr[i], sub[i]);
addEditTextToTableRow(tr[i], cde[i]);
addEditTextToTableRow(tr[i], crd[i]);
ll.addView(tr[i]);
}

try creating your components in the XML file, which will serve as a vision, and activity or in the XML Set The only text you need to set ... if you need utilzar one table, use the listView in XML and put all your EditText inside the listView

Related

What's the best practice for keeping track of dynamically generated EditText?

I have a quantity of items which I'm retrieving from a database. The user is expected to enter a serial number for every item, and the input values are to be posted back to the database. Thus, a dynamic EditText is to be generated for every item. I normally do it this way:
int itemQuantity;
List<EditText> listEditText = new ArrayList<EditText>();
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linearLayout);
for(int i = 0; i < itemQuantity); i++){
EditText editText = new EditText(this);
editText.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
linearLayout.addView(editText);
listEditText.append(editText);
}
Then I use listEditText.get(index).getText().toString(); to retrieve values from each EditText.
Is this the right way of doing when considering performance and good practices?

Cant get the value of an Edittext [duplicate]

This question already has answers here:
Android getText from EditText field
(6 answers)
Closed 8 years ago.
hi i made a program that have EditText... i used for loop to set the number of my EditText, i cant get the value of my EditText(excellent), how can i do that? please help me..my output goes this way...
i want that 3,2,5,1,4 will display in my EditText,,
here are my codes...
final TableLayout table = new TableLayout(getApplicationContext());
table.setVerticalScrollBarEnabled(true);
table.setPadding(10, 10, 10, 10);
TableRow tableRow = new TableRow (getApplicationContext());
TextView txt = new TextView (getApplicationContext());
TextView txt2 = new TextView (getApplicationContext());
TextView txt3 = new TextView (getApplicationContext());
TextView txt4 = new TextView (getApplicationContext());
TextView txt5 = new TextView (getApplicationContext());
TextView txt6 = new TextView (getApplicationContext());
tableRow.addView(txt);
tableRow.addView(txt2);
tableRow.addView(txt3);
tableRow.addView(txt4);
tableRow.addView(txt5);
tableRow.addView(txt6);
tableRow.setBackgroundColor(Color.GRAY);
txt.setText("Question ");
txt2.setText("Excellent ");
txt3.setText("Best ");
txt4.setText("Better ");
txt5.setText("Good ");
txt6.setText("Poor ");
txt.setTextColor(Color.BLACK);
txt2.setTextColor(Color.BLACK);
txt3.setTextColor(Color.BLACK);
txt4.setTextColor(Color.BLACK);
txt5.setTextColor(Color.BLACK);
txt6.setTextColor(Color.BLACK);
table.addView(tableRow);
TableRow tableRow2 = null;
EditText excellent = null;
EditText best = null;
EditText better = null;
EditText good = null;
EditText poor = null;
TextView name = null;
int j=0;
for(j = 1; j<=count; j++){
Random rnd = new Random();
int color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
tableRow2 = new TableRow (getApplicationContext());
excellent = new EditText (getApplicationContext());
best = new EditText (getApplicationContext());
better = new EditText (getApplicationContext());
good = new EditText (getApplicationContext());
poor = new EditText (getApplicationContext());
name = new TextView (getApplicationContext());
//i want to retrive the value of this --->//excellent.setBackgroundColor(color);
best.setBackgroundColor(color);
better.setBackgroundColor(color);
good.setBackgroundColor(color);
poor.setBackgroundColor(color);
name.setText("Q#"+Integer.toString(j));
tableRow2.addView(name);
tableRow2.addView(excellent);
tableRow2.addView(best);
tableRow2.addView(better);
tableRow2.addView(good);
tableRow2.addView(poor);
table.addView(tableRow2);
}
final StringBuilder output = new StringBuilder();
final String[] a = excellent.getText().toString().split(",");
output.append(a+",");
TableRow tableRow1 = new TableRow (getApplicationContext());
Button get = new Button(getApplicationContext());
tableRow1.addView(get);
get.setText("Get!");
get.setTextSize(8);
//******************************************************************************//
// GET! //
//******************************************************************************//
get.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
EditText x = new EditText (getApplicationContext());
x.setText(output);
table.addView(x);
}
});
//******************************************************************************//
// END OF GET! //
//******************************************************************************//
The problem is that this
output.append(a+",");
Prints a representation of the object a, not the strings that a contains. Try somthing like this:
for(String s : a){
output.append(s);
}
There are multiple things going wrong here.
you split on "," but that is not what you want. you want the excellent.getText().toString(); of the EditTexts
You append the text it is just after creation. It will be empty string at that time.
What you want to do
Make an ArrayList of EditText
Put all excellent EditTexts in it.
In the onClick go through this list and append all the getText().toString() of these EditTexts
I won't give you an exact implementation. You should be able to figure that out on your own.
Your split function should be followed by a FOR loop. Split gives you a array.
So you need to iterate the array and append inside the lopp. Example
String str = "one-two-three";
for (String val: str.split("-", 2)){
// Append your output with val here
}
Cheers

get inputs in an edittext dynamically and pass that to next screen

This is my code..I have created a dynamic screen where i am generating one textview and edittext under a for loop.but after button click it is only getting the last input.I need to get all the inputs of editText and have to pass them to new screen..guyss..plzz help me out.here is my code..
runOnUiThread(new Runnable() {
public void run() {
final LinearLayout findViewById = (LinearLayout) findViewById(R.id.dynamicInputs);
// TextView textView = (TextView) findViewById(R.id.name);
TextView textView = new TextView(Activity_UserInput.this);
textView.setText(map.get(KEY_NAME) + " :");
textView.setTextColor(Color.BLACK);
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 17);
findViewById.addView(textView);
final EditText editText = new EditText(
Activity_UserInput.this);
editText.setText("");
editText.setFocusableInTouchMode(true);
editText.requestFocus();
findViewById.addView(editText);
final ArrayList<EditText> allEds = new ArrayList<EditText>();
allEds.add(editText);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// it = new Intent(Activity_UserInput.this,
// Submition_Activity.class);
// it.putExtra("input", arryList);
// System.out.println("get the input"
// + arryList);
String[] string = new String[allEds.size()];
for (int i = 0; i < string.length; i++) {
string[i] = allEds.get(i).getText().toString();
}
}
});
}
});
Here, in your code arraylist of EditText created every time.So initialize allEds at startup
final ArrayList<EditText> allEds = new ArrayList<EditText>();
and then add EditText to arraylist
final EditText editText = new EditText( Activity_UserInput.this);
editText.setText("");
editText.setFocusableInTouchMode(true);
editText.requestFocus();
findViewById.addView(editText);
allEds.add(editText);
You are creating a new allEds everytime you put final ArrayList<EditText> allEds = new ArrayList<EditText>(); So the allEds will contain just one element.
Try creating it outside the for loop.
You have created only one edittext and also you have added only one edittext to the allEds arraylist. You have also declared the size of the string[] to be the size of the allEds arraylist. So, the length of string[] will be 1 only and the for loop will execute only once. so that, you will get only one input.
If you want to create multiple edittext create them inside a for loop. Or if you are already creating them under a for loop try to declare the following line outside the loop instead of declaring inside.
{ArrayList<EditText> allEds = new ArrayList<EditText>();}
My Layout :
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/mTopLayout" ></LinearLayout>
Within my activity
LinearLayout mLayout = (LinearLayout) findViewById(R.id.mTopLayout);
ArrayList<EditText> mCheck = new ArrayList<EditText>();
EditText mEdit = new EditText(MainActivity.this);
mEdit.setText("");
mEdit.setFocusableInTouchMode(true);
mEdit.requestFocus();
mLayout.addView(mEdit);
mCheck.add(mEdit);
EditText mEdit1 = new EditText(MainActivity.this);
mEdit1.setText("");
mEdit1.setFocusableInTouchMode(true);
mEdit1.requestFocus();
mLayout.addView(mEdit1);
mCheck.add(mEdit1);
EditText mEdit2 = new EditText(MainActivity.this);
mEdit2.setText("");
mEdit2.setFocusableInTouchMode(true);
mEdit2.requestFocus();
mLayout.addView(mEdit2);
mCheck.add(mEdit2); ......
Within onClick method
String[] st = new String[mCheck.size()];
for(int i=0;i<st.length;i++){
st[i] = mCheck.get(i).getText().toString();
}
Log.i("Recived String", st.length+"");
for(int j=0;j<st.length;j++){
Log.i(" String", st[j]+""+j); }
It works fine for me....Please review your layout and let me know.... thanks...

Get Text from Multiple EditText with Button in Android

My problem is creating activity to enter an array manually in Android. So I tried to create multiple EditText by these code lines:
public void EnterArray(int n)
{
for(int i=0; i<n; i++){
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
LinearLayout layout = (LinearLayout)findViewById(R.id.layout1);
EditText txt = new EditText(this);
txt.setId(i);
layout.addView(txt, params);
}
}
The remain problem is how can I get the text from them by using the Save Button I create in the layout. Is there any resolve? I hope there's someone could help with it.
Thank you in advance ^^
You can try something like this.
int count =layout.getChildCount();
for(int i=0;i<count;i++)
{
EditText text=(EditText)layout.getChildAt(i);
String value= text.getText().toString()
}

How to get EditText ID

I have added some EditTexts using a loop,
textFieldsLayout = (LinearLayout) findViewById(R.id.LinearLayout2);
for(int i=1; i <= 8; i++){
final EditText ed = new EditText(this);
ed.setText("" + i);
ed.setInputType(2);
ed.setLayoutParams(lparams);
textFieldsLayout.addView(ed);
}
}
After this I want to get the text that the user adds into the EditText fields, but I am stuck on how to do this. How would I get an Id for each of these EditText Fields?
Thanks, Oli
just add them to a collection when you're creating them that you can easily refer to by index or key or loop through or otherwise.
EditText[] etCollection = new EditText[8];
..........
textFieldsLayout = (LinearLayout) findViewById(R.id.LinearLayout2);
for(int i=1; i <= 8; i++){
final EditText ed = new EditText(this);
ed.setText("" + i);
ed.setInputType(2);
ed.setLayoutParams(lparams);
textFieldsLayout.addView(ed);
etCollection[i] = ed; <------ adding them to the collection
}
}
You'll have to do ed.setId( id ); on each one your create, and id should be unique for each one you want to find. When you go to find one do
EditText ed = (EditText)findViewById( id );
You'll have to keep track of your ids somewhere.
Alternatively you can just keep a list of the EditTexts that you create and run through the list to get the text of each one.

Categories

Resources