I need to compare strings from title of fragment dialogue with R.id.string from resources. It looks like smth like this (user Robotium).
assertTrue(doesAlertTitleExist(getSolo().getString(R.string.error_dialog_title_text)));
But I can't get text from title of dialogue window fragment created with the help of ActionBar Sherlock. This code finds only views from screen over which this dialogue appears, but not views from dialogue.
You can find screenshot here: http://imgur.com/d1ZjL
public boolean doesAlertTitleExist(String extectedTitle) {
List<TextView> views = getDialogTextViews((ViewGroup) getSolo().getView(android.R.id.content));
assertNotNull(views);
for(TextView view : views){
if(view.getText().toString().equals(extectedTitle)){
return true;
}
}
return false;
}
private List<TextView> getDialogTextViews(ViewGroup view) {
List<TextView> out = new LinkedList<TextView>();
int count = view.getChildCount();
for(int i=0; i<count; i++){
View child = view.getChildAt(i);
if(child instanceof ViewGroup){
out.addAll(getDialogTextViews((ViewGroup) child));
}else if(child instanceof TextView){
out.add((TextView) child);
}
}
return out;
}
Related
So I have a recycler view item that looks like this:
Transition View Start
And I want to end up with this:
Transition View End
The problem I keep getting is an index out-of-bounds exception. So the start has 6 views that transition and all have the appropriate transition name based on a unique id.
The end view has all 6 but 2 more, the small water and thermometer images. Those two have no transition names. Yet they keep getting added to a list that stores the transition views. The following code is in DefaultSpecialEffectsController.java - line 701
void captureTransitioningViews(ArrayList<View> transitioningViews, View view) {
if (view instanceof ViewGroup) {
if (!transitioningViews.contains(view)
&& ViewCompat.getTransitionName(view) != null) {
transitioningViews.add(view);
}
ViewGroup viewGroup = (ViewGroup) view;
int count = viewGroup.getChildCount();
for (int i = 0; i < count; i++) {
View child = viewGroup.getChildAt(i);
if (child.getVisibility() == View.VISIBLE) {
captureTransitioningViews(transitioningViews, child);
}
}
} else {
if (!transitioningViews.contains(view)) {
transitioningViews.add(view);
}
}
}
And the index out of bounds occurs here FragmentTranstionImpl.java - line 176
void setNameOverridesReordered(final View sceneRoot,
final ArrayList<View> sharedElementsOut, final ArrayList<View> sharedElementsIn,
final ArrayList<String> inNames, final Map<String, String> nameOverrides) {
final int numSharedElements = sharedElementsIn.size();
final ArrayList<String> outNames = new ArrayList<>();
for (int i = 0; i < numSharedElements; i++) {
final View view = sharedElementsOut.get(i);
final String name = ViewCompat.getTransitionName(view);
outNames.add(name);
if (name == null) {
continue;
}
ViewCompat.setTransitionName(view, null);
final String inName = nameOverrides.get(name);
for (int j = 0; j < numSharedElements; j++) {
if (inName.equals(inNames.get(j))) {
ViewCompat.setTransitionName(sharedElementsIn.get(j), name);
break;
}
}
}
Is it possible to add the two small icons and in general any view, without them being in the starting transition view in recycler view in Fragment A?
This is a bug in Fragments , specifically fixed in Fragment 1.3.5. You'll need to upgrade to that version.
implementation "androidx.fragment:fragment:1.3.5"
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 :)
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.
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);
}
}
}
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(");