Android : Using context to get all TextView - android

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.

Related

How to add views to Fragment B that are not involved in a shared element transition from Fragment A?

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"

How to fix null object that appears all the time on my phone only?

I have a problem with Android Studio. I'm trying to do a pretty simple app but on my phone (Galaxy S8) it always gives me the same error. And I use two computer it does the same at home and at school. When I use the emulator, everything's good.
Try this defensive programming code. This should avoid NPE problem, but the root reason are:
You should refactor your game code with some Design Mode (try MVC at lease). You should not relay on the TextView String Value for game logic decision.
Some UI Tree node maybe has been changed inside the loop.
public boolean checkEndOfGame(LinearLayout cartes, LinearLayout piles){
if(nbCartes == 0){
return true;
}
for (int i=0; i< piles.getChildCount(); i++){
LinearLayout sorte = (LinearLayout)piles.getChildAt(i);
for(int j=0; j< sorte.getChildCount(); j++){
View sorteChild = sorte.getChildAt(j);
if(sorteChild instanceof ConstraintLayout){
ConstraintLayout pile = (ConstraintLayout)sorteChild;
if(0 == pile.getChildCount) {
Log.e("TAG", "pile's child count = 0 ");
continue; //or break ?
}
View pileChild = pile.getChildAt(0);
if(pileChild instanceof TextView) {
TextView textePile = (TextView) pileChild;
int noPile = Integer.valueOf(textePile.getText());
for(int k=0; k< cartes.getChildCount(); k++){
View cartesChild = cartes.getChildAt(i);
if(cartesChild instanceof LinearLayout) {
LinearLayout rangee = (LinearLayout)cartesChild;
for(int l=0; l< rangee.getChildCount(); l++){
View carteChild = rangee.getChildAt(l);
if(carteChild instanceof ConstraintLayout) {
ConstraintLayout carte = (ConstraintLayout)carteChild;
View tv = carte.getChildAt(0);
if(tv instanceof TextView) {
TextView texteCarte = (TextView) tv;
int noCarte = Integer.valueOf(texteCarte.getText());
if(i>0){ //UP
if(noCarte>noPile){
return false;
}
}else{ //DOWN
if(noCarte<noPile){
return false;
}
}
}
}
}
}
}
}
}
}
}
return true;
}

Set Text all EditText at once

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 😎😎

android studio how to set some TextViews id's with for

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 :)

Android Clearing all EditText Fields with Clear Button

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(");

Categories

Resources