0

I am a bit confused about this. I have an app where user draws rectangular text objects. I added some textviews in a relative layout in my xml (lets say that I have a maximum of 3 text objects). The objects can be moved and resized. I added this code for each textView

TextView tv=(TextView) findViewById(R.id.text1);            
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
System.out.println(texts.get(i).Sx+ " , "+texts.get(i).Sy+ " , "+ texts.get(i).Lx+ " , "+texts.get(i).Ly);
if(texts.get(i).Sx<=texts.get(i).Lx){
    if(texts.get(i).Sy<=texts.get(i).Ly){                       
        lp.setMargins((int)texts.get(i).Sx, (int)texts.get(i).Sy, (int)texts.get(i).Lx, (int)texts.get(i).Ly);
    } else{
        lp.setMargins((int)texts.get(i).Sx, (int)texts.get(i).Ly, (int)texts.get(i).Lx, (int)texts.get(i).Sy);
    }
} else{
    if(texts.get(i).Sy<=texts.get(i).Ly){
        lp.setMargins((int)texts.get(i).Lx, (int)texts.get(i).Sy, (int)texts.get(i).Sx, (int)texts.get(i).Ly);
    } else{
        lp.setMargins((int)texts.get(i).Lx, (int)texts.get(i).Ly, (int)texts.get(i).Sx, (int)texts.get(i).Sy);
    }
}           
tv.setLayoutParams(lp);
tv.setWidth((int)(texts.get(i).width));
tv.setHeight((int)(texts.get(i).height));
tv.setTextSize(texts.get(i).textSize);
tv.setText(text.text);
tv.setTextColor(Color.WHITE);
tv.requestLayout();

Sx, Sy are the starting coos of the object in pixels and Lx, Ly the ending coos.

in the xml I added this:

<RelativeLayout 
    android:layout_height="fill_parent"
    android:layout_width="fill_parent" 
    android:id="@+id/texts" >

    <TextView 
        android:layout_height="fill_parent"
        android:layout_width="fill_parent" 
        android:id="@+id/text1" />

    <TextView 
        android:layout_height="fill_parent"
        android:layout_width="fill_parent" 
        android:id="@+id/text2" />

    <TextView 
        android:layout_height="fill_parent"
        android:layout_width="fill_parent" 
        android:id="@+id/text3" />              

</RelativeLayout>

This seems to work as for in placing the text in the right place. But the width and height of the textview does not seem to be right. Is this a problem of setting the margins in pixels? Some enlightenment would be very useful right now

Dipak Keshariya
  • 22,193
  • 18
  • 76
  • 128
george
  • 1,386
  • 5
  • 22
  • 38
  • if your @text1 fills the parent vertically and horizontally, then nothing is left for the other – max4ever Jun 26 '12 at 08:34
  • it is a relativelayout so they can be one above the other can't they? The problem is, that having only the text1 it also has problems – george Jun 26 '12 at 08:36

2 Answers2

0

LayoutParams are used to align your view dynamically. You can add rule to your params like below, above, bottom, top, align, margin and all(same like you do through xml) then finally you can set this layout params to your view. E.g :

    // Set the params eg. for a button.

    RelativeLayout.LayoutParams button_params = new RelativeLayout.LayoutParams(RelativeLayout
        .LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

    button_params.addRule(RelativeLayout.LEFT_OF,
                          any_view.getId());
    button_params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM,
                          RelativeLayout.TRUE);
    button_params.bottomMargin = screen_height / 15;
    button_params.rightMargin = 20;
    button.setLayoutParams(button_params);
Daud Arfin
  • 2,499
  • 1
  • 18
  • 37
  • but isnt this the same that I do with lp.setMargin method? I want my textView to fit exactly the box the the user painted. I cannot predict where the box is, but I have the coos (Sx,Sy,Lx,Ly). I tried also lp.topMargin=(int)texts.get(i).Sy; lp.bottomMargin=(int)texts.get(i).Ly; lp.leftMargin=(int)texts.get(i).Sx; lp.rightMargin=(int)texts.get(i).Lx; but the problem is still there – george Jun 26 '12 at 08:45
0

You first set width/height to FILL_PARENT, then you overwrite it with px ?

try something like this

View temp = this.findViewById(recent);
RelativeLayout.LayoutParams relativeParams = (LayoutParams) temp.getLayoutParams();

relativeParams.setMargins(0, 0, 0, 50); // llp.setMargins(left, top, right, bottom);
relativeParams.width = 123;
relativeParams.height = 123;
temp.setLayoutParams(relativeParams);
max4ever
  • 11,909
  • 13
  • 77
  • 115