Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a variable number of textviews in android

Hopefully this isn't a bad question, however I've searched through S.O. and haven't been able to find the answer.

I am creating an android application that is essentially an alarm clock. I want the main activity to display all alarms that have been created along with some information about the alarms. My question about this is how to create a given number of text views based on how many alarms have been created. For example if a user has created (and not deleted) 5 alarms, how do I have it display 5 textviews rather than just a hard-coded number of text views? Following is my horribly hard-coded prototype to test the functionality (aside from this hangup).

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Launch" >

    <ScrollView
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <LinearLayout android:orientation="vertical" 
                      android:layout_width="fill_parent" 
                      android:layout_height="fill_parent">

            <TextView
                android:orientation = "vertical"
                android:layout_width="fill_parent"
                android:layout_height="75dp"
                android:text="Time"/>
                    <View
                    android:layout_width="fill_parent"
                    android:layout_height="0.2dp"
                    android:id="@+id/separator"
                    android:visibility="visible"
                    android:background="@android:color/darker_gray"/>

            <TextView
                android:orientation = "vertical"
                android:layout_width="fill_parent"
                android:layout_height="75dp"
                android:text="Time"/>
                    <View
                    android:layout_width="fill_parent"
                    android:layout_height="0.2dp"
                    android:id="@+id/separator"
                    android:visibility="visible"
                    android:background="@android:color/darker_gray"/>

            <TextView
                android:orientation = "vertical"
                android:layout_width="fill_parent"
                android:layout_height="75dp"
                android:text="Time"/>
                    <View
                    android:layout_width="fill_parent"
                    android:layout_height="0.2dp"
                    android:id="@+id/separator"
                    android:visibility="visible"
                    android:background="@android:color/darker_gray"/>


        </LinearLayout>

    </ScrollView>


</RelativeLayout>

As you can see, there are just three hard-coded text views rather than a number of text views as created by the user.

Again, I hope this isn't too in depth of a question or one that has an answer posted elsewhere but I searched for it and couldn't find anything.

Thanks in advance!

like image 309
The Dude Avatar asked Jun 18 '13 03:06

The Dude


1 Answers

You can do this programmatically:

int size = numAlarms; // total number of TextViews to add

TextView[] tv = new TextView[size];
TextView temp; 

for (int i = 0; i < size; i++) 
{
    temp = new TextView(this);

    temp.setText("Alarm: " + i); //arbitrary task

    // add the textview to the linearlayout
    myLinearLayout.addView(temp);

    tv[i] = temp;
}
like image 107
Steve P. Avatar answered Oct 07 '22 14:10

Steve P.