I have imageviews inside a linearlayout. I am trying to get all the imageviews(children) when its parent(linearlayout) is clicked. Could you please tell me how to access all the children of linearlayout when the linearlayout is clicked?
Asked
Active
Viewed 477 times
-4
-
Please elaborate on what exactly you are trying to achieve. – Jantzilla Nov 19 '18 at 06:05
-
It doesn't look like you have searched before asking, while you have to. – Vladyslav Matviienko Nov 19 '18 at 06:09
2 Answers
1
<?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
}
});
}
}

Community
- 1
- 1

Machhindra Neupane
- 679
- 7
- 20
0
Try this code..
LinearLayout ll =findViewById(R.id.linear);
final int childCount = ll.getChildCount();
for (int i = 0; i < childCount; i++) {
View v = ll.getChildAt(i);
}