Dodanie TextView w kodzie android

0

Witam próbuję dodać do Layoutu TextView gdy dany warunek jest spełniony. Umiem to zrobić na nowy layoucie ale chcę zrobić na aktualnym.Problem w tym że nie wiem jak dostać się do aktualnego layout kod który powinien działać nie działa

 setContentView(R.layout.activity_main);
        View layout=findViewById(R.id.) // tu go nie mogę wyszukać

a to ten layout

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.micha.shoppinglist.MainActivity">

    <ImageButton
        android:id="@+id/imageButton4"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_marginBottom="32dp"
        android:layout_marginEnd="32dp"
        android:layout_marginRight="32dp"
        android:background="@drawable/button_rounded"
        android:clickable="true"
        android:src="@drawable/plus"
        android:stateListAnimator="@null"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintRight_toRightOf="parent" />

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        android:minHeight="?attr/actionBarSize"
        android:textAlignment="center"
        android:backgroundTint="@color/colorPrimary"
        android:theme="?attr/actionBarTheme"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="-2dp"
        app:layout_constraintHorizontal_bias="0.0">

        <TextView
            android:id="@+id/toolBarTitle"
            style="@style/TextAppearance.AppCompat.Widget.ActionBar.Title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:fontFamily="monospace"
            android:text="@string/app_name"
            android:textColor="@color/white" />
    </android.support.v7.widget.Toolbar>

    <ListView
        android:id="@+id/list"
        android:layout_width="0dp"
        android:layout_height="519dp"
        android:layout_marginTop="8dp"
        app:layout_constraintTop_toBottomOf="@+id/toolbar"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginBottom="92dp"
        app:layout_constraintVertical_bias="1.0" />


</android.support.constraint.ConstraintLayout>

0

Nie możesz po prostu pokazywać tego EditView gdy warunek jest spełniony i chować gdy nie jest?

Jeżeli koniecznie chcesz dodawać, to możesz zrobić to w ten sposób:

LayoutParams lparams = new LayoutParams(
   LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
TextView tv=new TextView(this);
tv.setLayoutParams(lparams);
tv.setText("test");
this.activity_main.addView(tv);

1 użytkowników online, w tym zalogowanych: 0, gości: 1