dynamically increase the relative layout size in android - android

i have a relative layout on which i am displaying a page and some content. when i zoom my page...the layout size is not increasing. i want my layout to increase its size dynamically. how do i set it??
i tried doing it in java code.
contentLayout.getLayoutParams().height = x (some value which is equal to the page size)
contentLayout.requestLayout()
but this is nt working. i have also set the layout params android:layout_width and android:layout_height to wrap-content in the xml file.
Kindly, help me out. thank you

You can do it this way. I also added setting of the margins:
RelativeLayout targetItem = (RelativeLayout) findViewById(R.id.RelativeLayout01);
RelativeLayout.LayoutParams adaptLayout = new RelativeLayout.LayoutParams(mWidth, mHeight);
adaptLayout.setMargins(marginLeft, marginTop, marginRight, marginBottom);
targetItem.setLayoutParams(adaptLayout);
For mWidth and mHeight you also may set dip values to make it adapt to the different screen sizes. Easy way to do that is to define Dimension in your strings.xml and work with them, not with absolute values.
So, if you define relative_width as 120dip, and relative_height as 100 dip then in code you have
RelativeLayout.LayoutParams adaptLayout = new RelativeLayout.LayoutParams(getResources().getDimension(R.dimen.relative_width), getResources().getDimension(R.dimen.relative_height));

Related

How to give layout width as double value in android?

I am trying to set layout width as float value programmatically. But it accept only int value.
LinearLayout linearLayout = findViewById(R.id.parent_layout);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
layoutParams.width = 200.75; // Here I am getting error.
But if I set by xml I am able to give float value.
<LinearLayout
android:id="#+id/parent_layout"
android:orientation="vertical"
android:layout_width="200.75dp"
android:layout_height="200dp"/>
But not able to set programmatically. Please let me some idea to resolve this.
DP (DIPs) are not pixels and "layoutParams.width=" requires Pixels.
200.75dp is not equal to 200.75px because it's Display Density dependant.
200.75dp could be more than 600px on some display.
You can convert PX in DP and vice-versa using simple methods (please search online).
However an internal conversion and Math.round() is done when "layout_width="200.75dp"" is executed in Runtime, and you can simulate it using appropriated conversion methods.

Set width and height of various views programmatically in Android app

For an app i have an activity with various views and I need them all to be square, have the exact same height and width and occupy all together a specified portion of the screen. Therefore, I need to set the width and height programmatically, which seems to work with
View view = findViewById(R.id.nutrition_bar_filled);
LayoutParams layoutParams = view.getLayoutParams();
layoutParams.width = newWidth;
view.setLayoutParams(layoutParams);
However, with many views this is pretty tedious, is there a way to set all heights and widths at once as they are all uniform?
Thanks in advance!

First child of LinearLayout inside ScrollView with 1/3 space of device height

I have a ScrollView with a LinearLayout with 3 elements inside. I would like that the first element has a height of 1/3 the height of the device height and the other 2 with wrap_content, is this possible to do in xml or how would you do this? Using weight alone it does not seem possible because the LinearLayout inside the ScrollView could be longer than the device's height.
According to Android docs, android:layout_weight:
Indicates how much of the extra space in the LinearLayout is allocated to the view associated with these LayoutParams. Specify 0 if the view should not be stretched. Otherwise the extra pixels will be pro-rated among all views whose weight is greater than 0.
This implies that the weight is not dependent on the screen height, but rather only on its parent view, so you can't achieve that from xml. To achieve this I would compute the height of the screen and then resize the view:
Display screen = getWindowManager().getDefaultDisplay();
Point size = new Point();
screen.getSize(size);
int screenHeight = size.y;
myView.setLayoutParams(new LayoutParams(myView.getWidth(), screenHeight / 3));
You can give the first linear layout weight=0.333 with height=0 and the other two layout wrap_content

Two ways of specifying the height of an android Layout?

Im having problems specifiying the height of a relative Layout. From what i understand, these two blocks of code should be equivalent (myLayout is a RelativeLayout that i defined in XML, with an initial height of "0dp", and it's parent is also a RelativeLayout):
BLOCK 1:
RelativeLayout.LayoutParams p = (RelativeLayout.LayoutParams)myLayout.getLayoutParams();
p.height = (int)(35*scale);
myLayout.setLayoutParams(p);
myLayout.invalidate();
BLOCK 2:
RelativeLayout.LayoutParams p = (RelativeLayout.LayoutParams)myLayout.getLayoutParams();
RelativeLayout.LayoutParams newP = new RelativeLayout.LayoutParams(p.width, (int)(35*scale));
myLayout.setLayoutParams(newP);
myLayout.invalidate();
Scale, in my case, is 2.
So i expect myLayout to have a height of 70 after the execution of either of these blocks, and i expect it to return that height when i call myLayout.getHeight(). Also, i expect myLayout to occupy a rect with the height of 70 and its former width (happens to be match_parent).
But when i run the code, the first block does not change the height of myLayout on screen, nor does it change the return value of myLayout.getHeight(). It does, however, change myLayout.getLayoutParams().height.
Now the second block does work, although i have to run it twice(!?) for the change to take effect. Im seriously at a loss here and i cant find anything even closely related to this in the docs.
I thought this would be an easy task when i set out yesterday, but by now im questioning my sanity, among other things. Any Ideas?
If size is determined dynamically, you may need to use ViewTreeObserver to get the size seen on the screen.
Set your width and height in defining your params. If you want the width to be match_parent, change your code like below and it is going to work (if you don't want match_parent, just set the width that you desire in the code below):
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
LayoutParams.MATCH_PARENT, (int)(35*scale));
myLayout.setLayoutParams(params);

Android layoutparms scrollvew height

I have a tableview inside of a scrollview. I want to reserve space for a listview 2/3 of the screen. I am getting the height of the screen, and will divide that in 2/3 and set the height of the ScrollView. But even with manually x and y numbers its blowing up.
App is crashing when I set the ScrollView width and height like this:
ScrollView sv = (ScrollView)findViewById(R.id.usdScroll);
ScrollView.LayoutParams layoutParams = new ScrollView.LayoutParams( -1, 550);
sv.setLayoutParams(layoutParams);
Any ideas what I did wrong?
You can use the layout_weight attribute to specify how much of a view the specific component will use, e.g. android:layout_weight=2. So for your listView set a weight of 1 and a weight of 2 for your tableview, I think. Anyway, play with the weight setting and you can split the view in any ratio you like.
LayoutParams is used to tell their parents how they want to be laid out. So the layout param's type which you set to must match its parent class type exactly.
and here is a better solution, using layout_weight instead. Try this please
<ScrollView android:layout_weight="1" ........></ScrollView>
<ListView android:layout_weight="2" ........></ListView>

Categories

Resources