1

I'm following the accepted answer to this question:

ArrayList<EditText> myEditTextList = new ArrayList<EditText>();

for( int i = 0; i < myLayout.getChildCount(); i++ )
  if( myLayout.getChildAt( i ) instanceof EditText )
    myEditTextList.add( (EditText) myLayout.getChildAt( i ) );

How do you get myLayout, what does it actually represent? In my app I define linear_layout in activitymain.xml but can't seem to get it in MainActivity.java (I tried using R but it didn't work). I'm new to Android development and could really use some high level guidance.

northerner
  • 756
  • 5
  • 21

4 Answers4

4

Let Linear Layout be the root of your xml:

<LinearLayout android:id="@+id/my_layout" >
   //your views goes here
</LinearLayout>

Then in your Activity you'll have to initialize the view once your layout gets inflated like this

LinearLayout myLayout = (LinearLayout) findViewById(R.id.my_layout);

Hope this helps.

Mayank Bhatnagar
  • 2,120
  • 1
  • 12
  • 20
  • Thanks that worked. Why does it work like this? Why must it be initialized in the activity if it's already in the XML? – northerner Jul 17 '18 at 21:40
  • 1
    @northerner You are not initializing the Layout in the activity. Initialization looks like this `LinearLayout mLinearLayout = new LinearLayout();` the method `findViewById()` just gives the reference to the View which has already been initialized because you wrote it in XML. – Vihaari Varma Jul 20 '18 at 06:51
1

You need to use myLayout as your root layout in your layout.xml file

the myLayout maybe a FrameLayout, LinearLayout, RelativeLayout or ConstraintLayout layout

SAMPLE CODE

<?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:id="@+id/myLayout"
    android:orientation="vertical">

        <!-- add your view here-->

</LinearLayout>

Now do findViewById in side your actvity

public class MyActivity extends AppCompatActivity {

    LinearLayout myLayout;

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

        myLayout = findViewById(R.id.myLayout);
    }


}
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
0

You can try below code to get a reference of your LinearLayout in Java code.

LinearLayout myLayout=(LinearLayout) findViewById(R.id.your_linear_layout_id)

P Vartak
  • 434
  • 5
  • 17
0

assuming your actiivtymain.xml looks like this

<?xml version="1.0" encoding="utf-8"?>
<SomeLayout
 android:id="@+id/some_id"
...>
 <EditText
...
/>

<EditText
...
/>
.
.
.
</SomeLayout>

mLayout you are referring is the parent of all your edit texts. It may not be the root of your layout but if you want to get a Java instance of it, you need to give it an id. and get the instance by using the code given by @Nilesh Rathod. One more thing to remember is to call the method findViewById(R.id.my_layout); only after setContentView(R.layout.activitymain);.

Vihaari Varma
  • 155
  • 11