Pouya Heydari
Pouya Heydari

Reputation: 2616

Setting Some TextView's Text

In my Project , I have 80 TextViews. I should set their text from 1 to 80 once project runs , and they dont need to be changed in future. Except TxtViews , I have some other things in my Layout, the TextViews are under ImagesViews. actually I have 80 imagesViews and under them are 80 TextViews. I want to set text of textViews from 1 to 80 dynamically. I know I can do it in my layout.xml , but its really time consuming. is there any way to do that by code? for example with a for cycle or something like that?

Upvotes: 2

Views: 94

Answers (5)

Mauker
Mauker

Reputation: 11487

If your TextViews are declared on the xml, wrap them on another view so you can reference it on the java code later, then simply use a for.

Something like:

View view = findViewById(R.id.your_wrapper);

for(int i=0; i<((ViewGroup)view).getChildCount(); i++) {
    View nChild = ((ViewGroup)view).getChildAt(i);
    TextView tv = (TextView) nChild;
    tv.setText(String.valueOf(i + 1));
}

If not, you can simply create them dynamically inside your java code, and append them to a layout like LinearLayout.

Example:

xml

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"    
    android:id="@+id/linear"
/>

Java code

LinearLayout ll = (LinearLayout) findViewById(R.id.linear);

for (int i = 1; i <= 80; i++) {
    TextView tv = new TextView(this); // Assuming you're inside an Activity.
    int count = ll.getChildCount();
    tv.setText(String.valueOf(i));

    ll.addView(tv, count, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
}

EDIT: But truly, you should use RecyclerView or ListView for that if your values are not going to change.

You can read more about RecyclerView here, and on ListView here.


Second edit: From what you're saying on your comments, you REALLY should be using ListView instead of your current design. The solutions above and from the other answers won't work at all for your problem.

Upvotes: 0

Rohit Jagtap
Rohit Jagtap

Reputation: 1670

Checkout the below example,

public class MainActivity extends Activity {

LinearLayout linearLayout ;
ScrollView scrollView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    scrollView = (HorizontalScrollView) findViewById(R.id.scrollViewActivityMain);
}

private void populateTextViews() {

    linearLayout = new LinearLayout(this);
    linearLayout.setOrientation(LinearLayout.HORIZONTAL);

    //add all textViews here
    for(int i=0; i < 80; i++){
        TextView myTextView = new TextView(this);
        myTextView.setText("My TextView "+i);
        myTextView.setGravity(Gravity.CENTER);
        linearLayout.addView(myTextView);
    }

    scrollView.addView(linearLayout);
}
}

Don't forget to put that scrollView in your xml. Let me know if it works for you...

Upvotes: 0

user4571931
user4571931

Reputation:

If you know that 80 Textview fixed then you should take listview for that.

Listview Benefit

  • Memory management automatically
  • Listview manage indexing

Upvotes: 1

Kenneth
Kenneth

Reputation: 4065

Create a ViewGroup suitable for your needs in the layout, for example:

<LinearLayout
    android:id="@+id/linear_layout"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
/>

Then you create you TextView instances programatically, and add them to the LinearLayout, like this:

    LinearLayout layout = (LinearLayout)findViewById(R.id.linear_layout);

    for(int i = 0; i < 80; i++) {
        TextView textView = new TextView(getContext());
        textView.setText("text" + i);
        layout.addView(textView);
    }

Optionally, you can add tags or whatever to locate them again. Alternatively just iterate over the layouts subviews.

Upvotes: 1

Tragalunas
Tragalunas

Reputation: 311

If they share the same layout, except for the text, and could be displayed as a list, you could use an ArrayAdapter and pass the values from code.

http://www.mkyong.com/android/android-listview-example/

Upvotes: 0

Related Questions