Get All Child Views Inside Linearlayout at Once

Get all child views inside LinearLayout at once

Use getChildCount() and getChildAt(int index).

Example:

LinearLayout ll = …
final int childCount = ll.getChildCount();
for (int i = 0; i < childCount; i++) {
View v = ll.getChildAt(i);
// Do something with v.
// …
}

How to access all children of linearLayout?

<?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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
tools:context=".MainActivity"
android:id="@+id/linerLayout"
>

<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/imageView"
android:src="@drawable/ic_launcher_background"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/textId"
/>

</LinearLayout>

MainActivity.java

package com.example.macchhindra.stackoverflowexample;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
LinearLayout linearLayout;
TextView myTextView;
ImageView myImageView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
linearLayout = (LinearLayout) findViewById(R.id.linerLayout);
myTextView = (TextView) findViewById(R.id.textId);
myImageView = (ImageView) findViewById(R.id.imageView);
linearLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
/// change your Image view image in this section
}
});
}
}

Find child of a LinearLayout inside a LinearLayout

I think I found the answer to it. You need to change a little bit of code in the startGame() method I m providing the code for startGame below.

   public void startGame(View view) {
LinearLayout ll = findViewById(R.id.playerView);
List text = new ArrayList();

for (int loop = 0; loop < ll.getChildCount(); loop++) {
//this is the code in question and where I want to get the text from
//all my EditTexts
LinearLayout inner = (LinearLayout) ll.getChildAt(loop);
for (int j = 0; j < inner.getChildCount(); j++) {
if (inner.getChildAt(j) instanceof EditText) {
EditText textET = (EditText) inner.getChildAt(j);

Log.d("TAG",textET.getText().toString());
}
}

}
}

In the above code you were able to get the first child only but as you have added a linearLayout with orientation Horizontal in a parent LinearLayout with orientation Vertical, you have written code for the child of parent layout i.e playerView. I have modified the code to get the elements of the child Linear layout and Log prints all the text from the EditText.

Hope that helps!!

Getting child elements from LinearLayout

You can always do something like this:

LinearLayout layout = setupLayout();
int count = layout.getChildCount();
View v = null;
for(int i=0; i<count; i++) {
v = layout.getChildAt(i);
//do something with your child element
}

Get all linearlayout child elements

Change this line

layout = (View) findViewById(R.id.exchangeView);

to

layout = (LinearLayout) findViewById(R.id.exchangeView);

I am assuming you have declared the variable layout as

View layout;

If so, change that to

LinearLayout layout;

The View class doesn't have the method getChildCount(), it is a method in the ViewGroup class. LinearLayout extends ViewGroup, and hence has access to it. View does not. This is related to polymorphism, which I suggest you look up.
Cheers.

Android | Get all children elements of a ViewGroup

(source)

If you want to get all the child views, as well as the views within children ViewGroups, you must do it recursively, since there is no provision in the API to do this out of the box.

private ArrayList<View> getAllChildren(View v) {

if (!(v instanceof ViewGroup)) {
ArrayList<View> viewArrayList = new ArrayList<View>();
viewArrayList.add(v);
return viewArrayList;
}

ArrayList<View> result = new ArrayList<View>();

ViewGroup viewGroup = (ViewGroup) v;
for (int i = 0; i < viewGroup.getChildCount(); i++) {

View child = viewGroup.getChildAt(i);

ArrayList<View> viewArrayList = new ArrayList<View>();
viewArrayList.add(v);
viewArrayList.addAll(getAllChildren(child));

result.addAll(viewArrayList);
}
return result;
}

This will give you an ArrayList with all the Views in the hierarchy which you can then iterate over.

Essentially, this code call itself if it finds another ViewGroup in the hierarchy, and then returns an ArrayList to be added to the bigger ArrayList.

Android when looping through LinearLayout child views it always gives me that child views are the same

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
}

Adding views inside an existing LinearLayout in a specific place according to map size

Add a LinearLayout where you have your comment <!-- This is where I want to add the dynamic views --> and add the TextViews inside that ViewGroup, instead of nestedLinearLayout

As for

so I am missing the option to put it at a specific place

you can call addView with a second int parameter which is the index where you want the view inserted

how to get child positions of dynamically added edittext in linearlayout?

Try this way setId for view controls.

        linearLayout=(LinearLayout)findViewById(R.id.lin);
textView.setText(s);
for(int i=0;i<3;i++){
txt=new TextView(Second.this);
txt.setText(""+i+"");
txt.setId(i);
linearLayout.addView(txt);
}
int position=linearLayout .getChildCount();
Log.d("linearLayout","count"+position);

for(int i = 0; i < linearLayout .getChildCount(); ++i) {
View v = linearLayout.getChildAt(i);
int currentViewId =v.getId();
Log.d("linearLayout","currentViewId"+currentViewId);
if(currentViewId == 1) {
txt.setTextColor(Second.this.getResources().getColor(R.color.colorPrimary));

}
else if (currentViewId==2){

}
}


Related Topics



Leave a reply



Submit