Android – findViewById() in a loop

By -
Problem: How do we iterate through all the views(taken in XML layout) programatically?
Description:

I am sure you don’t get the problem exactly. So let me give more idea for the better understanding of this problem.

Consider, i have declared 10 button with ‘id’ like button1, button2, button3, button4, button5, button6 and so on. So To access it within activity class, we have to declare and access it through findViewById() method.

So one simple way is to write 10 sentences for 10 buttons to find view, example:

Button btn1 = (Button) findViewById(R.id.button1);
Button btn2 = (Button) findViewById(R.id.button2);
Button btn3 = (Button) findViewById(R.id.button3);
Button btn4 = (Button) findViewById(R.id.button4);
....
....
....

Just consider that we are having more number of buttons say for example 100 buttons, then?

So here is the solution to write less lines of code to perform the same task.

Solution:

LoopFindViewByIDActivity.java

 

package com.technotalkative.loopfindviewbyid;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class LoopFindViewByIDActivity extends Activity implements OnClickListener{
/** Called when the activity is first created. */

Button[] buttons;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

buttons = new Button[10];

for(int i=0; i<buttons.length; i++) {
{
String buttonID = "button" + (i+1);

int resID = getResources().getIdentifier(buttonID, "id", getPackageName());
buttons[i] = ((Button) findViewById(resID));
buttons[i].setOnClickListener(this);
}
}
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub

int index = 0;
for (int i = 0; i < buttons.length; i++)
{
if (buttons[i].getId() == v.getId())
{
index = i;
break;
}
}

Toast.makeText(this, "Button clicked index => "+index, Toast.LENGTH_SHORT).show();
}
}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />

<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />

<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />

<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />

<Button
android:id="@+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />

<Button
android:id="@+id/button7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />

<Button
android:id="@+id/button8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />

<Button
android:id="@+id/button9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />

<Button
android:id="@+id/button10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>

Download Full Example here: Android – findViewById() in a loop

  

CEO & Co-Founder at SolGuruz® | Organiser @ GDG Ahmedabad | Top 0.1% over StackOverflow | 15+ years experienced Tech Consultant | Helping startups with Custom Software Development

Loading Facebook Comments ...
Loading Disqus Comments ...