Android: Add textViews in RelativeLayout Programmatically

11,670

Solution 1

params1.addRule(RelativeLayout.BELOW, tv.getId());

You are setting it to be below itself?
If you want them below eachother do it this way:

RelativeLayout journals = (RelativeLayout);
findViewById(R.id.JournalSearchListView);
LinearLayout lL = new LinearLayout(context);
lL.setOrientation(LinearLayout.VERTICAL);

for (int i=0;i< authorNames.size();i++) {
    TextView tv = new TextView(this);
    tv.setId(i); 
    tv.setText(authorNames.get(i));
    tv.setTextColor(Color.BLACK);
    Integer a = tv.hashCode();
    map.put(a,authorNames.get(i));

    if(i!=0){
        params1.addRule(RelativeLayout.BELOW, i-1);
    }

    tv.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            System.out.println("Clicked "+ map.get(v.hashCode()) ); 
        }
    });

    lL.addView(tv);   

}

journals.addview(lL);

Solution 2

params1.addRule(RelativeLayout.BELOW, tv.getId());
journals.addView(tv, params1);

This basically says "place the textview below itself". Which of course doesn't work, so all the views will take the default position within the RelativeLayout. Use the id of the TextView you added before instead (or the id of the progress bar if you insert the first element).

Solution 3

// Every UI object must have a layout parameter
        int widthContent = RelativeLayout.LayoutParams.WRAP_CONTENT;
        int heightContent = RelativeLayout.LayoutParams.WRAP_CONTENT;

        // Create relative layout
        RelativeLayout.LayoutParams rlParamsName = new RelativeLayout.LayoutParams(widthContent,heightContent);
        rlParamsName.setMargins(10, 0, 0, 0);   

        // Create a linear layout to add new object as vertical
        LinearLayout lL = new LinearLayout(context);
        lL.setOrientation(LinearLayout.VERTICAL);

        for (int i=0;i< 3;i++) {

            // Every time create new object of text view 
            TextView objDonateTextView = new TextView(this);
            objDonateTextView.setLayoutParams(rlParamsName);
            objDonateTextView.setText("Donate");
            objDonateTextView.setId(i);
            objDonateTextView.setTypeface(null, Typeface.BOLD);
            objDonateTextView.setTextColor(0xFF000000);

            lL.addView(objDonateTextView);

        }

        //Add button number two to the activity.
        RelativeLayout rl = (RelativeLayout)findViewById(R.id.row_special);   
        rl.addView(lL);

//vKj

Share:
11,670
Milos Cuculovic
Author by

Milos Cuculovic

Masters degree in Embedded and comunicant systems.

Updated on June 04, 2022

Comments

  • Milos Cuculovic
    Milos Cuculovic almost 2 years

    I have an RelativeLayout and I would like to insert some textViews.

    Here is the code:

     <RelativeLayout
        android:id="@+id/JournalSearchListView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:minHeight="30dp"
        android:layout_below="@+id/JournalsSearchTextView"
        android:orientation="vertical" 
        android:cacheColorHint="#00000000">
    
        <ProgressBar
            android:id="@+id/JournalSearchProgressBar"
            style="?android:attr/progressBarStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true" />
    
    </RelativeLayout>
    

    And how I am doing this programmatically:

    RelativeLayout journals = (RelativeLayout) findViewById(R.id.JournalSearchListView);
    RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);  
    for (int i=0;i< authorNames.size();i++) {
        TextView tv = new TextView(this);
        tv.setId(i); 
        tv.setText(authorNames.get(i));
        tv.setTextColor(Color.BLACK);
        Integer a = tv.hashCode();
        map.put(a,authorNames.get(i));
        params1.addRule(RelativeLayout.BELOW, tv.getId());
        journals.addView(tv, params1);
        tv.setOnClickListener(new OnClickListener() {
             public void onClick(View v) {
                 System.out.println("Clicked "+ map.get(v.hashCode()) );    
             }
        });   
    }
    

    But the problem is that each textView is in the same position as the others.

  • Milos Cuculovic
    Milos Cuculovic almost 12 years
    Thank you Alex, but I am trying the Andreas's method without success.
  • Anders Metnik
    Anders Metnik almost 12 years
    Okay, why do you even want to use relativelayout? If all you want to do is to place items below eachother, use a linearlayout. And my name is Anders, not Andreas :-)
  • Milos Cuculovic
    Milos Cuculovic almost 12 years
    Oh sorry Anders, my mistake. I would like to use a relative layout because I have an progressBar in my layout, which is positioned in the middle of the layout, which is not possible with the linear layout, right?
  • Dheeresh Singh
    Dheeresh Singh almost 12 years
    basicaly uncomment the line lp.addRule(RelativeLayout.RIGHT_OF, <Id>); and provide the <id> because as you know in RelativeLayout one aligns his view in relative to other view so you need to give other view id....
  • Dheeresh Singh
    Dheeresh Singh almost 12 years
  • Dheeresh Singh
    Dheeresh Singh almost 12 years
  • Anders Metnik
    Anders Metnik almost 12 years
    Then in the relative layout add a linearlayout :)
  • Milos Cuculovic
    Milos Cuculovic almost 12 years
    Thank you but this is still not resolving my problem, i am creating textViews dynamicaly and owervriting their names...
  • Milos Cuculovic
    Milos Cuculovic almost 12 years
    Great answer Anders, i dit like this and this solved my problem