How to change width and height of existing layout params - android

Please consider ImageView inside RelativeLayout it has some specific layout params for RelativeLayout. I need to change the width and height of my ImageView programatically according to the screen size.
How can I save all layout parameters but only change width and height
Following code creates empty layout params but I need to save my old params and change only height and width.
imageViewArticleImage.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, mScreenSize.y / 3));
How can I achieve this ?

Use getLayoutParams to get the current layout params. Change the width and height and go from there. IF the view is already visible, call requestLayout to force a new layout pass.

Related

layoutparams is not honored when using addView

swipeToRefresh.addView(webView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 1));
Basically my webview is added to swipeToRefresh. I try to set the height here to be 1, but my webview still fill the entire screen.
swipeToRefresh is a subclass of SwipeRefreshLayout and is using match_parent in both width and height. Any idea how can I know why the height is not used? How to know why is it changed?

addRule in RelativeLayout.LayoutParams is not working

I have this code:
final RelativeLayout headerRl=new RelativeLayout(SpeakersActivity.this);
headerRl.setBackgroundColor(Color.parseColor(almacen.getColor()));
final TextView initial=new TextView(SpeakersActivity.this);
final RelativeLayout.LayoutParams headerParams=new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 80);
headerParams.addRule(RelativeLayout.CENTER_VERTICAL,RelativeLayout.TRUE);
initial.setText(arrayInitials.get(i));
initial.setTextColor(getColor(R.color.white));
initial.setTextSize(16);
initial.setPadding(10,0,0,0);
runOnUiThread(new Runnable() {
#Override
public void run() {
headerRl.addView(initial,headerParams);
ll.addView(headerRl);
}
});
"ll" is a LinearLayout. I am adding the rule on a RelativeLayout.LayoutParams, I am adding the view to a RelativeLayout, which is added to a LinearLayout. But my view is not centered vertically. I tried to center it horizontally to see if it was working, but it isn't. Why is my view not centered in the RL? How to center it?
Thank you.
Depending on what result you are expecting
There are 2 ways to deal with your problem.
First one:
The issue is in the headerRl which is set by default as wrap_content, wrap_content
So, what you need is to define it's layout params with match_parent:
headerRlLayoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
headerRl.setLayoutParams(headerRlLayoutParams)
The first parameter could be wrap_content, depends on what you want, but the second one should be match_parent, otherwise, it will be wrap_content by default which will position your view at the top
Create headerParams as headerParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT...
And that's it.
Match parent here causes your view to take the whole parent's width. If that's what you want, then see the second option below
Second one:
If you want your view as match_parent and take the whole view, then
your issue is:
new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT
Which cause your text view to be match_parent in the layout. And because you didn't set view's gravity - text has default one - top/start.
And the solution is simple - Set TextView Gravity (which same as setting gravity param in the XML) - the view will stay as match_parent - taking the whole width of its parent's layout but the text will be centered vertically
initial.setGravity(Gravity.CENTER_VERTICAL);
And a small hint - You also can set in developer options "show layout bounds" to see how much space your layout takes and how views are positioned them self in the layout. This could help to debug and understand what exactly is going wrong
initial.setGravity(Gravity.CENTER_VERTICAL);
Try with
final RelativeLayout.LayoutParams headerParams=new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 80);
/**
* Rule that centers the child vertically with respect to the
* bounds of its RelativeLayout parent.
*/
headerParams.addRule(RelativeLayout.CENTER_VERTICAL,RelativeLayout.TRUE);
// your textview
initial.setGravity(Gravity.CENTER_VERTICAL);

Resize ImageView programmatically

I'm working with an ImageView and I want to resize it programmatically passing (for example) from a full-screen ImageView to a 50x50. Is there a way to do that?
Different from the one you suggested me to see because I don't need to fit the image in the ImageView but to resize the dimension of the ImageView.
My suggestion use a relative layout and put image inside that has with weight and able to auto resize which depends on the weight. The best way to auto resize even your screen rotates.
I didn't remember which one layout has weight format
You can use LayoutParams to set height width programmatically -
ViewGroup.LayoutParams layoutParams = imageView.getLayoutParams();
layoutParams.width = 30;
layoutParams.height = 30;
imageView.setLayoutParams(layoutParams);
ConstraintLayout provides a mechanism for fixing the aspect ratio of a child view. Select the child view that we want to control, and then set the ratio value, we can manually put the ratio that we want to set.
Using constraintLayout makes UI shrink or expand according to screen size without distorting the UI. This is the most important advantage of using ConstraintLayout in UI.

Framelayout different heights

I have a Activity conainting a header with some buttons(See picture).
You can switch the framelayout by clicking on the buttons. I have the framelayout set on wrap_content in the height for making the whole thing scrollable in a scrollview.
But I want to show only a map in the framelayout if you push on button2. And the height of the map need to be FILL_PARENT INSTEAD of WRAP_CONTENT. So I need to change the heigth of the FRAMELAYOUT.
Any ideas how I can do that?
This is what I tried:
ViewGroup.LayoutParams layoutParams = frameLayout.getLayoutParams();
layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT;//MATCH_PARENT because FILL_PARENT is deprectated
frameLayout.setLayoutParams(layoutParams);

Image with fixed size in procents on Android

I have a bunch of images with different sizes. Each of them should be presented on the top part of the screen and must take the space in height equal to 60% of screen height. Width of the image will be dependent on it's height to save initial proportions. I tried to use weightsum property in layout and weight property in ImageView, but I don't know what to put in the height property of my image view. If it is "wrap_content", every image resizes my ImageView and all mark-up crushes.
Any advices?
If you want, you can set the image's dimensions by code.
Just set the width with the weight_sum method and then do something like:
WindowManager manager = (WindowManager) context.getSystemService(Activity.WINDOW_SERVICE);
int screenHeight = manager.getDefaultDisplay().getHeight();
YOUR_VIEW.getLayoutParams().height = (int) (screenWidth * 0.6);
Please mind that you can do so only AFTER your ImageView has been drawn on screen - so calling it within the onCreate() method will not work.
You can either call it delayed (postDelayed) or set a layout listener to one of your view.
Hope this helps.

Categories

Resources