I have an 5 buttons and I need to explicitly create each editText. I found a solution by using for loop we can group the buttons. like below.
private Button[] btn = new Button[4];
private int[] btn_id = {R.id.btn0, R.id.btn1, R.id.btn2, R.id.btn3};
for(int i = 0; i < btn.length; i++){
btn[i] = (Button) findViewById(btn_id[i]);
btn[i].setBackgroundColor(Color.rgb(207, 207, 207));
btn[i].setOnClickListener(this);
}
Here I need to use the same button only. How can I use the above code for the different button names?
private Button btn1,button2,bt3,b4;
How can I achieve the for loop for the above button declaration. I tried to add the all the button in to an arrayList bu that doesn't work. Any suggestions.
You can try this way, Hope this help you!
for(int i = 0; i < btn.length; i++){
int btnId= mContxt.getResources().getIdentifier("btn_"+i, "id", getPackageName());
Button btn = (Button)findViewById(buttonId);
btn.setBackgroundColor(Color.rgb(207, 207, 207));
btn.setOnClickListener(this);
}
After the for-loop you have this array filled with Buttons,
private Button[] btn = new Button[4];
Then you can just do this, after the for-loop
private Button btn1 = btn[0];
private Button btn2 = btn[1];
private Button btn3 = btn[2];
private Button btn4 = btn[3];
You can do this in a better way, but I hope this will give you a small indication on how to do it.
Related
I will start off with this is my first post on StackOverflow, so I'm sorry if i leave anything important out, i am also very new at coding and still learning.
What i am trying to is make a form of a memory game, a user will input a number, it will generate that number of tiles (even only) and give them set values. I am stuck on the giving them 2 set values each and only two for x number of tiles.
An example would be if the user choose the lowest number of tiles, 4, there would be two with A and two with B. The code i have right now is a mess but kind of a draft and it is setting text on a button, not a value. This code is also only for the 4 tiles example.
On a side note, I'm sure there is a simpler way to declare all 20 of my buttons, maybe someone could point me in the right direction.
#Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.second_layout);
Button btn1 = (Button)findViewById(R.id.btn1);
Button btn2 = (Button)findViewById(R.id.btn2);
Button btn3 = (Button)findViewById(R.id.btn3);
Button btn4 = (Button)findViewById(R.id.btn4);
Button btn5 = (Button)findViewById(R.id.btn5);
Button btn6 = (Button)findViewById(R.id.btn6);
Button btn7 = (Button)findViewById(R.id.btn7);
Button btn8 = (Button)findViewById(R.id.btn8);
Button btn9 = (Button)findViewById(R.id.btn9);
Button btn10 = (Button)findViewById(R.id.btn10);
Button btn11 = (Button)findViewById(R.id.btn11);
Button btn12 = (Button)findViewById(R.id.btn12);
Button btn13 = (Button)findViewById(R.id.btn13);
Button btn14 = (Button)findViewById(R.id.btn14);
Button btn15 = (Button)findViewById(R.id.btn15);
Button btn16 = (Button)findViewById(R.id.btn16);
Button btn17 = (Button)findViewById(R.id.btn17);
Button btn18 = (Button)findViewById(R.id.btn18);
Button btn19 = (Button)findViewById(R.id.btn19);
Button btn20 = (Button)findViewById(R.id.btn20);
int tiles = 0;
super.onCreate(savedInstanceState);
TextView tv = (TextView)findViewById(R.id.calling_activity_info_text_view);
String tileNum = getIntent().getExtras().getString("tilesNumber");
String[] tileSet = {"A","B","C","D","E","F","G","H","I","J"};
Button[] btnA = {btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9, btn10, btn11, btn12, btn13
, btn14, btn15, btn16, btn17, btn18, btn19, btn20};
if(tileNum.equals("4")) {
tiles = Integer.parseInt(tileNum);
tv.setText("" + tiles);
for(int i = 0; i <= 3; i++){
btnA[i].setVisibility(View.VISIBLE);
btnA[i].setText(tileSet[i]);
}
How about thinking it backwards?
Take item from tileSet;
Set any two buttons with the item you just took;
Remove the buttons you set from btnA so you don't repeat them;
Rinse and repeat until you filled all the buttons defined by "tileNumber".
PS: is there any reason why "tileNumber" should be a string? You can add an integer to an intent just as easily.
PS2: You could add the buttons programatically inside a for loop instead of instantiating every single one of them, which would increase scalability.
EDIT:
First of all, to make this work, you'd probably need to replace your button array by an ArrayList so it is easier to remove elements. The code would look something like this:
Random random = new Random();
for (int i = 0; i < tiles/2; i++) {
for (int j = 0; j < 2; j++) {
//Generates random number in the range [0, number of buttons)
int buttonIndex = random.nextInt(btnA.length());
// Get button from array
btnA.get(buttonIndex).setText(tileSet[i]);
//Remove button from button list
btnA.remove(buttonIndex);
}
}
Of course this code is not optimized nor saves the reference for the button click, but let me know if it sheds some light into solving the problem.
I try to store some button into a array like this:
Button Intro,Product;
Button[]toogleButtons={Intro,Product};
private int[] ToogleButtonID = { R.id.tab2_info_intro,R.id.tab2_info_product };
after this I initialize the button :
private void iniToogleButton() {
for (int i = 0; i < toogleButtons.length; i++) {
toogleButtons[i] = (Button) findViewById(ToogleButtonID[i]);
toogleButtons[i].setOnClickListener(new View.OnClickListener() {}}
Intro.setBackgroundColor(Color.RED);
}
And it get NullPointerException on Intro.setBackgroundColor();
Seen like I cant store those button and initialize with an array.
Any Idea or good way to make it posible?
use
toogleButtons[i].setBackgroundColor(Color.RED);
instead of
Intro.setBackgroundColor(Color.RED);
to change Button Background color because you are not initializing Intro Button instance before calling setBackgroundColor
You need to initialize Intro before calling setBackgroundColor(Color.RED).
Rename toogleButtons to buttons like this,
buttons[i] = (Button) findViewById(ToogleButtonID[i]);
buttons[i].setOnClickListener(new View.OnClickListener() {}}
Intro.setBackgroundColor(Color.RED);
I have 4 button seen below sequentially numbered in my layout file. In my code i want to get in a loop the advert that corresponds to the position we are in the loop e.g for the second time around in the loop i would get advert2 back.
Can anyone help. Thanks
Button advert1 = (Button) findViewById(R.id.advert1);
Button advert2 = (Button) findViewById(R.id.advert2);
Button advert3 = (Button) findViewById(R.id.advert3);
Button advert4 = (Button) findViewById(R.id.advert4);
Button[] btnArr= new Button[4];
int[] arr = new int[4];
arr[0]=R.id.advert1;
arr[1]=R.id.advert2;
arr[2]=R.id.advert3;
arr[3]=R.id.advert4;
for(int i=0;i<4+i++)
{
btnArr[i]=(Button) findViewById(arr[i]);
}
I created nine Button view in the main.xml and named it to Button1, Button2... Button9.. Then in my code I created an ArrayList of Button and its obvious that this array of buttons will hold my Button in the main.xml and in my current knowledge that to get the Button in my main.xml I need to use the findViewById method and basically I need to loop the array to get the button
ArrayList<Button> buttons;
int MAXBTN = 9;
for( int i = 0; i < BTN; i++ )
{
// Code here to use the findViewById method to get the button
}
And my problem is that I need to pass R.id.Button1 .. R.id.Button3 to the findViewById method, but I need to loop this. Is there anyway I can pass a counter in the findViewById while loop?
Please advise.
You can try this.
ArrayList<Button> buttons;
int MAXBTN = 9;
for( int i = 0; i < BTN; i++ )
{
String buttonID = "Button" + i+1 ;
int resID = getResources().getIdentifier(buttonID, "id", "packageName");
buttons.get(i) = ((Button) findViewById(resID));
}
I think there is already a same question asked for this check this
Its always better to search first before asking
private final Button[] BUTTONS = {
btn1, btn2, btn3,btn4
};
...
btn1 = (Button) this.findViewById(R.id.btn_1);
btn2 = (Button) this.findViewById(R.id.btn_2);
btn3 = (Button) this.findViewById(R.id.btn_3);
btn4 = (Button) this.findViewById(R.id.btn_4);
...
int n = BUTTONS.length;
for(int i=0; i<n; i++) {
if(DEBUG) Log.d(TAG, String.valueOf(i));
BUTTONS[i].setOnClickListener(this);
}
throws NullPointerException, whereas
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
btn3.setOnClickListener(this);
btn4.setOnClickListener(this);
works fine. Doesn't make any sense to me.
I think it's because your Buttons array is created when btn1,... are still null.
So when you call BUTTONS[i].setOnClickListener in the loop you are really saying null.setOnClickListener which will give an exception.
Try setting up the array as a variable and sey AFTER you've assigned btn1, etc.
Haven't tested it but something like this might work better...
private ArrayList mBtns = new ArrayList();
private void initButton(int id) {
button = (Button) findViewById(id);
button.setOnClickListener(this);
mBtns.add(button);
}
...
initButton(R.id.btn_1);
initButton(R.id.btn_2);
initButton(R.id.btn_3);
initButton(R.id.btn_4);
Also unless the buttons do very similar things you may find it better to simply define the onClick attribute on each in the layout and save yourself A LOT of coding (only available in Android 1.6 and higher).