How to Lay Out Views in Relativelayout Programmatically

Relative Layout adding views programmatically one below other not aligning correctly

I was using the index of the for loop as id for the textViews which I changed to make Android generate Id for it as below and it is working fine now as expected.

I changed the line

textView.setId(i)

to

textView.setId(ViewGroup.generateViewId())

Thanks.

How to align CenterVertical in Relative layout programmatically

(Posted solution on behalf of the question author).

I finally found the solution myself! I changed the relative layout to a vertical linear layout (I don't know if it matters or not):

val params2 = LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)

val linearLayout = LinearLayout(this)
linearLayout.setLayoutParams(params2)
linearLayout.orientation = LinearLayout.VERTICAL
linearLayout.requestLayout()

and then set the width of imageButtonDelete to match parent.

val params3 = LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)


val imageButtonDelete = ImageButton(this)
imageButtonDelete.setLayoutParams(params3)
imageButtonDelete.requestLayout()
imageButtonDelete.setBackgroundColor(Color.TRANSPARENT)

Align horizontally Views programmatically in a Relative Layout

I resolved.. this is the update of my loop:

int idOltText = 0;
for(DocumentField d: listOfFields){
if(d.isHighlighted()){
TextView txt = new TextView(getApplicationContext());
txt.setBackgroundDrawable(getResources().getDrawable(R.drawable.shp_round_corners));
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
params.setMargins(3, 0, 0, 0);
if(idOltText != 0){
System.out.println(idOltText);
params.addRule(RelativeLayout.RIGHT_OF, idOltText);
}
txt.setLayoutParams(params);
txt.setTag("field");
GradientDrawable drawable = (GradientDrawable)txt.getBackground();
if(usedcolorMap.get(code) != null)
drawable.setColor(usedcolorMap.get(code));
if(d.isNumeric())
txt.setText(d.convertedNumber());
else if(d.isText())
txt.setText(d.getTextValue());
else if(d.isDate())
txt.setText(d.convertedDate());
String text = txt.getText().toString();
if(metrics.densityDpi <= DisplayMetrics.DENSITY_MEDIUM){
if(text.length() > 8)
text = text.substring(0, 8).trim() + "...";
}else if(text.length() > 15){
text = text.substring(0, 15).trim() + "...";
}
txt.setTextColor(Color.WHITE);
txt.setTextSize(12);
txt.setTypeface(null, Typeface.BOLD);
txt.setText(text);
idOltText++;
txt.setId(idOltText);
ll.addView(txt);

}
}

How to add a view programmatically to RelativeLayout?

Heres an example to get you started, fill in the rest as applicable:

TextView tv = new TextView(mContext);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
params.leftMargin = 107
...
mRelativeLayout.addView(tv, params);

The docs for RelativeLayout.LayoutParams and the constructors are here

Add view programmatically to RelativeLayout android

You are aligning the textviews with button tops and lefts here:

    textparams.addRule(RelativeLayout.ALIGN_TOP, btn.getId());
textparams.addRule(RelativeLayout.LEFT_OF, btn.getId());

You need to do the same for the ImageButtons - except align the first one with the parent, then each subsequent one you need to align the top to the previous ImageButton (here is fake code):

    btn.addRule(RelativeLayout.ALIGN_BOTTOM, btnAbove.getId());

Then keep a reference to the previous button in "btnAbove" at the end of your loop like:

btnAbove = btn;

Programmatically Position Views in RelativeLayout

Since my content is not dynamic, I worked around the issue by simply editing my image in Paint and placing text below the image. Then I made that the background for the ImageView.

Creating relativeLayout programmatically?

Try the following. I have not tested it. Let me know if you have any problems.

Define the styles like this

<style name="MyStyleText">
<item name="android:textColor">#ff0000</item>
<item name="android:textSize">12sp</item>
<item name="android:shadowColor">#ffffff</item>
<item name="android:shadowDx">0</item>
<item name="android:shadowDy">0</item>
<item name="android:shadowRadius">10</item>
</style>

And in java file

RelativeLayout.LayoutParams lp ;
lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);


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

RelativeLayout rl = new RelativeLayout(this);


TextView tv = new TextView(this);
tv.setText(getString(R.string.hello_world));
tv.setTextAppearance(getApplicationContext(), R.style.MyStyleText);

rl.addView(tv, textLP);
this.addContentView(rl, lp);

Add views programmatically in a layout and clustered to the center

From what I have understood, you don't need to set gravity to the LinearLayout childs. Just set

android:layout_centerVertical="true"

on the LinearLayout itself (with height = wrap_content, and vertical orientation). Something like:

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"

android:layout_alignParentLeft="true"
android:layout_centerVertical="true"

android:gravity="center_vertical"
android:orientation="vertical">

Programmatically (though untested):

LinearLayout linearLayout = new LinearLayout(context);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
params.addRule(RelativeLayout.CENTER_VERTICAL);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setGravity(Gravity.CENTER_VERTICAL); //not sure it's needed
linearLayout.setLayoutParams(params);

The linear layout rule will not propagate to its childs: LL will have equally spaced childs. But we set height to wrap_content so it's gonna be exactly that tall, and we set center_vertical so it's gonna stay centered.



Related Topics



Leave a reply



Submit