Android set margins programmatically

Set margins in dp programmatically – Android

Sometimes at runtime, it is required to set margin to a view. But, the problem is getting different size for different screen sizes, i.e. hdpi, xhdpi, xxhdpi, etc.

This could be done easily with the help of screen specific value resources.

Provide screen specific dimension

values/dimen.xml

values-hdpi/dimen.xml

values-xhdpi/dimen.xml

values-xxhdpi/dimen.xml

Calculate the pixels taken by dp

For setting margins programmatically, we need to convert dp into px. This could be done by calling this method.

(adsbygoogle = window.adsbygoogle || []).push(<>);

Set LayoutParams to the view

To set margin to the view create LayoutParams instance, and set margin to the view.

NOTE: In this example, LinearLayout.LayoutParams is used by assuming that the View yourView is enclosed within LinearLayout. If the view is enclosed within any other Layout, use the

Читайте также:  Пройденная san andreas для андроид

Display item selected from AutoCompleteTextView in Chip – Android

In this blog we are going to display a list of planets in an AutoCompleteTextView. The list of planets will…

Display progress using ProgressBar – Android

The scenario is to display progress in a ProgressBar and the percentage in a TextView. Once the download is complete,…

Add border to just one side of View – Android

At some point there is a need to display a layout with border(s). And sometimes the border is required in…

BottomSheetDialogFragment – Disable Cancel on Touch Outside and Drag – Android

Disable Cancel on Touch Outside & on Back press In order to create a BottomSheetDialogFragment which should not dismiss on…

Источник

Set margins in dp programmatically – Android

Sometimes at runtime, it is required to set margin to a view. But, the problem is getting different size for different screen sizes, i.e. hdpi, xhdpi, xxhdpi, etc.

This could be done easily with the help of screen specific value resources.

Provide screen specific dimension

values/dimen.xml

values-hdpi/dimen.xml

values-xhdpi/dimen.xml

values-xxhdpi/dimen.xml

Calculate the pixels taken by dp

For setting margins programmatically, we need to convert dp into px. This could be done by calling this method.

(adsbygoogle = window.adsbygoogle || []).push(<>);

Set LayoutParams to the view

To set margin to the view create LayoutParams instance, and set margin to the view.

NOTE: In this example, LinearLayout.LayoutParams is used by assuming that the View yourView is enclosed within LinearLayout. If the view is enclosed within any other Layout, use the

Display item selected from AutoCompleteTextView in Chip – Android

In this blog we are going to display a list of planets in an AutoCompleteTextView. The list of planets will…

Читайте также:  Лучший ocr для android

Display progress using ProgressBar – Android

The scenario is to display progress in a ProgressBar and the percentage in a TextView. Once the download is complete,…

Add border to just one side of View – Android

At some point there is a need to display a layout with border(s). And sometimes the border is required in…

BottomSheetDialogFragment – Disable Cancel on Touch Outside and Drag – Android

Disable Cancel on Touch Outside & on Back press In order to create a BottomSheetDialogFragment which should not dismiss on…

Источник

Оцените статью