Creation of EditText Dynamically and get their text from each of EditText - android

I have created EditText views Dynamically now I want to get their texts into one list or array, How can I do that?

You have to get the reference for your edittexts while you are creating them dynalically like this..
EditText ed;
List<EditText> allEds = new ArrayList<EditText>();
for (int i = 0; i < count; i++) {
//count is number of edittext fields
ed = new EditText(MyActivity.this);
allEds.add(ed);
linear.addView(ed);
}
now allEds will have the references to your edittexts.. and you can get the text like this..
String [] items=new String[allEds.size()]
for(int i=0; i < allEds.size(); i++){
items[i]=allEds.get(i).getText().toString();
}

You can create two List's one for storing dynamically created EditText's and one for storing their text's.
Then iterate through the EditText list using a for-each loop and get the text of each EditText and add it to List of text's.
List<EditText> myList = new ArrayList<EditText>();
List<String> etText = new ArrayList<String>();
EditText myEt1 = new EditText(this);
EditText myEt2 = new EditText(this);
EditText myEt3 = new EditText(this);
//so on...
myList.add(myEt1);
myList.add(myEt2);
myList.add(myEt3);
for(EditText et : myList){
String settext = et.getText().toString();
etText.add(settext);
}

if your problem is accessibility to these views:
// assume ll is your container LinearLayout and your EditTexts inserted in it
LinearLayout ll;
int nViews = ll.getChildCount();
for (int i = 0; i < nViews; i++) {
View child = ll.getChildAt(i);
if (child instanceof EditText ) {
EditText edt= (EditText) child;
//...
}
}

Related

Android-SharedPreferences with EditText which created with For Loop

I created EditTexts with "for loop" and I want to save datas from EditText with using SharedPreferences. I can manage this with creating EditTexts via xml but this takes lots of time and efforts. Below you can see codes.With second "for loop" i have been creating 15 EditTexts (numbers will increase) and I want to store datas from those EditTexts for some calculations. How can I use SharedPreferences with "for loop" for those EditTexts?
public class MainActivity extends AppCompatActivity {
private ScrollView sV;
private LinearLayout pnl;
private GridLayout gL;
private TextView tV;
private Button btn;
private EditText eT;
private void init() {
sV = new ScrollView(this);
hsV = new HorizontalScrollView(this);
pnl = new LinearLayout(this);
pnl.setOrientation(LinearLayout.VERTICAL);
gL = new GridLayout(this);
gL.setColumnCount(2);
final String[] title = getResources().getStringArray(R.array.title);
final String[] info = getResources().getStringArray(R.array.info);
for (int j = 0; j < 2; j++) {
tV = new TextView(this);
tV.setText(title[j]);
tV.setTextSize(20);
tV.setTypeface(Typeface.DEFAULT_BOLD);
tV.setTextColor(getResources().getColor(R.color.colorText));
gL.addView(tV);
}
for (int i = 0; i < 15; i++) {
tV = new TextView(this);
tV.setText(info[i]);
tV.setTextSize(20);
tV.setTextColor(getResources().getColor(R.color.colorText));
gL.addView(tV);
eT = new EditText(this);
eT.setTextSize(20);
eT.setInputType(InputType.TYPE_CLASS_NUMBER);
gL.addView(eT);
}
btn = new Button(this);
btn.setText("Save");
gL.addView(btn);
pnl.addView(gL);
sV.addView(pnl);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
init();
setContentView(sV);
}
}
You can add tags on your EditTexts to identify them.
You can then use these tags as keys in your SharedPreferences, something like that :
for (int i = 0; i < 15; i++) {
eT = new EditText(this);
eT.setTextSize(20);
eT.setInputType(InputType.TYPE_CLASS_NUMBER);
// adding a tag to identify the EditText
String tag = "edit" + i;
eT.setTag(tag);
gL.addView(eT);
}
How to store
#Override
public void afterTextChanged(Editable editable) {
// get values
String key = (String)editable.getTag()
String value = editable.getText().toString()
// save in SharedPreferences
sharedPreferencesEditor.putString(key, values)
sharedPreferencesEditor.commit()
}

How to get values from dynamic edittext and radio group?

I have created the dynamic view. That view contains two edittext and one radio group. when I click the add button the view is added to the layout. Now I got a confusion, how to get values from these type of dynamic views. I tried but it doesn't work. when I add the two or more views, the loop does not find the next views values. I want to add that values to ArrayList. This is code:
private void addDynamicViews() {
EditText name = new EditText(this);
EditText mobile = new EditText(this);
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
p.setMargins(10, 10, 5, 5);
name.setLayoutParams(p);
name.setBackgroundResource(R.drawable.edittext_box);
name.setHint("Enter Name");
studentslayout.addView(name);
mobile.setLayoutParams(p);
mobile.setBackgroundResource(R.drawable.edittext_box);
mobile.setHint("Enter Mobile No");
studentslayout.addView(mobile);
/* radioGroup - Radio Group
maleButton,femaleButton - Radio Buttons
studentslayout - Linear Layout */
radioGroup = new RadioGroup(this);
radioGroup.setOrientation(RadioGroup.VERTICAL);
maleButton = new RadioButton(this);
maleButton.setText("Male");
radioGroup.addView(maleButton);
femaleButton = new RadioButton(this);
radioGroup.addView(femaleButton);
femaleButton.setText("Female");
studentslayout.addView(radioGroup);
}
How to take all dynamic edittext and radio group values ?
I tried this code But unfortunately it stopped.
#Override
public void onClick(View v) {
String[] array = new String[studentslayout.getChildCount()];
int count = studentslayout.getChildCount();
for (int i=0; i < studentslayout.getChildCount(); i++){
editText = (EditText)studentslayout.getChildAt(i);
array[i] = editText.getText().toString();
RadioButton radValues = (RadioButton) studentslayout.getChildAt(i);
array[i] = radValues.getText().toString();
}
}
RadioButton radValues = (RadioButton) studentslayout.getChildAt(i);
You have added radioGroup and are expecting radiobutton. Also since you are looping, you should check the type of the view.
You can try something like this:
int childCount = studentslayout.getChildCount();
for (int i = 0; i < childCount; i++) {
View childView = studentslayout.getChildAt(i);
if (childView instanceof EditText) {
EditText editText = (EditText) childView;
String text = editText.getText().toString();
//use text
} else if (childView instanceof RadioGroup) {
RadioGroup radioGroup = (RadioGroup) childView;
int radioCount = radioGroup.getChildCount();
for (int j = 0; j < radioCount; j++) {
RadioButton radioButton = (RadioButton) radioGroup.getChildAt(i);
//use radioButton.
}
}
}

How to add buttons dynamically?

I want to add buttons dynamically. I am adding multiple buttons dynamically but I want to add the buttons in following pattern:
[BUTTON1] [BUTTON2]
[BUTTON3] [BUTTON4]
[BUTTON5] [BUTTON6]
That means I want to add only 2 buttons in a row that is dynamically.
I have tried many options.
one of them is:
LinearLayout ll = (LinearLayout) findViewById(R.id.buttonlayout);
Button[][] buttonArray = new Button[count][count];
TableLayout table = new TableLayout(this);
for (int row = 0; row < count; row++) {
TableRow currentRow = new TableRow(this);
for (int button = 0; button < row; button++) {
Button currentButton = new Button(this);
// you could initialize them here
currentButton.setOnClickListener(this);
// you can store them
buttonArray[row][button] = currentButton;
// and you have to add them to the TableRow
currentRow.addView(currentButton);
}
// a new row has been constructed -> add to table
table.addView(currentRow);
}
and finally takes that new table and add it to your layout. ll.addView(table);
Note: count of button could be random.
How can I do that?
Use vertical LinearLayout in XML. Then programmatically create horizontal LinearLayout and add buttons in horizontal layout. For each line, create and add new horizontal layout.
XML:
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/buttonlayout">
</LinearLayout>
ACTIVITY:
public class dynamicButtons extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.myLayout);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
int numberOfRows = 3;
int numberOfButtonsPerRow = 2;
int buttonIdNumber = 0;
final LinearLayout verticalLayout= LinearLayout)findViewById(R.id.buttonlayout);
for(int i=0;i<numberOfRows;i++){
LinearLayout newLine = new LinearLayout(this);
newLine.setLayoutParams(params);
newLine.setOrientation(LinearLayout.HORIZONTAL);
for(int j=0;j<numberOfButtonsPerRow;j++){
Button button=new Button(this);
// You can set button parameters here:
button.setWidth(20);
button.setId(buttonIdNumber);
button.setLayoutParams(params);
button.setText("Button Name");
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent is = new Intent(getApplicationContext(), someOtherApplication.class);
is.putExtra("buttonVariable", buttonIdNumber);
startActivity(is);
}
});
newLine.addView(button);
buttonIdNumber++;
}
verticalLayout.addView(newLine);
}
}
}
try this code:
TableLayout layout = new TableLayout (this);
layout.setLayoutParams( new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT));
for (int i=0; i<2; i++) { //number of rows
TableRow tr = new TableRow(this);
for (int j=0; j<2; j++) { //number of columns
Button b = new Button (this);
b.setText("Button:"+i+j);
b.setTextSize(10.0f);
b.setOnClickListener(this);
tr.addView(b, 30,30);
}
layout.addView(tr);
}
As your number of button is random u can use:
int total = 20; //random number of buttons
int column = 3; //specify the column number
int row = total / column;
now use the column and row value to dynamically show buttons
Do something like this:
LinearLayout ll_Main = new LinearLayout(getActivity());
LinearLayout ll_Row01 = new LinearLayout(getActivity());
LinearLayout ll_Row02 = new LinearLayout(getActivity());
ll_Main.setOrientation(LinearLayout.VERTICAL);
ll_Row01.setOrientation(LinearLayout.HORIZONTAL);
ll_Row02.setOrientation(LinearLayout.HORIZONTAL);
final Button button01 = new Button(getActivity());
final Button button02 = new Button(getActivity());
final Button button03 = new Button(getActivity());
final Button button04 = new Button(getActivity());
ll_Row01.addView(button01);
ll_Row01.addView(button02);
ll_Row02.addView(button03);
ll_Row02.addView(button04);
ll_Main.addView(ll_Row01);
ll_Main.addView(ll_Row02);
button04.setVisibility(View.INVISIBLE);
button04.setVisibility(View.VISIBLE);
Create a listview/recyclerview with custom item that hold 2 button as you mentioned in your question, than populate that listView with the buttons (inside the adapter if item index % 2 == 0 it will take the left position otherwise the right position).

Set the ArrayList value to the RadioButton in the for loop

I am getting arrayList [5,8,13,18,19] from the server and I would like to create a RadioGroup in the xml file. After selecting the diserable items, I will put the selected items in arrayList and transmit the query to the server after clicking the OK button. How can I create such as programmatically RadioGroup?
I have tried this but I dont know how to loog through to set the RadioButton with the ArrayList value. How can I get that tp work?
private void createRadioButton(final ArrayList<Integer> items) {
final LinearLayout ll = (LinearLayout) findViewById(R.id.lila);
final ArrayList<RadioButton> rb = new ArrayList<RadioButton>();
final RadioGroup rg = new RadioGroup(this); // create the RadioGroup
rg.setOrientation(RadioGroup.HORIZONTAL);// or RadioGroup.VERTICAL
for (int i = 0; i < items.size(); i++) {
items.get(i) = new RadioButton(this);
}
}
Try this
final RadioGroup rg = new RadioGroup(this); // create the RadioGroup
rg.setOrientation(RadioGroup.HORIZONTAL);// or RadioGroup.VERTICAL
for (int i = 0; i < items.size(); i++) {
RadioButton rb = new RadioButton(this);
rb.setText(items.get(i)+"");
rg.addView(rb);
}
With the addView you are adding the dynamically created RadioButton to the RadioGroup.

How to declare n number of EditTexts dynamically in android?

I have an SQLite Database in my application with fieldname,fieldtype columns.
fieldname consists of EditText namimg ...ex: etsearch,etsave,etcancel etc..
fieldtype is EditText.
I add fieldname values dynamically. I need to declare these EditTexts in my java class, as i'am creating my layout using java code.
I created an arraylist and added the fieldname values into it. I am confused how to declare the EditTexts using for loop.
My code :
public class MainActivity extends Activity
{
LinearLayout ll;
LinearLayout llgrower;
Cursor cursor;
ArrayList<String> edittexts;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
ll = new LinearLayout(this);
llgrower= new LinearLayout(this);
ll.addView(llgrower);
this.setContentView(ll);
cursor = DataBase.getdata("query to get fieldname,fieldtype");
try
{
if (cursor.moveToFirst())
{
do
{
edittexts.add(cursor.getString(0));
} while (cursor.moveToNext());
}
}
catch (Exception e) {}
for (int i=0;i<edittexts.size();i++)
{
}
}
}
Here's the basics of how to do it.
LinearLayout layout = (LinearLayout) findViewById(R.id.layout); // this is whatever view you want to add them to
EditText editText;
for(int i=0;i<nunberOfEditTexts;i++){
editText = new EditText(this);
...
// set the properties of the EditText
...
// add to the view
layout.addView(editText);
}
You will probably want to look at LayoutParams to understand how to size and position your EditTexts.
Good luck
private void addEditText(int count) {
int id = 999;
EditText et;
RelativeLayout.LayoutParams params;
for(int i=0; i<count; i++) {
et= new EditText(this);
et.setId(id);
params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
params.setMargins(0, 10, 0, 0);
params.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
if(i > 0) {
params.addRule(RelativeLayout.BELOW, id - i);
}
//ASSIGN VALUE FOR YOUR EDITTEXT FROM db HERE.
layoutSpinnerContainer.addView(spinner, params);
id++;
}
}

Categories

Resources