1

In my example i'm adding new LinearLayout which containt 3 views (TextView, EditText, Button) by pressing a button.

After adding every LinearLayout i want to loop through his child views, find TextView and EditText and give them some stupid text.

However, when i loop through LinearLayout child views, and do checks to find out which child view is TextView and which EditText, it looks like every child views is TextView.

Here is the code how LinearLayout looks, xml file name is: (field_konacno.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="50dp"
    android:orientation="horizontal" >

    <TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:text="A)"
        android:layout_marginTop="10dp"
        android:textSize="20sp"
        android:textStyle="bold"
        android:gravity="center"
        />

    <EditText
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="5"
        android:hint="Unesite pitanje"
        android:textColorHint="@color/colorPrimaryDark"/>

    <Button
        android:layout_width="0dp"
        android:layout_height="40dp"
        android:layout_weight="1"
        android:layout_marginTop="10dp"
        android:background="@android:drawable/ic_delete"
        android:onClick="deleteView" />

</LinearLayout>

Here is how my Main4Activity looks:

public class Main4Activity extends AppCompatActivity {

    private LinearLayout parentLinearLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main4);
        parentLinearLayout = (LinearLayout) findViewById(R.id.activity4_linearayout);

        //creating first view
        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final View rowView = inflater.inflate(R.layout.field_konacno, null);
        parentLinearLayout.addView(rowView, parentLinearLayout.getChildCount());

    }

//adding new LinearLayout by pressing button
    public void addNewLinearLayoutWithViews(View view) {
        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final View rowView = inflater.inflate(R.layout.field_konacno, null);

        parentLinearLayout.addView(rowView, parentLinearLayout.getChildCount());


        if(rowView instanceof LinearLayout){
            LinearLayout linearLayout = (LinearLayout) rowView;
            for (int i = 0; i<linearLayout.getChildCount(); i++){
                if (linearLayout.getChildAt(i) instanceof TextView){
                    TextView someTetView = (TextView) linearLayout.getChildAt(i);
                    someTetView.setText("TV " + i);
                }else if (linearLayout.getChildAt(i) instanceof EditText){
                    EditText editText = (EditText) linearLayout.getChildAt(i);
                    editText.setText("EditText " + i);
                }else {
                    //do nothing
                }
            }
        }
    }

    public void deleteView(View view) {
        parentLinearLayout.removeView((View) view.getParent());
    }


}

Here is how my activity_main4.xml looks like: Please note that i have add onClick via xml, not via listener.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.petar.addviewsdynamicly.Main4Activity">

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

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Dodaj polje"
        android:onClick="addNewLinearLayoutWithViews"/>


</LinearLayout>

And here is the picture so that you can see that when ever i add new linearLayout, all his child views are TextView. In code i set up for TextView to write TV and in EditText to write Edit Text

enter image description here

Thanks in advance!

NoName
  • 263
  • 5
  • 19

1 Answers1

3

Start with a condition for EditText. EditText is subclass of TextView, so "instanceof TextView" returns true for its instances.

if (linearLayout.getChildAt(i) instanceof EditText){
    EditText editText = (EditText) linearLayout.getChildAt(i);
    editText.setText("EditText " + i);
} else if (linearLayout.getChildAt(i) instanceof TextView){
     TextView someTetView = (TextView) linearLayout.getChildAt(i);
     someTetView.setText("TV " + i);
}else {
   //do nothing
}
Josef Adamcik
  • 4,873
  • 3
  • 30
  • 38