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);
}
}
}
Related
I have a fragment with about 30 EditText inside it. I want to know is there any way to grab all EditTexts and clear the text inside it without doing it one by one.
thanks.
This should work if all your edittexts are within the same layout, for example a relativelayout
ViewGroup group = (ViewGroup)findViewById(R.id.your_group);
for (int i = 0, count = group.getChildCount(); i < count; ++i) {
View view = group.getChildAt(i);
if (view instanceof EditText) {
((EditText)view).setText("");
}
}
Try something like this :
ArrayList<EditText> editTextsList = new ArrayList<EditText>();
for( int i = 0; i < myLayout.getChildCount(); i++ )
if( myLayout.getChildAt( i ) instanceof EditText )
editTextsList.add( (EditText) myLayout.getChildAt( i ) );
The iterate on editTextsList and do your actions on each of EditText on the list
I hope this helps
Yes, you can do with the following method.
public void clearStudentInfo(ViewGroup textViewsGroup) {
for (int i = 0, count = textViewsGroup.getChildCount(); i < count; ++i) {
View view = textViewsGroup.getChildAt(i);
// Check and confirm, is it is the TextView or not?
if (textViewsGroup instanceof EditText) {
((EditText)textViewsGroup).setText("");
}
}
}
You can call the method when you click on clear button as given below.
...
Button btnClear = findViewById(R.id.btnClr);
...
btnClear.setOnClickListener(new View.OnClickListener) {
#Override
void onClick(...) {
clearStudentInfo((ViewGroup) findViewById(R.id.student_info));
}
Thanks 😎😎
i want to set lots of Edit Texts id's and i don't know how to get those id's.
EditText[] texts;
for(int i=0;i++;i<15){
tests[i]=(EditText)findViewById(R.id. )
}
my Edit Text id's:
et1,et2,et3,...
first create a empty list of EditTexts
then use this code and send them rootView of Your Layout to find all EditTexts :
private List<EditText> editTexts; //global Scop
private void findAllEditTexts(ViewGroup group) {
for (int i = 0, count = group.getChildCount(); i < count; ++i) {
View view = group.getChildAt(i);
if (view instanceof EditText) {
if (editTexts == null)
editTexts = new ArrayList<EditText>();
editTexts.add((EditText) view);
} else if (view instanceof ViewGroup && (((ViewGroup) view).getChildCount() > 0)) {
findAllEditTexts((ViewGroup) view);
}
}
}
now in editTexts List you have all EditTexts :)
In my program I, have a TableLayout with one TableRow where I added more TableRows with 4 TextViews in it dynamically on a Button Click. Now I want to save and restore this table if I change the orientation of my phone.
I have following code which should do this:
#Override
protected void onSaveInstanceState(Bundle outState) {
rowcount = tableLayout.getChildCount();
for (int i = 0; i < rowcount; i++) {
View row = tableLayout.getChildAt(i);
if (row instanceof TableRow) {
View rowchild1 = tableRow.getChildAt(0);
if (rowchild1 instanceof TextView) {
savewarnum[i] = ((TextView) rowchild1).getText().toString();
}
View rowchild2 = tableRow.getChildAt(1);
if (rowchild2 instanceof TextView) {
savewarname[i] = ((TextView) rowchild2).getText().toString();
}
View rowchild3 = tableRow.getChildAt(2);
if (rowchild3 instanceof TextView) {
savewarmeng[i] = ((TextView) rowchild3).getText().toString();
}
View rowchild4 = tableRow.getChildAt(3);
if (rowchild4 instanceof TextView) {
savewarpreis[i] = ((TextView) rowchild4).getText().toString();
}
}
}
outState.putStringArray("Nummer", savewarnum);
outState.putStringArray("Name", savewarname);
outState.putStringArray("Menge", savewarmeng);
outState.putStringArray("Preis", savewarpreis);
outState.putInt("Rows", rowcount);
super.onSaveInstanceState(outState);
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
savewarnum = savedInstanceState.getStringArray("Nummer");
savewarname = savedInstanceState.getStringArray("Name");
savewarmeng = savedInstanceState.getStringArray("Menge");
savewarpreis = savedInstanceState.getStringArray("Preis");
rowcount = savedInstanceState.getInt("Rows");
tableLayout = (TableLayout) findViewById(R.id.kaufwaren);
for(int i=0; i<=rowcount; i++) {
View tablechild = tableLayout.getChildAt(i);
if (tablechild instanceof TableRow) {
View rowchild1 = tableRow.getChildAt(0);
if (rowchild1 instanceof TextView) {
((TextView) rowchild1).setText(savewarnum[i]);
}
View rowchild2 = tableRow.getChildAt(1);
if (rowchild2 instanceof TextView) {
((TextView) rowchild2).setText(savewarname[i]);
}
View rowchild3 = tableRow.getChildAt(2);
if (rowchild3 instanceof TextView) {
((TextView) rowchild3).setText(savewarmeng[i]);
}
View rowchild4 = tableRow.getChildAt(3);
if (rowchild4 instanceof TextView) {
((TextView) rowchild4).setText(savewarpreis[i]);
}
}
createtablerow();
}
super.onRestoreInstanceState(savedInstanceState);
}
In my onSavedInstanceState method, I'm going through my table where I save every Textview in a StringArray. In onRestoreInstanceState I'm trying to recreate the table.
Now I have two Problems. My First Problem is, that my Program doesn't go through the table in onSavedInstanceState. It just adds the TextViews of the last row to my string array instead of the TextViews from all Rows.
For example i have two rows:
Row 1: 'abc' '123' 'def' '456'
Row 2: 'cba' '321' 'fed' '654'
Now ABC and CBA should be in my first string array, but there is two times CBA because it's the last row.
My second Problem is in onRestoreInstanceState. As soon as I reach 'View rowchild1 = tableRow.getChildAt(0);' my program crashes.
Does anyone know a solution?
You should initialize tableRow variable before usage in onSaveInstanceState
#Override
protected void onSaveInstanceState(Bundle outState) {
...
View row = tableLayout.getChildAt(i);
if (row instanceof TableRow) {
TableRow tableRow = (TableRow) row;
View rowchild1 = tableRow.getChildAt(0);
...
}
}
Same might be an issue in onRestoreInstanceState.
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
...
if (tablechild instanceof TableRow) {
TableRow tableRow = (TableRow) tablechild;
View rowchild1 = tableRow.getChildAt(0);
...
}
}
Is it possible to get all the TextView in an activity and change their values without knowing any ID? May be something like UI automtor tool that return UI hierarchy but at Java programming level. I tried to Google this but couldn't find any solution.
Edit
What I am trying to achieve here is, I have an external library/SDK which modifies all textView values. So I am planning to initialise it on top of each activity and let the SDK do the work of modifying all the TextViews value.
You can use this code to find all TextViews in your layout, just pass the parent view and it will do the rest:
public static void findViews(View v) {
try {
if (v instanceof ViewGroup) {
ViewGroup vg = (ViewGroup) v;
for (int i = 0; i < vg.getChildCount(); i++) {
View child = vg.getChildAt(i);
// recursively call this method
findViews(child);
}
} else if (v instanceof TextView) {
//do whatever you want ...
}
} catch (Exception e) {
e.printStackTrace();
}
}
try this
First initialize the parent control and use this
ArrayList<TextView> list = new ArrayList<TextView>();
for( int i = 0; i < layout.getChildCount(); i++ ){
if( layout.getChildAt( i ) instanceof TextView )
{
// change the values of text view here
((TextView)layout.getChildAt( i )).setText("your text here");
}
}
If you have activity layout then try as follows
ArrayList<EditText> myEditTextList = new ArrayList<EditText>();
for( int i = 0; i < activityLayout.getChildCount(); i++ ){
if( activityLayout.getChildAt( i ) instanceof EditText )
myEditTextList.add( (EditText) activityLayout.getChildAt( i ) );
}
and getting their values
for(int i=0;i < myEditTextList.size();i++){
System.out.println(myEditTextList.get(i).getText().toString());
}
Hope this will helps you.
How do I clear all the EditText fields in a layout with a Clear Button. I have a registration Activity that has about 10 different EditTexts. I know I could go and grab a reference to each specifically and then set.Text(""); But I am looking for a more dynamic elegant way. Possibly grab the Layout and loop through all the items in there looking for EditText types and then setting those to "". Not sure how to do that though and tried searching on the web for it but no luck. Any sample code?
The answer by #Pixie is great but I would like to make it much better.
This method works fine only if all the EditText are in a single(one) layout but when there are bunch of nested layouts this code doesn't deal with them.
After scratching my head a while I've made following solution:
private void clearForm(ViewGroup group) {
for (int i = 0, count = group.getChildCount(); i < count; ++i) {
View view = group.getChildAt(i);
if (view instanceof EditText) {
((EditText)view).setText("");
}
if(view instanceof ViewGroup && (((ViewGroup)view).getChildCount() > 0))
clearForm((ViewGroup)view);
}
}
To use this method just call this in following fashion:
clearForm((ViewGroup) findViewById(R.id.sign_up));
Where you can replace your R.id.sign_up with the id of root layout of your XML file.
I hope this would help many people as like me.
:)
You can iterate through all children in a view group and clear all the EditText fields:
ViewGroup group = (ViewGroup)findViewById(R.id.your_group);
for (int i = 0, count = group.getChildCount(); i < count; ++i) {
View view = group.getChildAt(i);
if (view instanceof EditText) {
((EditText)view).setText("");
}
}
Use editText.getText().clear();
after onclick of any action do below step
((EditText) findViewById(R.id.yoursXmlId)).setText("");
or Clear all EditText fields by iterating all childrens:
ViewGroup group = (ViewGroup)findViewById(R.id.your_group);
for (int i = 0, count = group.getChildCount(); i < count; ++i) {
View view = group.getChildAt(i);
if (view instanceof EditText) {
((EditText)view).setText("");
}
}
or use simple below step :
editText.getText().clear();
Might be helpful.
It's very simple.Type this in your function of button-
finish();
startActivity(this,YourCurrentActivity.class);
As Simple as that.
Welcome.
In my case I've done this.
public static void resetForm(ViewGroup group) {
for (int i = 0, count = group.getChildCount(); i < count; ++i) {
View view = group.getChildAt(i);
if (view instanceof EditText) {
((EditText) view).getText().clear();
}
if (view instanceof RadioGroup) {
((RadioButton)((RadioGroup) view).getChildAt(0)).setChecked(true);
}
if (view instanceof Spinner) {
((Spinner) view).setSelection(0);
}
if (view instanceof ViewGroup && (((ViewGroup) view).getChildCount() > 0))
resetForm((ViewGroup) view);
}
}
I used this for nested LinearLayout to clear EditTexts and RadioButtons
//method clear
private void clear(ViewGroup group)
{
for(int i=0,count=group.getChildCount();i
if(view instanceof LinearLayout)
{
clear((ViewGroup) view);
}
else if(view instanceof EditText)
{
((EditText) view).getText().clear();
}
if (view instanceof RadioButton)
{
((RadioButton) view).setChecked(false);
}
}//end for
}//end method clear
You can always do this...it works for me:
mClearButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mEditName.getText().clear();
mEditSummary.getText().clear();
mEditPrice.getText().clear();
mEditQuantitiy.getText().clear();
}
});
this way you have one fat button that clears all the fields for you once
I created a reset button and write a reset onclick method as follow:
public void reset(View View){
Intent intent = new Intent(MainActivity.this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
It works for me and also it works without addflags part. But I would like to know whether it is a good approach or not.
// Update answer
private void clearEditTextGroup(ViewGroup group){
for(int i=0 ; i< group.getChildCount(); i++){
View view = group.getChildAt(i);
if(view instanceof EditText){
// use one of clear code
}
if(view instanceof ViewGroup && (((ViewGroup)view).getChildCount() > 0))
clearEditTextGroup((ViewGroup)view);
}
}
use one of this code to clear your edittext
edittext.getText().clear();
or
edittext.setText(null);
or
edittext.setText("");
use
editText.getText().clear();
or setText as Empty using this below code
editText.setText(");