Layout margin not applying to constraint layout in RecyclerView
I am attempting to add spacing between views in the RecyclerView. Using the RecyclerView I have a repeating list of constraint views using a view adapter. On each constraint view I have set the layout margin to equal 15dp like so android:layout_margin="15dp"
. When previewing the layout in design view I can see that the margin is being applied however when the app is compiled the RecyclerView.
The XML for the view being used inside the RecyclerView is as follows:
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:background="@drawable/gradient"
android:clipToPadding="true"
android:minHeight="0dp">
<TextView
android:id="@+id/moment_timestamp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="16dp"
android:text="28 MAR @ 2:45pm"
android:textColor="@android:color/white"
app:layout_constraintEnd_toStartOf="@+id/moment_text"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/moment_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:layout_marginEnd="20dp"
android:layout_marginStart="20dp"
android:layout_marginTop="40dp"
android:background="@android:color/transparent"
android:includeFontPadding="false"
android:paddingVertical="0dp"
android:text="A kind man held the door open for me today"
android:textAlignment="viewStart"
android:textColor="@android:color/white"
android:textSize="25sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/moment_timestamp" />
</android.support.constraint.ConstraintLayout>
In design view, the component I am designing looks like this:
However, when compiled and being used in the RecyclerView, the margins are not applied:
This is my code where I inflate the layout. Originally the width of the view was not being applied so as shown in this answer I changed my code.
View view = inflater.inflate(R.layout.text_moment, null, false);
RecyclerView.LayoutParams lp = new
RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
view.setLayoutParams(lp);
return new MomentViewHolder(view);
Searched around quite extensively now and can't seem to find a solution.
java android android-recyclerview android-constraintlayout
add a comment |
I am attempting to add spacing between views in the RecyclerView. Using the RecyclerView I have a repeating list of constraint views using a view adapter. On each constraint view I have set the layout margin to equal 15dp like so android:layout_margin="15dp"
. When previewing the layout in design view I can see that the margin is being applied however when the app is compiled the RecyclerView.
The XML for the view being used inside the RecyclerView is as follows:
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:background="@drawable/gradient"
android:clipToPadding="true"
android:minHeight="0dp">
<TextView
android:id="@+id/moment_timestamp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="16dp"
android:text="28 MAR @ 2:45pm"
android:textColor="@android:color/white"
app:layout_constraintEnd_toStartOf="@+id/moment_text"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/moment_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:layout_marginEnd="20dp"
android:layout_marginStart="20dp"
android:layout_marginTop="40dp"
android:background="@android:color/transparent"
android:includeFontPadding="false"
android:paddingVertical="0dp"
android:text="A kind man held the door open for me today"
android:textAlignment="viewStart"
android:textColor="@android:color/white"
android:textSize="25sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/moment_timestamp" />
</android.support.constraint.ConstraintLayout>
In design view, the component I am designing looks like this:
However, when compiled and being used in the RecyclerView, the margins are not applied:
This is my code where I inflate the layout. Originally the width of the view was not being applied so as shown in this answer I changed my code.
View view = inflater.inflate(R.layout.text_moment, null, false);
RecyclerView.LayoutParams lp = new
RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
view.setLayoutParams(lp);
return new MomentViewHolder(view);
Searched around quite extensively now and can't seem to find a solution.
java android android-recyclerview android-constraintlayout
Possible duplicate of LayoutInflater ignoring parameters?
– Killer
Dec 28 '18 at 23:32
add a comment |
I am attempting to add spacing between views in the RecyclerView. Using the RecyclerView I have a repeating list of constraint views using a view adapter. On each constraint view I have set the layout margin to equal 15dp like so android:layout_margin="15dp"
. When previewing the layout in design view I can see that the margin is being applied however when the app is compiled the RecyclerView.
The XML for the view being used inside the RecyclerView is as follows:
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:background="@drawable/gradient"
android:clipToPadding="true"
android:minHeight="0dp">
<TextView
android:id="@+id/moment_timestamp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="16dp"
android:text="28 MAR @ 2:45pm"
android:textColor="@android:color/white"
app:layout_constraintEnd_toStartOf="@+id/moment_text"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/moment_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:layout_marginEnd="20dp"
android:layout_marginStart="20dp"
android:layout_marginTop="40dp"
android:background="@android:color/transparent"
android:includeFontPadding="false"
android:paddingVertical="0dp"
android:text="A kind man held the door open for me today"
android:textAlignment="viewStart"
android:textColor="@android:color/white"
android:textSize="25sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/moment_timestamp" />
</android.support.constraint.ConstraintLayout>
In design view, the component I am designing looks like this:
However, when compiled and being used in the RecyclerView, the margins are not applied:
This is my code where I inflate the layout. Originally the width of the view was not being applied so as shown in this answer I changed my code.
View view = inflater.inflate(R.layout.text_moment, null, false);
RecyclerView.LayoutParams lp = new
RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
view.setLayoutParams(lp);
return new MomentViewHolder(view);
Searched around quite extensively now and can't seem to find a solution.
java android android-recyclerview android-constraintlayout
I am attempting to add spacing between views in the RecyclerView. Using the RecyclerView I have a repeating list of constraint views using a view adapter. On each constraint view I have set the layout margin to equal 15dp like so android:layout_margin="15dp"
. When previewing the layout in design view I can see that the margin is being applied however when the app is compiled the RecyclerView.
The XML for the view being used inside the RecyclerView is as follows:
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:background="@drawable/gradient"
android:clipToPadding="true"
android:minHeight="0dp">
<TextView
android:id="@+id/moment_timestamp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="16dp"
android:text="28 MAR @ 2:45pm"
android:textColor="@android:color/white"
app:layout_constraintEnd_toStartOf="@+id/moment_text"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/moment_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:layout_marginEnd="20dp"
android:layout_marginStart="20dp"
android:layout_marginTop="40dp"
android:background="@android:color/transparent"
android:includeFontPadding="false"
android:paddingVertical="0dp"
android:text="A kind man held the door open for me today"
android:textAlignment="viewStart"
android:textColor="@android:color/white"
android:textSize="25sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/moment_timestamp" />
</android.support.constraint.ConstraintLayout>
In design view, the component I am designing looks like this:
However, when compiled and being used in the RecyclerView, the margins are not applied:
This is my code where I inflate the layout. Originally the width of the view was not being applied so as shown in this answer I changed my code.
View view = inflater.inflate(R.layout.text_moment, null, false);
RecyclerView.LayoutParams lp = new
RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
view.setLayoutParams(lp);
return new MomentViewHolder(view);
Searched around quite extensively now and can't seem to find a solution.
java android android-recyclerview android-constraintlayout
java android android-recyclerview android-constraintlayout
asked Dec 28 '18 at 23:24
Alex WileyAlex Wiley
114112
114112
Possible duplicate of LayoutInflater ignoring parameters?
– Killer
Dec 28 '18 at 23:32
add a comment |
Possible duplicate of LayoutInflater ignoring parameters?
– Killer
Dec 28 '18 at 23:32
Possible duplicate of LayoutInflater ignoring parameters?
– Killer
Dec 28 '18 at 23:32
Possible duplicate of LayoutInflater ignoring parameters?
– Killer
Dec 28 '18 at 23:32
add a comment |
2 Answers
2
active
oldest
votes
Try to to this:
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="15dp" //This line is the important one
android:background="@drawable/gradient"
android:clipToPadding="true"
android:minHeight="0dp">
Or what i always do:
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp" //This line is the important one
android:background="@drawable/gradient"
android:clipToPadding="true"
android:minHeight="0dp">
See above, just managed to figure it out :) Thanks for taking the time anyway.
– Alex Wiley
Dec 28 '18 at 23:32
Nice, happy coding. =)
– Alexander Cavalheiro Becker
Dec 28 '18 at 23:34
add a comment |
I've managed to figure out the solution. It turns out the answer that I followed to try and resolve the width issue was causing the margin issue. Instead of setting null as the ViewGroup parameter on the inflate function, I have now passed ViewGroup into the inflate function like so:
View view = inflater.inflate(R.layout.text_moment, viewGroup, false);
you should mark it duplicate. as there are plenty of question on SO related to ignoring params when parent is not defined
– Killer
Dec 28 '18 at 23:33
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53965353%2flayout-margin-not-applying-to-constraint-layout-in-recyclerview%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Try to to this:
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="15dp" //This line is the important one
android:background="@drawable/gradient"
android:clipToPadding="true"
android:minHeight="0dp">
Or what i always do:
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp" //This line is the important one
android:background="@drawable/gradient"
android:clipToPadding="true"
android:minHeight="0dp">
See above, just managed to figure it out :) Thanks for taking the time anyway.
– Alex Wiley
Dec 28 '18 at 23:32
Nice, happy coding. =)
– Alexander Cavalheiro Becker
Dec 28 '18 at 23:34
add a comment |
Try to to this:
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="15dp" //This line is the important one
android:background="@drawable/gradient"
android:clipToPadding="true"
android:minHeight="0dp">
Or what i always do:
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp" //This line is the important one
android:background="@drawable/gradient"
android:clipToPadding="true"
android:minHeight="0dp">
See above, just managed to figure it out :) Thanks for taking the time anyway.
– Alex Wiley
Dec 28 '18 at 23:32
Nice, happy coding. =)
– Alexander Cavalheiro Becker
Dec 28 '18 at 23:34
add a comment |
Try to to this:
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="15dp" //This line is the important one
android:background="@drawable/gradient"
android:clipToPadding="true"
android:minHeight="0dp">
Or what i always do:
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp" //This line is the important one
android:background="@drawable/gradient"
android:clipToPadding="true"
android:minHeight="0dp">
Try to to this:
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="15dp" //This line is the important one
android:background="@drawable/gradient"
android:clipToPadding="true"
android:minHeight="0dp">
Or what i always do:
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp" //This line is the important one
android:background="@drawable/gradient"
android:clipToPadding="true"
android:minHeight="0dp">
answered Dec 28 '18 at 23:30
Alexander Cavalheiro BeckerAlexander Cavalheiro Becker
156111
156111
See above, just managed to figure it out :) Thanks for taking the time anyway.
– Alex Wiley
Dec 28 '18 at 23:32
Nice, happy coding. =)
– Alexander Cavalheiro Becker
Dec 28 '18 at 23:34
add a comment |
See above, just managed to figure it out :) Thanks for taking the time anyway.
– Alex Wiley
Dec 28 '18 at 23:32
Nice, happy coding. =)
– Alexander Cavalheiro Becker
Dec 28 '18 at 23:34
See above, just managed to figure it out :) Thanks for taking the time anyway.
– Alex Wiley
Dec 28 '18 at 23:32
See above, just managed to figure it out :) Thanks for taking the time anyway.
– Alex Wiley
Dec 28 '18 at 23:32
Nice, happy coding. =)
– Alexander Cavalheiro Becker
Dec 28 '18 at 23:34
Nice, happy coding. =)
– Alexander Cavalheiro Becker
Dec 28 '18 at 23:34
add a comment |
I've managed to figure out the solution. It turns out the answer that I followed to try and resolve the width issue was causing the margin issue. Instead of setting null as the ViewGroup parameter on the inflate function, I have now passed ViewGroup into the inflate function like so:
View view = inflater.inflate(R.layout.text_moment, viewGroup, false);
you should mark it duplicate. as there are plenty of question on SO related to ignoring params when parent is not defined
– Killer
Dec 28 '18 at 23:33
add a comment |
I've managed to figure out the solution. It turns out the answer that I followed to try and resolve the width issue was causing the margin issue. Instead of setting null as the ViewGroup parameter on the inflate function, I have now passed ViewGroup into the inflate function like so:
View view = inflater.inflate(R.layout.text_moment, viewGroup, false);
you should mark it duplicate. as there are plenty of question on SO related to ignoring params when parent is not defined
– Killer
Dec 28 '18 at 23:33
add a comment |
I've managed to figure out the solution. It turns out the answer that I followed to try and resolve the width issue was causing the margin issue. Instead of setting null as the ViewGroup parameter on the inflate function, I have now passed ViewGroup into the inflate function like so:
View view = inflater.inflate(R.layout.text_moment, viewGroup, false);
I've managed to figure out the solution. It turns out the answer that I followed to try and resolve the width issue was causing the margin issue. Instead of setting null as the ViewGroup parameter on the inflate function, I have now passed ViewGroup into the inflate function like so:
View view = inflater.inflate(R.layout.text_moment, viewGroup, false);
answered Dec 28 '18 at 23:31
Alex WileyAlex Wiley
114112
114112
you should mark it duplicate. as there are plenty of question on SO related to ignoring params when parent is not defined
– Killer
Dec 28 '18 at 23:33
add a comment |
you should mark it duplicate. as there are plenty of question on SO related to ignoring params when parent is not defined
– Killer
Dec 28 '18 at 23:33
you should mark it duplicate. as there are plenty of question on SO related to ignoring params when parent is not defined
– Killer
Dec 28 '18 at 23:33
you should mark it duplicate. as there are plenty of question on SO related to ignoring params when parent is not defined
– Killer
Dec 28 '18 at 23:33
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53965353%2flayout-margin-not-applying-to-constraint-layout-in-recyclerview%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Possible duplicate of LayoutInflater ignoring parameters?
– Killer
Dec 28 '18 at 23:32