1

This isn't much of a question, more of an explanation, I like to know whats happening and not treat things like a black box. I'm making an android app, and I created a form where you can dynamically make a new field to add to the form, and the field has 2 edit text next to each other.

formLayout.xml

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="1">

<EditText
    android:id="@+id/team_form_player_name"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginBottom="10dp"
    android:layout_marginRight="7dp"
    android:layout_weight="1"
    android:background="@color/white"
    android:hint="@string/form_hint_team_player_name"
    android:inputType="textCapWords"
    android:padding="15dp"/>

<EditText
    android:id="@+id/team_form_player_number"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="10dp"
    android:background="@color/white"
    android:hint="@string/form_hint_team_player_number"
    android:inputType="number"
    android:padding="15dp"/>
</LinearLayout>

And this is inside a button listener when clicked, it'll create the new field

 View layout = getLayoutInflater().inflate(R.layout.layout_add_new_field_team, null);
 EditText name = (EditText)layout.findViewById(R.id.team_form_player_name);
 EditText number = (EditText)layout.findViewById(R.id.team_form_player_number);
 playerName.add(name); //arraylist
 playerNumber.add(number); //arraylist
 containerLayout.addView(layout);

being that its a different layout i had to inflate the view. anyway, my question is this. how does this even work? by code, I'm adding an edit text, each time to its respectful array. im adding the same layout each time, im not even using "new" so its not instantiating anything new. and if I create 10 fields at once, then fill in the fields. and run this method

for(EditText edit : playerNumber) {
      String test = edit.getText().toString();
      System.out.println(test);
}

it gives me all the correct values in each field. How does that happen? Because on add field click, it instantly adds the edit text to the array with the fields EMPTY. and theres no code inputting the values into the array. I'm just boggled that this works and would like to know how it does. anyone have any idea?

Steven R
  • 323
  • 2
  • 6
  • 18

1 Answers1

3

.inflate(layoutResId) == "create new instance of this layout"

So basically every time you use inflate you are creating instances of all the Views in the layout, in your case a LinearLayout and two EditText Views inside it.

Raanan
  • 4,777
  • 27
  • 47
  • that makes sense! and im assuming that when created they have their own special id that can't be seen? (like its own address) – Steven R Jul 13 '15 at 16:57
  • In general yes, they are unique and each will have its own hashCode. Every View is a Java Object and it's uniqueness is defined by its hashCode and you can see it using getHashCode(). http://stackoverflow.com/questions/4930781/how-do-hashcode-and-identityhashcode-work-at-the-back-end – Raanan Jul 13 '15 at 17:37