I have two text views, at one instance only one text view should be present and at another instance both the text should be present. When only one one text view is present it should be aligned center.
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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/disp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/background_color">
<ImageView
android:id="@+id/dev_ic"
style="@style/dev_list"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/dev_name"
style="@style/dev_title"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
app:layout_constraintStart_toEndOf="@+id/dev_ic"
app:layout_constraintTop_toTopOf="@id/dev_ic"
tools:text="TEXT" />
<TextView
android:id="@+id/dev_connect_state"
android:visibility="gone"
android:text="@string/connecting"
style="@style/connection_state"
app:layout_constraintStart_toStartOf="@id/dev_name"
app:layout_constraintTop_toBottomOf="@id/dev_name" />
<View
style="@style/separator"
android:id="@+id/separator"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/dev_ic" />
</androidx.constraintlayout.widget.ConstraintLayout>
Styles.xml :
<style name="dev_list">
<item name="android:layout_width">@dimen/icon_size</item>
<item name="android:layout_height">@dimen/icon_size</item>
<item name="android:layout_marginStart">@dimen/element_margin</item>
</style>
<style name="dev_title">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:ellipsize">end</item>
<item name="android:textColor">?attr/text_color</item>
<item name="android:textSize">@dimen/small_text_size</item>
</style>
<style name="connection_state">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:ellipsize">end</item>
<item name="android:textColor">@color/primary</item>
<item name="android:textSize">@dimen/small_text_size</item>
</style>
I have to set app:layout_constraintBottom_toBottomOf="@id/dev_ic" (sets to the middle, adjacent to the image view) for text view with id : dev_name programmatically..
@BindView(R2.id.disp)
ConstraintLayout mDevItem;
void alignDeviceNameToCenter(boolean isAlignCenter) {
ConstraintSet constraintSet = new ConstraintSet();
constraintSet.clone(mDevItem);
if (isAlignCenter) {
Log.d("TAG","Going to align center");
constraintSet.connect(R2.id.dev_name, ConstraintSet.BOTTOM, R2.id.dev_ic, ConstraintSet.BOTTOM, 0);
} else {
Log.d("TAG","Going to clear the center alignment");
constraintSet.clear(R2.id.dev_name, ConstraintSet.BOTTOM);
}
constraintSet.applyTo(mDevItem);
}
Though I could, see the prints, the Text view is not aligned center to the imageview. Whereas when I set the app:layout_constraintBottom_toBottomOf="@id/dev_ic in xml, it gets aligned. But I couldn't do the same programmatically.
Update :
When there is only one text view, then it should be centered w.r.t image icon. otherwise, the other text view appears to the bottom.