Reputation: 166
I want to make a vertical line at the start of each item in a list view, it looks great at the android studio preview screen as shown in here
But when i run it on my phone it doesn't appear, any idea why? screenshoot from my phone
Here is the XML script for item_view.xml file:
<?xml version="1.0" encoding="utf-8"?>
<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:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:maxHeight="80dp"
android:orientation="vertical"
android:padding="8dp">
<View
android:id="@+id/line2"
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="@color/colorPrimaryDark"
app:layout_constraintLeft_toLeftOf="parent" />
<TextView
android:id="@+id/tv_item_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:maxLines="1"
android:singleLine="true"
android:text="Tesla Stock's Terrible Week: A Buying Opportunity?"
android:textColor="@color/colorAccent"
android:textSize="18dp"
android:textStyle="bold"
app:layout_constraintLeft_toRightOf="@+id/line2"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/tv_item_by"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:text="By"
app:layout_constraintStart_toStartOf="@+id/tv_item_title"
app:layout_constraintTop_toTopOf="@+id/tv_item_name"
tools:layout_conversion_absoluteHeight="19dp"
tools:layout_conversion_absoluteWidth="336dp" />
<TextView
android:id="@+id/tv_item_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:ellipsize="end"
android:text="Fool.com"
app:layout_constraintStart_toEndOf="@+id/tv_item_by"
app:layout_constraintTop_toBottomOf="@+id/tv_item_title" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Read More"
android:textColor="@color/colorPrimaryDark"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="@+id/tv_item_name"
app:layout_constraintEnd_toStartOf="@+id/iv_arrow"
app:layout_constraintTop_toTopOf="@+id/tv_item_name" />
<ImageView
android:id="@+id/iv_arrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_arrow"
app:layout_constraintBottom_toBottomOf="@+id/textView2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@+id/textView2" />
</android.support.constraint.ConstraintLayout>
I didn't change any attributes from java code. Thank you.
Upvotes: 1
Views: 431
Reputation: 3234
The problem occurs because you're using ConstraintLayout which has it's own rules.
Instead of using:
<View
android:id="@+id/line2"
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="@color/colorPrimaryDark"
app:layout_constraintLeft_toLeftOf="parent" />
Use:
<View
android:id="@+id/line2"
android:layout_width="1dp"
android:layout_height="0dp"
android:background="@color/colorPrimaryDark"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent" />
Essentially, if you look at this documentation on ConstraintLayout's Dimensions Constraints, you'll see that it mentions about not using MATCH_PARENT and to use MATCH_CONSTRAINT (aka 0dp) instead.
Also, you need to add in the top and bottom constraints so that the ConstraintLayout knows how to size it's height.
Upvotes: 1