Get values of all child EditBoxes - android

Is this code completely correct or not?
myRLayout = (RelativeLayout) findViewById( R.id.rel_layout );
for(int i = 0; i < myRLayout.getChildCount(); i++) {
View v = myRLayout.getChildAt(i);
if(v instanceof EditText) {
TextView res = (TextView) findViewById(R.id.result_text);
String str1 = ((EditText) findViewById(R.id.edittext_1)).getText().toString();
String str2 = ((EditText) findViewById(R.id.edittext_2)).getText().toString();
res.setText(str1+str2);
}
}
Why I ask? For each iteration only one EditText gets in action or all together at once?

use v instance of Child to get value from all EditText and use TextView.append for showing values in TextView. try it as:
TextView res = (TextView) findViewById(R.id.result_text);
res.setText(""); //<< clear textview here
for(int i = 0; i < myRLayout.getChildCount(); i++) {
View v = myRLayout.getChildAt(i);
if(v instanceof EditText) {
String str1 = v.getText().toString(); //<< get text from ith EditText
res.append(str1); // append value to result_text TextView
}
}

Related

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.
}
}
}

i want the value of inflated edittext with other layout using for loop in java

I need the value of inflated editText, I am inflating the design layout which is the child layout and MainActivity is a parent layout.
below is the MainActivity class
public class MainActivity extends AppCompatActivity {
View array[];
String text;
LinearLayout container;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText edittext1 = (EditText) findViewById(R.id.email);
for (int i = 0; i < 4; i++) {
LinearLayout myLayout = (LinearLayout) findViewById(R.id.linearlayout);
View v = getLayoutInflater().inflate(R.layout.design, myLayout, false);
myLayout.addView(v);
int id = View.generateViewId();
v.setId(id);
EditText edittext = (EditText) v.findViewById(R.id.edittext);
text = edittext.getText().toString();
}
edittext1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplication(), "" + text, Toast.LENGTH_SHORT).show();
}
});
}
}
Here is total code of store object and retrieval,
read all comments please,
public class MainActivity extends AppCompatActivity {
View array[];
String text;
LinearLayout container;
// list of edittext
List<EditText> edtlist = new ArrayList()<EditText>;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText edittext1 = (EditText) findViewById(R.id.email);
for (int i = 0; i < 4; i++) {
LinearLayout myLayout = (LinearLayout) findViewById(R.id.linearlayout);
View v = getLayoutInflater().inflate(R.layout.design, myLayout, false);
myLayout.addView(v);
int id = View.generateViewId();
v.setId(id);
EditText edittext = (EditText) v.findViewById(R.id.edittext);
// store object in list
edtlist.add(edittext);
text = edittext.getText().toString();
}
edittext1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplication(), "" + text, Toast.LENGTH_SHORT).show();
}
});
// call this whenever you want in any event
// here 0 is first edittext
// you can get all text value using for loop
// list.get(0).getText().toString();
}
}
You are looking for something like ,I'm not sure about it maybe it works maybe it wont.! it just a rough concept based code havnt Implemented yet.!
List<EditText> edtxtList;
List<String> edtxtStrList;
for (int i = 0; i < 4; i++) {
LinearLayout myLayout = (LinearLayout) findViewById(R.id.linearlayout);
View v = getLayoutInflater().inflate(R.layout.design, myLayout, false);
myLayout.addView(v);
int id = View.generateViewId();
v.setId(id);
EditText edittext = (EditText) v.findViewById(R.id.edittext);
text = edittext.getText().toString();
edtxtList.add(edittext);// try something like this.!
}
Then use Foreach loop on EditText;
for (EditText edText : edtxtList) {
if(!edText.getText().trim()=null){
edtxtStrList.add(edText.getText().trim());
}
}
I havnt tried It yet but maybe this ruff concept may helps you.!
Maybe this Method helps You.!
ArrayList<EditText> myEditTextList = new ArrayList<EditText>();
for( int i = 0; i < myLayout.getChildCount(); i++ )
if( myLayout.getChildAt( i ) instanceof EditText )
myEditTextList.add( (EditText) myLayout.getChildAt( i ) );
In Above code mLayout is your layout.!
<LinearLayout android:id="#+id/myLinearLayout" >
//Here is where your EditTexts would be declared
</LinearLayout>

Android getting value empty from custom list view Edittext

Am getting value here for Checkbox, and textview properly. But, I could not get the EditText Values. am getting empty only.
my sample code:
View view = null ;
for(int i = 0; i < list.getCount(); i++ ) {
view = list.getAdapter().getView(i, null, null) ;
TextView textViewNickName = (TextView) view.findViewById(R.id.textview);
String textviewValue = textViewNickName.getText().toString().trim() ;
Log.v("ppi", "textViewNickName::"+textviewValue);
EditText edittext = (EditText) view.findViewById(R.id.edittext_amount) ;
String edittextValue = edittext.getText().toString().trim() ;
Log.v("ppi", "Amount edittextValue::"+edittextValue);
CheckBox checkBoxOne = (CheckBox) view.findViewById(R.id.checkBox1) ;
Log.v("ppi", "checkBoxOne::"+checkBoxOne.isChecked());
}
I referred this link also :
Iterate through ListView and get EditText-Field values
Thanks Advance
You have mistake to get value from EditText, any event time you can get value initialization time you can't get text from EditText
EditText edittext = (EditText) view.findViewById(R.id.edittext_amount) ;
String edittextValue = edittext.getText().toString().trim() ;
Log.v("ppi", "Amount edittextValue::"+edittextValue);
see above code that in your mistake if you have no enter any string in EditText then how can you retrieve initialization time.
you have to make change in you code as like blow.
EditText edittext = (EditText) view.findViewById(R.id.edittext_amount) ;
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String edittextValue = edittext.getText().toString().trim() ;
Log.v("ppi", "Amount edittextValue::"+edittextValue);
}
});

defining a view based upon a int

is there anyway to change which view you are editing based upon the value of a int?
Without using a if statement?
for example:
int counter = 1;
public void attach(){
final SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
TextView text1 = (TextView) findViewById(R.id.text1)
TextView text2 = (TextView) findViewById(R.id.text2)
TextView text3 = (TextView) findViewById(R.id.text3)
TextView text4 = (TextView) findViewById(R.id.text4)
if (counter < 5){
if (sp.getInt("test" + counter , 0) == counter){
text + counter .setText("test" + counter);
}else{counter = counter + 1; attach();}}
}
Simplest way is to define an array of possible view IDs.
int[] viewIds = new int[] {
R.id.text1,
R.id.text2,
R.id.text3,
R.id.text4
}
//some code
int i = /*whatever*/;
TextView tv = (TextView)findViewById(viewIds[i]);
tv.setText("test"+i);

Save data into an email:

Save data from edit texts, text views, spinner selections, and checkboxes from multiple activities into an email:
I am already using:
Intent EmailSend = new Intent(android.content.Intent.ACTION_SEND);
EmailSend.setType("plain/text");
EmailSend.putExtra(android.content.Intent.EXTRA_TEXT,
"Pretext"+edittext.getText().toString());
the put string is not working for items not listed in the .java
When I use the last line in that i get error saying -edittext cannot resolved-
and how to get data from checkbox & spinner
I will have 80 or so items to compile to this email over 8 activities
I wrote a snippet to automate it a little:
ViewGroup root = (ViewGroup) ((ViewGroup) findViewById(android.R.id.content)).getChildAt(0);
StringBuilder str = new StringBuilder();
public void extractText(ViewGroup root, StringBuilder str){
int count = root.getChildCount();
for(int i = 0; i < count; i++) {
View v = root.getChildAt(i);
if(v instanceof Spinner) {
str.append(i).append(" Spinner: ").append(((Spinner) v).getSelectedItem());
} else if(v instanceof TextView) {
str.append(i).append(" TextView: ").append(((TextView) v).getText());
} else if(v instanceof CheckBox) {
str.append(i).append(" Checkbox: ").append(((CheckBox) v).isChecked());
}else if(v instanceof ViewGroup){
extractText((ViewGroup)v, str);
}
}
}

Categories

Resources