<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>view margin &#8211; MaXEster Technologies  | Technical Blog</title>
	<atom:link href="https://www.maxester.com/blog/tag/view-margin/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.maxester.com/blog</link>
	<description>Tutorials, Examples and Implementation code for Developers Help</description>
	<lastBuildDate>Mon, 17 Feb 2025 09:17:48 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=5.0.22</generator>
	<item>
		<title>Change margin of a view programmatically?</title>
		<link>https://www.maxester.com/blog/2023/07/12/change-margin-of-a-view-programmatically/</link>
		<comments>https://www.maxester.com/blog/2023/07/12/change-margin-of-a-view-programmatically/#respond</comments>
		<pubDate>Wed, 12 Jul 2023 11:09:16 +0000</pubDate>
		<dc:creator><![CDATA[Jony Chawla]]></dc:creator>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[android]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[margin]]></category>
		<category><![CDATA[programmatically]]></category>
		<category><![CDATA[serMargin]]></category>
		<category><![CDATA[view margin]]></category>

		<guid isPermaLink="false">https://www.maxester.com/blog/?p=1123</guid>
		<description><![CDATA[<p>Let suppose view in a LinearLayout. First you have to get Layoutparams of the view and cast into to specific Layoutparams and modify the margins. As I remove margins by setting it all by 0. OR You can create a&#8230;</p>
<p><a href="https://www.maxester.com/blog/2023/07/12/change-margin-of-a-view-programmatically/" class="btn-more">Read More<span class="arrow-more">&#8594;</span></a></p>
<p>The post <a rel="nofollow" href="https://www.maxester.com/blog/2023/07/12/change-margin-of-a-view-programmatically/">Change margin of a view programmatically?</a> appeared first on <a rel="nofollow" href="https://www.maxester.com/blog">MaXEster Technologies  | Technical Blog</a>.</p>
]]></description>
				<content:encoded><![CDATA[
<p>Let suppose view in a LinearLayout. First you have to get Layoutparams of the view and cast into to specific Layoutparams and modify the margins. As I remove margins by setting it all by 0.</p>



<pre class="wp-block-code"><code>((LinearLayout.LayoutParams) view.getLayoutParams()).setMargins(0,0,0,0);

               {or}

((LinearLayout.LayoutParams) view.getLayoutParams()).topMargin = 5;</code></pre>



<p style="text-align:center">OR</p>



<p>You can create a LayoutParams variable and set margins to it. And set LayoutParams back to the view. as below</p>



<pre class="wp-block-code"><code>LinearLayout.LayoutParams parmas = (LinearLayout.LayoutParams) holder.imageView.getLayoutParams();
            parmas.setMargins(0,5,0,0);
            holder.imageView.setLayoutParams(parmas);</code></pre>



<p style="text-align:center">OR</p>



<p>You can use it in more generic way without specifying the particular Layout. </p>



<p><br>If the view is inside RelativeLayout.  </p>



<pre class="wp-block-code"><code>RelativeLayout.LayoutParmas</code></pre>



<p>Or you can use generic ViewGroup.MarginLayoutParams without specifying parent</p>



<pre class="wp-block-code"><code>if (view.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
        ViewGroup.MarginLayoutParams mp = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
        mp.setMargins(0,5,0,0);
        view.requestLayout();
    }</code></pre>
<p>The post <a rel="nofollow" href="https://www.maxester.com/blog/2023/07/12/change-margin-of-a-view-programmatically/">Change margin of a view programmatically?</a> appeared first on <a rel="nofollow" href="https://www.maxester.com/blog">MaXEster Technologies  | Technical Blog</a>.</p>
]]></content:encoded>
			<wfw:commentRss>https://www.maxester.com/blog/2023/07/12/change-margin-of-a-view-programmatically/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
