1

There's similar questions, but I haven't been able to find any as of yet that get into the basics. I'm making a to-do list application and I want it so that when I click the plus button, the user is asked for text and the item is displayed on the screen. Once the text is recieved from the user, I'm not sure how you put this into a textview. add that textview into a constraintlayout(activity_main.xml) and align it with another textview (menutitletext) so that it doesn't default to 0,0. I haven't been able to add code since I genuinely don't know where to start - the answers either are adding imageviews, are in Kotlin or are using a relativelayout. I'd appreciate any help on this.

Someone asked for my design - I think this is what you wanted ?

<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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<View
    android:id="@+id/divider"
    android:layout_width="393dp"
    android:layout_height="2dp"
    android:layout_marginTop="28dp"
    android:background="#000000"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/menuaddbutton" />

<ImageButton
    android:id="@+id/menusettingsbutton"
    android:layout_width="65dp"
    android:layout_height="55dp"
    android:layout_marginStart="11dp"
    android:layout_marginTop="30dp"
    android:layout_marginEnd="8dp"
    android:layout_marginBottom="20dp"
    android:src="@drawable/test1w"
    app:layout_constraintBottom_toTopOf="@+id/divider"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toEndOf="@+id/menutrashbutton"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.235" />

<ImageButton
    android:id="@+id/menutrashbutton"
    android:layout_width="60dp"
    android:layout_height="65dp"
    android:layout_marginStart="12dp"
    android:layout_marginTop="30dp"
    android:layout_marginBottom="20dp"
    app:layout_constraintBottom_toTopOf="@+id/divider"
    app:layout_constraintStart_toEndOf="@+id/menuaddbutton"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.0"
    app:srcCompat="@drawable/test1d" />

<ImageButton
    android:id="@+id/menuaddbutton"
    android:layout_width="78dp"
    android:layout_height="59dp"
    android:layout_marginStart="22dp"
    android:layout_marginTop="30dp"
    android:layout_marginEnd="156dp"
    android:layout_marginBottom="20dp"
    android:background="@android:color/background_light"
    app:layout_constraintBottom_toTopOf="@+id/divider"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toEndOf="@+id/menueditbutton"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.6"
    app:srcCompat="@drawable/test1z" />

<ImageButton
    android:id="@+id/menueditbutton"
    android:layout_width="59dp"
    android:layout_height="59dp"
    android:layout_marginStart="80dp"
    android:layout_marginTop="30dp"
    android:layout_marginBottom="20dp"
    android:background="@android:color/background_light"
    android:onClick="edittitle"
    app:layout_constraintBottom_toTopOf="@+id/divider"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.0"
    app:srcCompat="@drawable/test1f" />

<TextView
    android:id="@+id/menutitletext"
    android:layout_width="366dp"
    android:layout_height="36dp"
    android:layout_marginStart="12dp"
    android:layout_marginTop="15dp"
    android:gravity="center_horizontal"
    android:text="@string/menutitleempty"
    android:textAppearance="@style/TextAppearance.AppCompat.Large"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/divider" />

</androidx.constraintlayout.widget.ConstraintLayout>

dreg41
  • 15
  • 5

2 Answers2

1

You can use same concept as in here https://stackoverflow.com/a/40527407/3904645

It adds views to ConstraintLayout programmatically and aligns them with programmatic constraints. This is what you need I believe.

For your case

set.connect(childView.getId(), ConstraintSet.TOP, parentLayout.getId(), 
ConstraintSet.TOP, 60);

change parentLayout.getId() with the last list item that you have added. You can track what is the last view with some instance variable.

class MainActivity : AppCompatActivity() {

lateinit var parentLayout: ConstraintLayout

var lastView: View? = null
var numberIfViews = 0

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    parentLayout = findViewById<View>(R.id.constraint_layout) as ConstraintLayout
    lastView = parentLayout
}

fun addTextView(text: String) {

    val set = ConstraintSet()

    val childView = TextView(this)
    childView.id = View.generateViewId()
    childView.text = text
    parentLayout.addView(childView, numberIfViews)

    set.clone(parentLayout)
    set.connect(
        childView.id,
        ConstraintSet.TOP,
        lastView!!.id,
        ConstraintSet.TOP,
        60
    )
    set.applyTo(parentLayout)
    numberIfViews++
    lastView = childView
}

}

Have not tested it but it should work

  • I'm a complete beginner, so sorry about that. I'm struggling with ConstraintLayout layout = (ConstraintLayout) findViewById(R.id.activity_main) . How do I reference activity_main.xml to create the constraintlayout object ? – dreg41 Jul 07 '20 at 10:15
  • Give main layout an id like other views have. and get it with 'findViewById'. In your case it's constraintLayout. – Rezo Shalikashvili Jul 07 '20 at 10:22
  • Your comment was in Kotlin, but it cleared my understanding - especially with the other answer link you provided. Thank you! – dreg41 Jul 07 '20 at 11:07
0
 ConstraintLayout parentLayout = (ConstraintLayout)findViewById(R.id.mainConstraint);
    

    TextView tt= new TextView (this);
    // set view id, else getId() returns -1
    childView.setId(View.generateViewId());
    layout.addView(tt, 0);

you can try this way

Firat
  • 43
  • 8
  • With R.id.mainConstraint, how do I reference activity_main.xml ? R.id.activity_main doesn't work. Additionally, how would I set a position relative to another object, say another textview called menutitletext? – dreg41 Jul 07 '20 at 10:17