In this post, we are going to discuss about Margins in Android, how to set them and why they might fail to work sometimes. If you would like to skip to the solution to “android layout_marginLeft not working”, you can scroll down to the last section.
Before we go into details in this post, I will first like to say that developing An Android App UI is very easy and a lot more understandable compared to developing for BlackBerry OS and BlackBerry10.
However, there are times when the simplest and obvious things like adding margins (top, right, down, left) to an android view will just not work. It will actually ignore you like you are a learner :).
Before we go into details about why the margin attributes might not work in some circumstances, I would like to define what a margin is.
What is a Margin?
A margin is simply the gap between the outer part of the view and another view. It can also be the gap between the outer part of a view and its parent. See the image below:
How to set Margin in Android
I like to categorize the methods used to set margins into 2 major categories namely:
- Setting margin with XML
- Setting margin programmatically (with Java)
For this tutorial, we will cover only the XML method.
Setting Margin Top with XML
1 2 3 4 |
<TextView android:layout_width="match_parent" android:layout_height="wrap_content" <b>android:layout_marginBottom="20dp"</b>/> |
Setting Margin Right with XML
1 2 3 4 |
<TextView android:layout_width="match_parent" android:layout_height="wrap_content" <b>android:layout_marginRight="20dp"</b>/> |
Setting Margin Bottom with XML
1 2 3 4 |
<TextView android:layout_width="match_parent" android:layout_height="wrap_content" <b>android:layout_marginBottom="20dp"</b>/> |
Setting Margin Left with XML
1 2 3 4 |
<TextView android:layout_width="match_parent" android:layout_height="wrap_content" <b>android:layout_marginLeft="20dp"</b>/> |
Setting Margin of all sides in 1 line
You can additionally set the margin of all sides of a view using the following code:
1 2 3 4 |
<TextView android:layout_width="match_parent" android:layout_height="wrap_content" <b>android:layout_margin="20dp"</b>/> |
Why Margin left,right,top and down might fail
Now that we know how to set margins with XML, let us examine the main Scenario where these codes will fail. There are different scenarios where setting margins will fail, however, in this post, we will discuss the most common scenario.
Scenario 1 (android
:margin is already set)
This simply means that you have already set Margin for all sides on that view. If you try to add an additional margin attribute to the view, it will be ignored. See the code sample below:
1 2 3 4 5 |
<TextView android:layout_width="match_parent" android:layout_height="wrap_content" <span style="color: red; font-weight: bold;">android:layout_margin="40dp"</span> android:layout_marginBottom="10dp"/> |
In the code above, android:layout_marginBottom="10dp"
would be ignored because android:layout_margin="40dp"
takes precedence.
In order to solve this issue, you need to get rid of the “android:layout_margin” attribute and replace it by individually specifying the attributes for setting marginTop, Right, Bottom and Left.
If you have any questions or contributions, feel free to post them in the comment box below 🙂 .