- Android and setting width and height programmatically in dp units
- 5 Answers 5
- how to set width which is equal to another widget on android
- 7 Answers 7
- Set ImageView width and height programmatically?
- 15 Answers 15
- Set View Width Programmatically
- 7 Answers 7
- How to programmatically set the width of the LinearLayout?
- 5 Answers 5
Android and setting width and height programmatically in dp units
button.setLayoutParams(new GridView.LayoutParams(65, 65));
According to the docs the units for the width and height (both 65 in the above) are «pixels». How do you force this to be device independent pixels, or «dp»?
5 Answers 5
You’ll have to convert it from dps to pixels using the display scale factor.
I know this is an old question however I’ve found a much neater way of doing this conversion.
Java
Kotlin
Looking at your requirement, there is alternate solution as well. It seems you know the dimensions in dp at compile time, so you can add a dimen entry in the resources. Then you can query the dimen entry and it will be automatically converted to pixels in this call:
And your dimens.xml will have:
Extending this idea, you can simply store the value of 1dp or 1sp as a dimen entry and query the value and use it as a multiplier. Using this approach you will insulate the code from the math stuff and rely on the library to perform the calculations.
simplest way(and even works from api 1) that tested is:
Retrieve a dimensional for a particular resource ID for use as a size in raw pixels. This is the same as getDimension(int), except the returned value is converted to integer pixels for use as a size. A size conversion involves rounding the base value, and ensuring that a non-zero base value is at least one pixel in size.
Yes it rounding the value but it’s not very bad(just in odd values on hdpi and ldpi devices need to add a little value when ldpi is not very common) I tested in a xxhdpi device that converts 4dp to 16(pixels) and that is true.
Источник
how to set width which is equal to another widget on android
I need to draw a horizontal line below a text field such that the width of the line equals the text width (not the width of the full screen).
In my app I have a textview below a view(Horizontal line). The width of the line view should be equal to the width of the textview. I tried android:layout_width=»wrap_content» and «match_parent», which does not solve the problem.
This is xml coding sample:
image of the screen is:
7 Answers 7
If you use a RelativeLayout you can use the align -attributes:
If you’re using a Layout other than a RelativeLayout, you can match the widths of your widgets programmatically, such as in this example:
Notice that both text fields are both set to wrap_content.
If you have multiple widgets that you want to have a uniform width, just repeat the above code for the new element. For example, let’s say there was a button I wanted to adjust the width to, to match the other elements:
Use RelativeLayout and use these two attribute in horizontal line view
What Jave said is correct — and the easiest, but what if you’re not using a RelativeLayout to contain the View’s ?
If you’re customizing your UI within onCreate() then you’ll find that obtaining the width from another widget will give you an incorrect result ! That’s because the UI hasn’t been set up yet.
But you can still set up your UI within onCreate. simply run code that executes after the UI is set up. This is achieved through use of the View.post() command.
The XML :
Java code:
The result is that the Width of button_one will match the Width of button_two . This is a nicer look when the amount of text varies heavily between the two View’s.
Источник
Set ImageView width and height programmatically?
How can I set an ImageView ‘s width and height programmatically?
15 Answers 15
It may be too late but for the sake of others who have the same problem, to set the height of the ImageView :
Important. If you’re setting the height after the layout has already been ‘laid out’, make sure you also call:
If your image view is dynamic, the answer containing getLayout will fail with null-exception.
In that case the correct way is:
This simple way to do your task:
and another way if you want to give screen size in height and width then use below code :
hope use full to you.
I have played this one with pixel and dp .
How to set by pixel:
How to set by dp:
Hope this would help you.
If you need to set your width or height to match_parent (as big as its parent) or wrap_content (large enough to fit its own internal content), then ViewGroup.LayoutParams has this two constants:
Or you can set them like in Hakem Zaied’s answer:
You can set value for all case.
Simply create a LayoutParams object and assign it to your imageView
The best and easiest way to set a button dynamically is
The above height and width are in pixels px. 45 being the height in dp and 42 being the width in dp.
So, for example, if you’ve placed your button within a TableRow within a TableLayout, you should have it as TableRow.LayoutParams
returns height value in pixels. You need to get the screen dimensions first to set the values properly.
After you get the screen dimensions, you can set the imageview width&height using proper margins.
In order to set the ImageView and Height Programatically, you can do
image.setLayoutParams(new ViewGroup.LayoutParams(width, height));
kotlin
In the scenario where the widget size needs to be set programmatically, ensure the below rules.
- Set LayoutParam for the Layout in which you are adding that view. In my case I am adding to TableRow so I had to do TableRow.LayoutParams
- Follow this code
final float scale = getResources().getDisplayMetrics().density; int dpWidthInPx = (int) (17 * scale); int dpHeightInPx = (int) (17 * scale);
Источник
Set View Width Programmatically
I am trying to set the width and height of a view in code to show an ad for a free app I am working on. All of the UI is done in XML with the exception of this ad. Here is the code that displays the ad. It is in onCreate .
This results in the ad being displayed like this. As you can see the width doesn’t quite fill the whole page.
Now when I change the code to this to attempt to set the size:
I have tried all different combinations of FILL_PARENT and WRAP_CONTENT all with the same result. Is there something I am missing? Or another way I can have the ad fill the width of the device without filling the height? Any suggestions and help is greatly appreciated.
EDIT when I try to put it in XML I get an exception:
7 Answers 7
You can use something like code below, if you need to affect only specific value, and not touch others:
This code let you fill the banner to the maximum width and keep the ratio. This will only work in portrait. You must recreate the ad when you rotate the device. In landscape you should just leave the ad as is because it will be quite big an blurred.
in code add below line:
The first parameter to LayoutParams is the width and the second is the height. So if you want the width to be FILL_PARENT, but the width to be, say, 20px, then use something new LayoutParams(FILL_PARENT, 20). Of course you should never use actual pixels in your code; you’ll need to conver that to density-independent pixels, but you get the idea. Also, you need to make sure your parent LinearLayout has the right width and height that you’re looking for. Seems to be you want the LinearLayout to fill the parent width-wise and then have the adview fill that linearlayout witdh-wise as well, so you probably need to specify android:layout_width:»fill_parent» and android:layout_height:»wrap_content» in your linear layout’s xml.
check it in mdpi device.. If the ad displays correctly, the error should be in «px» to «dip» conversion..
Источник
How to programmatically set the width of the LinearLayout?
Is the source of the slider and the lesson link:
In this case, the width of the file «left_menu.xml» determined TextView (android:layout_width=»260dp») How do I set the width to «LinearLayout» file «left_menu.xml» depending on the device screen? For example, I want the width of the «LinearLayout» was always 1/3 of the screen device? Or any way to set the width of the TextView 1/3 of the width of the device screen.
5 Answers 5
To set the LinearLayout or TextView width to 1/3 of the device screen:
first of all get the device screen width:
now just create a LayoutParams and set it to the Text:
As you can see, you can set Integer values for the LinearLayout.LayoutParams() constructor, like this:
The costructor wants pixels and not dp(Density Pixels), here’s a formula to convert PXs from DPs:
The above answers are correct. I’ll just suggest future readers to take a look at their xml layout file and be sure that the LinearLayout element is not using weight attribute. If so, consider updating weight in your LayoutParams object. Another good practice is to retrieve the layout params object from the view you’re trying to adjust, instead of creating one from scratch and having to set all the values.
Источник