0

I have a lot of textviews in my program . I want the numbers inside these textviews to be separated by 3 to 3 . Should I write code for any textview? Is there a way to write code once and use it for the whole program? Thank You .

4 Answers4

1

I am not a Java developer, but I'm thinking you could write a class that inherits from the textview class and override the method that sets the text to separate the numbers. Then you'd just need to replace the class of your textview objects with your new class, which should be quite easy.

lexugax
  • 43
  • 6
1

It dependes on the type of Layout. For example in a simple LinearLayout you can proceed like this :

LinearLayout linearLayout = (LinearLayout) findViewById(R.id.mylayout);
for(int k = 0 ; k < n ; k++){
    TextView tView = new TextView(context);
    tView.setText(k*3)
    linearLayout.addView(tView);
}

Or another way could be iterating on all Views that are TextView setting the text. For example, assuming that mylayout is the parent view :

for(int k=0; k < mylayout.getChildCount(); k++ ){
    if( mylayout.getChildAt(k) instanceof TextView ){
       ((TextView)mylayout.getChildAt(k)).setText(k*3);
    }
}
ollaw
  • 2,086
  • 1
  • 20
  • 33
1

In order to use your own TextView style, you can creat one by extend the TextView class and then use it in the xml activity

See example how to extend: How to make a custom TextView?

In the constractor of your new TextView, you can add TextWacher by addTextChangedListener method to perform your number seperation.

See example how: android on Text Change Listener

Tamir Adler
  • 341
  • 2
  • 14
1

You have to use DecimalFormat and the grouping pattern

Bruno
  • 3,872
  • 4
  • 20
  • 37