5

I simply want to know . is there a way to create an id's using for loop

I have 10 buttons in xml . there id's are button1,button2,button3... button10 Now i create an Array of Button in java class and do it like this

public class Menu extends Activity
{
    Button[] arrayButton=new Button[10];

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

        init();
    }

    private void init()
    {
        for(int i=1 ; i<9 ; i++)
        {
            String abc = "but"+String.valueof(i);
            int x = Integer.parseInt(abc);


            Log.d("abc", abc);

            Log.d("x", String.valueOf(x) );

            //arrayButton[i] = (Button) findViewById(R.id.x);  // giving error 

            //arrayButton[i].setText("Hello:");
        }
    }

}

I want to know how can i do this kind of work . Getting all button using for loop to make my work faster and some time when i want to set the text of all the buttons .

4 Answers 4

3

use getResources().getIdentifier like

String abc = "but"+String.valueof(i);
int resID = getResources().getIdentifier(abc, "id", getPackageName());
arrayButton[i] = (Button) findViewById(resID );
arrayButton[i].setText("Hello:");

i.e. simply rewrite init() method as

private void init()
    {
        for(int i=1 ; i<9 ; i++)
        {
            String abc = "but"+String.valueof(i);
            int resID = getResources().getIdentifier(abc, "id", getPackageName());
            arrayButton[i] = (Button) findViewById(resID);
            arrayButton[i].setText("Hello:");
        }
    }

Or simple you may use

    int[] buttonIDs = new int[] {R.id.but1, R.id.but2, R.id.but3,R.id.but4, ... }
    for(int i=0; i<buttonIDs.length; i++) {
        Button b = (Button) findViewById(buttonIDs[i]);
        b.setText("Hello:" + b.getText().toString());
}
Sign up to request clarification or add additional context in comments.

2 Comments

Now if i want to open Activities like this how can i do it. i mean startActivity("Activity"+i).. in a for looop ....is it possible
@Nepster but you may start one activity at a time,Please describe exact situation.
2

If you already know all the ids, you can simply use:

int [] ids = new int [] {R.id.btn1, R.id.btn2, ...};
Button [] arrayButton = new Button[ids.length];

for(int i=0 ; i < arrayButton.length ; i++)
{
  arrayButton[i] = (Button) findViewById(ids[i]);
}

Or if you don't know them, use:

getResources().getIdentifier("btn1","id",getPackageName())

5 Comments

+1 Now if i want to open Activities like this how can i do it. i mean startActivity("Activity"+i).. in a for looop ....is it possible
The same way: Class [] classes = new Class [] {Activity1.class, Activity2.class, ...};
getResources().getClasses("btn1","id",getPackageName()) .. any other method like this..
No, you can't use this. It's only for resource identifiers.
Any other way like getting Classes or get classes under a PackageName
2

Try this way,hope this will help you to solve your problem.

main.xm

    <LinearLayout
        android:id="@+id/lnrMain"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <Button
            android:id="@+id/button3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <Button
            android:id="@+id/button4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <Button
            android:id="@+id/button5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <Button
            android:id="@+id/button6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <Button
            android:id="@+id/button7"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <Button
            android:id="@+id/button8"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <Button
            android:id="@+id/button9"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <Button
            android:id="@+id/button10"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    </LinearLayout>
</LinearLayout>

MainActvity.java

public class MainActivity extends Activity {

    private LinearLayout lnrMain;
    private Button[] arrayButton;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lnrMain = (LinearLayout) findViewById(R.id.lnrMain);
        arrayButton=new Button[10];
        init();
    }

    private void init()
    {
        for(int i=0;i<lnrMain.getChildCount() ; i++)
        {
            arrayButton[i] = (Button) lnrMain.getChildAt(i);
            arrayButton[i].setText("Hello:"+(i+1));
        }
    }

}

Comments

0

You can create a helper void like:

public static int getResourceID(Context mContext, String StrId) {
    return mContext.getResources().getIdentifier(StrId, "id", mContext.getPackageName());
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.