0

Problem


I've been trying to place a couple of TextViews underneath eachother, but I can't seem to get it working. They always overlap eachother in stead of getting placed beneath eachother.

What have I tried


I've tried messing with gravity (which doesn't work with a RelativeLayout), and all sorts of layout parameters. I've concluded that the best solution for me would be to use the RelativeLayout.BELOW parameter. The only problem is I'm trying to find the id of the previous TextView.

I assign the id's for the TextViews using the iterator. It seems I can't use the iterator - 1 to count as an id (even though other answers on SO suggest this works). I've even tried assigning the current TextView to a "previous_tv" variable to use in the next iteration.

I tried finding the TextView I just placed using this.findViewByID and the correct iterator value for the id, this also did not work.

Code


I'm trying to figure out what I should place as an id for my parameter rule. This is the code I'm reffering to -edit, placed full code as per request-:
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE); //Don't show application name on the top of the screen (valuable space)
    setContentView(R.layout.activity_task_play); //Set which layout file to use for this activity

    //Get the Task that was sent by TaskActivity
    this.task = (Task) this.getIntent().getSerializableExtra("TASK_OBJECT");
    //Get the Sums for this Task
    this.sums = this.task.getSums();

    //GridView setup
    this.gridview = (GridView) this.findViewById(R.id.gridView_task_play);

    ArrayAdapter<String> adapter = this.task.getArrayAdapterForGridView(this);

    this.gridview.setAdapter(adapter);

    //TextView setup
    RelativeLayout layout = (RelativeLayout) this.findViewById(R.id.activity_task_play_relative);

    for(int i = 0; i < this.sums.length; i++)
    {
        //Layout parameters
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        params.addRule(RelativeLayout.CENTER_HORIZONTAL);

        //This variable counts how many times the placeholder text is skipped and a operator (+-/ etc.) is placed in the sum_text.
        //This is usefull for placing the correct placeholder for each number in the sum (a, b, c etc.)
        int times_skipped = 0; 

        TextView tv = new TextView(this);

        String sum_text = "";

        for(int j = 0; j < this.sums[i].getVariables().length; j++)
        {               
            if(this.isParsable(this.sums[i].getVariables()[j]))
            {
                sum_text += TaskPlayActivity.PLACEHOLDERS[(j - times_skipped)] + " ";
            }
            else
            {
                sum_text += this.sums[i].getVariables()[j] + " ";
                times_skipped++;
            }
        }

        if(i > 0) 
        {
            params.addRule(RelativeLayout.BELOW, i - 1);
        }
        tv.setId(i);
        tv.setText(sum_text + "= " + this.sums[i].getAnswer());
        tv.setTextColor(TaskPlayActivity.COLOURS[i]);
        tv.setTextSize(25);
        tv.setLayoutParams(params);
        layout.addView(tv);
    }
}



XML

Added the XML. I'm not very good in layouts so it could very well be that the fault resides here.
<RelativeLayout 
    android:id="@+id/activity_task_play_relative"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <RelativeLayout 
        android:layout_width="wrap_content"
        android:layout_height="220dip"
        android:layout_alignParentBottom="true">

        <GridView
            android:id="@+id/gridView_task_play"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:background="#EFEFEF"
            android:horizontalSpacing="1dp"
            android:numColumns="3"
            android:paddingTop="1dp"
            android:verticalSpacing="1dp" >

        </GridView>

    </RelativeLayout>

</RelativeLayout>

Any other suggestions are welcome as well, though I would prefer keeping a RelativeLayout. Thanks in advance.

Bono
  • 4,757
  • 6
  • 48
  • 77

2 Answers2

1

The documentation of View#setId says the identifier should be a positive number so you should make sure not to use zero as identifier value.

Also you have to create a new LayoutParams instance for each TextView. As it is all your TextViews share the same LayoutParams object and changes to that one object affect all TextViews.

And you could use View#generateViewId to generate an ID and remember the ID of the last iteration.

devconsole
  • 7,875
  • 1
  • 34
  • 42
  • generateViewId requires me to move to a higher API. I would rather not do this unless it is really necesary. – Bono May 10 '13 at 16:43
  • I think the main problem is your reuse of the LayoutParams object (I edited my answer once I noticed that). – devconsole May 10 '13 at 16:45
  • Thanks, I edited that and I actually thought it might solve it (fallen for that mistake so many times) but unfortunately it did not. – Bono May 10 '13 at 16:46
  • The implementation of generateViewId() seems to be concerned with not returning zero, maybe you should start with 1. – devconsole May 10 '13 at 16:48
  • Wow... That actually did it, thanks! If you edit your answer I'll accept it! – Bono May 10 '13 at 16:51
0

Seems like you are calling this code in a loop. In that case just put i - 1 (previous view id) as id for the rule.

MaciejGórski
  • 22,187
  • 7
  • 70
  • 94
  • However, this should work. Can you post your full code (with the loop), so that we could figure out why ? – Orabîg May 10 '13 at 16:26