Android findViewByID programmatically - android

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]);
}

Related

can LinearLayout put two items on the same line?

I have the code as below, and I need 2 things:
I wish to put tv[i] on the same line as txt[i]
and I want keyboard to appear after touching tv[i].
I'm a complete beginner to Android. Thank you.
public void click2(View view) {
Button button3 = findViewById(R.id.button2);
button3.setText("hello");
// Button[] buttons = new Button[10](this);
/*
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.activity_main);
Button txt1 = new Button(this);
// linearLayout.setBackgroundColor(Color.TRANSPARENT);
linearLayout.addView(txt1);
*/
Integer.toBinaryString(12);
// Put buttons into an array
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.activity_main);
// GridLayout gridLayout=(GridLayout) findViewById(R.id.activity_main);
// Put buttons into an array
// Button[] txt = {new Button(this), new Button(this)};
Button[] txt = new Button[8];
TextView[] tv = new TextView[8];
// loop over all values of i between 0 to the end of the button array
for (int i = 0; i < txt.length; i = i + 1) {
// Access array elements by index so txt1 is txt[1], etc.
txt[i]=new Button(this);
txt[i].setText(Integer.toBinaryString(i));
linearLayout.addView(txt[i]);
tv[i]=new TextView(this);
linearLayout.addView(tv[i]);
}
};
MAYBE I'm putting your line
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
at a wrong place.
KOD MainActivity.java
public void click2(View view) {
Button button3 = findViewById(R.id.button2);
button3.setText("hello");
// Put buttons into an array
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.activity_main);
// GridLayout gridLayout=(GridLayout) findViewById(R.id.activity_main);
// Put buttons into an array
// Button[] txt = {new Button(this), new Button(this)};
Button[] txt = new Button[8];
TextView[] tv = new TextView[8];
// loop over all values of i between 0 to the end of the button array
for (int i = 0; i < txt.length; i = i + 1) {
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
// Access array elements by index so txt1 is txt[1], etc.
txt[i]=new Button(this);
txt[i].setText(Integer.toBinaryString(i));
linearLayout.addView(txt[i]);
tv[i]=new TextView(this);
linearLayout.addView(tv[i]);
}
};
Call
linearLayout.setOrientation(LinearLayout.VERTICAL);
to make 2 items appear side by side.
For showing keyboard, take a look here:
How do you close/hide the Android soft keyboard programmatically?
Looks like you are referencing your xml file here:
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.activity_main);
You should go to your xml file, add a id (android:id="#+id/some_id") tag to your <LinearLayout> and use this id in your LinearLayout linearLayout =(LinearLayout) findViewById(R.id.HERE)
Other option is to go to your xml file and add sandroid:orientation="horizontal" like this:
<LinearLayout
android:id="#+id/some_id"
android:orientation="vertical"
...>

assign different button names to id using for loop

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.

Trouble figuring out how to generate set values only twice for a set amount of times

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.

Removing Layout on a button clilck

I am working on android.And i was creating an application in which i dynamically created a group of buttons on a button click.But what happens is buttons are created every time i click the button.i want this to happen once.Can i clear the space just at the beginning of OnClickListener()? i dunno how to do it.Below is my code.
button_generate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//*-----------clearing the variables--------*
final TableLayout rel=(TableLayout)findViewById(R.id.tab1);
int k=0,i=0,j=0;
count=3;
System.out.println("count is"+count);
while(j<count)
{
TableRow tr = new TableRow(EspeakerActivity.this);
//for(int c=1; c<=3; c++) {
Button b1 = new Button (EspeakerActivity.this);
Button b2= new Button (EspeakerActivity.this);
Button b3 = new Button (EspeakerActivity.this);
b1.setText("1");
b2.setText("2");
b3.setText("3");
b1.setTextSize(10.0f);
200));
tr.addView(b1, 100,50);
tr.addView(b2, 100,50);
tr.addView(b3, 100,50);
rel.addView(tr);
}
j++;
}
}
}
);//*--------closing the button click------*
Give the buttons ID's and then use findViewByID() to see if the buttons already exist before adding them.
TableRow tr = new TableRow(EspeakerActivity.this);
tr.setId(/*some id*/)
//for(int c=1; c<=3; c++) {
Button b1 = new Button (EspeakerActivity.this);
Button b2= new Button (EspeakerActivity.this);
Button b3 = new Button (EspeakerActivity.this);
b1.setText("1");
b2.setText("2");
b3.setText("3");
b1.setTextSize(10.0f);
200));
tr.addView(b1, 100,50);
tr.addView(b2, 100,50);
tr.addView(b3, 100,50);
//check rel already has that tr.. if it has don't add anythin.. better if you do it before even creating the TAbleRow
rel.addView(tr);
Hiding linear layout on runtime in Android
you can refer the link and i am sure you will get your ans.if not solve through this link
then tell me.
You can clear your TableLayout on each button click as follows
....
onClick(View v) {
final TableLayout rel=(TableLayout)findViewById(R.id.tab1);
for(int x=0; x<rel.getChildCount(); x++)
{
View v = rel.getChildAt(x);
rel.removeView(v);
}
....
}

setOnClickListener throws NullPointerException ONLY inside for loop. Why?

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).

Categories

Resources