Babadzhanov
Babadzhanov

Reputation: 528

Position the ImageButton after its inflated in ConstraintLayout

I already have ConstraintLayout and I want to add few buttons for colour pick. But the button I add is always at the very top left and can't seem to find a solution.. hot to put them under everything else That's the .xml i have:

<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:id="@+id/activity_editor_main"
    android:layout_height="match_parent">

<!-- Overview category -->
<LinearLayout
    android:id="@+id/linearLayout2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/view">

    <!-- Label -->
    <TextView
        style="@style/PlayerTextStyle"
        android:padding="16dp"
        android:text="@string/player_1" />

    <EditText
        style="@style/InputTextStyle"
        android:hint="@string/player_name"
        android:inputType="textCapWords"
        android:textAppearance="?android:textAppearanceMedium" />
</LinearLayout>

<!-- Label -->
<TextView
    android:id="@+id/player_colour"
    style="@style/PlayerTextStyle"
    android:padding="16dp"
    android:text="@string/player_colour"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/linearLayout2" />

<LinearLayout
    android:id="@+id/linearLayout3"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp"
    android:orientation="horizontal"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/player_colour">


</LinearLayout>




</android.support.constraint.ConstraintLayout>

That's the button i want to add in the last LinearLayout:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/colour_button_editor"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <ImageButton
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:background="@drawable/a"
        android:gravity="center_vertical|center_horizontal" />

</FrameLayout>

I want to use LayoutInflater to inflate like 10-12 different colours to pick from and add them under few TextViews. I don't want a dialog! Any suggestions how to make it work??

Upvotes: 1

Views: 771

Answers (1)

Sree...
Sree...

Reputation: 343

When you add view dynamically to a layout whose parent is ConstarintLayout the default position is top-left corner. You can use ConstraintLayout.LayoutParams to set constraints dynamically.

Upvotes: 1

Related Questions