Jacopo Sciampi
Jacopo Sciampi

Reputation: 3443

Programmatically add textView with constraint

I have a ScrollView defined by the xml. Inside of it I want to have two textView like in this photo :

enter image description here

Doing that in xml is fine. But how to add them programmatically? Here's what the code look like without the two TextView example:

.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/rl"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/transparent"
    tools:context=".InventoryActivity">

    <ImageView
        android:id="@+id/imageView9"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="1.0"
        app:srcCompat="@drawable/inventorylayoutbg" />

    <ImageView
        android:id="@+id/imageView33"
        android:layout_width="61dp"
        android:layout_height="59dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:onClick="closeDialog"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.254"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.927"
        app:srcCompat="@drawable/blank" />

    <ScrollView
        android:id="@+id/sv"
        android:layout_width="326dp"
        android:layout_height="374dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/imageView9">

        <LinearLayout
            android:id="@+id/ll"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"></LinearLayout>
    </ScrollView>

</android.support.constraint.ConstraintLayout>

.java file

package com.example.jsciampi.tapper;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ScrollView;

public class InventoryActivity extends AppCompatActivity {
    //Form
    ScrollView sv;
    LinearLayout ll;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_inventory);

        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);

        int width = (int)(dm.widthPixels*.9);
        int height = (int)(dm.heightPixels*.8);

        getWindow().setLayout(width,height);

        sv = findViewById(R.id.sv);
        ll = findViewById(R.id.ll);
        for(int i = 0; i < 5; i++){
           Button b = new Button(InventoryActivity.this);

           //Add Constraint there

            ll.addView(b);
        }
    }

    public void closeDialog(View view) {
        finish();
    }
}

The second TextView had this xml config:

<TextView
    android:id="@+id/textView2"
    android:layout_width="324dp"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:text="TextView"
    app:layout_constraintBottom_toBottomOf="@+id/sv"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/firstText"
    app:layout_constraintVertical_bias="0.0" />

and I really just need to converto from xml to java this part here :

app:layout_constraintBottom_toBottomOf="@+id/sv"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/aads"
        app:layout_constraintVertical_bias="0.0"

Can anyone help me out?

Upvotes: 0

Views: 902

Answers (1)

pop
pop

Reputation: 579

 for (int i = 0; i < 5; i++) {
         View row = LayoutInflater.from(this).inflate(R.layout.image_text_row,null,false);
         ll.addView(row);
        }

add the below layout to file with name image_text_row.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/background_dark"
    android:orientation="horizontal">

    <ImageView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_weight="1"
        android:src="@android:drawable/ic_dialog_email" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:text="Text"
        android:textColor="@android:color/white"
        android:textSize="20sp" />

</LinearLayout>

Upvotes: 1

Related Questions