Android - Setting Layoutparams Programmatically

android - setting LayoutParams programmatically

Just replace from bottom and add this

tv.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));

before

llview.addView(tv);

Setting LayoutParams programmatically

The type of LayoutParams are actually set by the parent of the view, so bar.getLayoutParams() should be returning an object of type RelativeLayout.LayoutParams. With an explicit cast to LinearLayout.LayoutParams I'd expect your logcat to contain a line like this:

Caused by: java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to android.widget.LinearLayout$LayoutParams

Changing it to this should do the trick:

RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) bar.getLayoutParams();

or, since height is in the base class, don't cast it at all:

ViewGroup.LayoutParams params = bar.getLayoutParams();

Setting LayoutParams programmatically not working

Add these line in your code

imageView.setScaleType(ScaleType.FIT_XY);

or

imageView.setScaleType(ScaleType.CENTER);

Try this it may work

How can I set the LayoutParams of an View Programmatically?

Well, I found the answer, but it's a completely different approach, since none of the above or those I found elsewhere worked for me.

I did the following changes in the onCreate method:

    mGraphicsHolder = new GraphicsHolder(this);

LayoutInflater inflate = (LayoutInflater)
getSystemService(Context.LAYOUT_INFLATER_SERVICE);

setContentView(mGraphicsHolder);

getWindow().addContentView(inflate.inflate(
R.layout.controllayout, null), new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.FILL_PARENT));

Joystick = (JoystickView) getWindow().findViewById(R.id.joystickView1);
Joystick.setOnJostickMovedListener(this);

Now I got my Joystick on an XML Layout, but It actually works (at least in my case where nothing else worked), and it's quite easy to make changes in things of Layout etc.

Add layout with parameters programmatically Android

LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
(w*2)/3,
LayoutParams.WRAP_CONTENT
);
linLayout.setLayoutParams(lp);

This will throw error because the LinearLayout is the child of Relative layout, so you have to set RelativeLayoutParams for that.

Instead use this

RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
(w*2)/3,
RelativeLayout.LayoutParams.WRAP_CONTENT
);
linLayout.setLayoutParams(lp);

or
Instead of creating new LayoutParams you can reuse the existing LayoutParams of the view as shown below.

RelativeLayout.LayoutParams lp = linLayout.getLayoutParams();
lp.width = (w*2)/3;
lp.height = RelativeLayout.LayoutParams.WRAP_CONTENT;
linLayout.setLayoutParams(lp);

How to set LayoutParams properly

  1. you write table.width/5 so you probably use j in 0..4

  2. See the reference for TableLayout:

    The children of a TableLayout cannot specify the layout_width attribute. Width is always MATCH_PARENT. However, the layout_height attribute can be defined.

    Use android:stretchColumns to have the table adjust the button widths correctly.

    But,

    The children of a TableRow do not need to specify the layout_width and layout_height attributes in the XML file. TableRow always enforces those values to be respectively ViewGroup.LayoutParams.MATCH_PARENT and ViewGroup.LayoutParams.WRAP_CONTENT.

    This means that, by the book, you cannot fill the parent with a table vertically. But by hook or by crook, you can force the height of each button to be exactly 1/10 of the full height.

    What you can't do, is take table.height while building your layout, because at this stage the height of the parent is not known yet. I hope you can calculate this height based on the screen size.

  3. With such programmatic setup, you don't need table layout, horizontal linear views inside vertical that fills the parent could be enough.

  4. With ConstraintLayout you can specify width and height of elements as percents of the parent, see https://android.jlelse.eu/whats-new-in-constraint-layout-1-1-0-acfe30cfc7be



Related Topics



Leave a reply



Submit