Get the ids program code in a for loop programmatically?

I just want to know. is there any way to create an id using for loop

I have 10 buttons in xml. there id button1,button2,button3... button10 Now I create a Button Array in the java class and do it like

 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 I can do this kind of work. Getting all the buttons used for the loop to make my work faster and for a while when I want to set the text of all the buttons.

+5
source share
4 answers

use getResources().getIdentifier as

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

i.e. just rewrite the 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 just you can 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()); } 
+2
source

If you already know all the identifiers, 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 do not know them, use:

 getResources().getIdentifier("btn1","id",getPackageName()) 
+2
source

Try this way, hope it helps you 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)); } } } 
+2
source
 Try this , this example is for textview your can customize it according to your requirement public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); rr = new RelativeLayout(this); b1 = new Button(this); b1.setId(R.id.Button01); recent = b1; b1.setText("Click me"); rr.addView(b1); setContentView(rr); b1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { tv1 = new TextView(can.this); tv1.setId((int)System.currentTimeMillis()); lp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); lp.addRule(RelativeLayout.BELOW, recent.getId()); tv1.setText("Time: "+System.currentTimeMillis()); rr.addView(tv1, lp); recent = tv1; } }); } 
0
source

Source: https://habr.com/ru/post/1200088/


All Articles