Hey I am making an android app that will have ~256 buttons.
Because I dont want to write the very same code for everyone of these I thought it might be possible to realize an easier solution via arrays. My approach in the onCreate to set the listeners was:
1 for (int i=1; i<32; i++)
2 {
3 button[i] = (Button)findViewById(R.id.button[i]);
4 button[i].setOnTouchListener(this);
5 }
I set the Button[] like that: Button[] button=new Button [64];
Now, eclipse tells me in line 3 "button cannot be resolved or is not a field" and it just underlines the word "button", so I think it ignores/just does not recognize the [i] (array)-stuff.
The rest of my code seems to get on with that perfectly because it gets recognized as an object (correct me if I said that wrong) but the findViewById() doesn't get on with it ..
Thanks for the replies, Alex
You can't do what you proposed in your solution. A better way to go about it is to add the buttons dynamically in code. For instance,
View parentView = (LinearLayout) findViewById(R.id.parentView);
// declare button array above
for (int i=1; i<32; i++)
{
Button btn = new Button(context);
// EDIT: adding a background resource
btn.setBackgroundResource(R.layout.button_layout);
btn.setText("This is my text");
btn.setOnTouchListener(this);
button[i] = btn;
}
User "Horschtele" answered it in a perfect way but he deleted his answer on his own (don't know why).
Horschtele, if you read that, I just want to say that this solution is just perfect!
I have to (or at least I think I have to) do this for every tableRow but this saves me an infinite amount of time. Thanks again Horschtele (are you german? :))
My modified version of Horschtele's answer if you already have your buttons in a table:
ViewGroup container = (ViewGroup) findViewById(R.id.tableRow1);
for(int i=0; i<container.getChildCount();i++){
System.out.println(container.getChildCount());
Button button = (Button)container.getChildAt(i);
button.setOnTouchListener(this);
}
(don't wonder about the println, you can easily check if the system correctly recognizes the container you are refering to).
If you did it my way with an array of Button then this is the way to go:
button[i] = (Button)container.getChildAt(i);
button[i].setOnTouchListener(this);
Related
My problem is, i have to make a timetable. For that i made the raw table with the Views i need. For changing the subjects i made Spinners. For just looking at it i use TextViews. In total there are over 100 Views. I don't want to write ervery Id in an Array for calling them. So i want to do that programaticly. How do i call them?
I tried using findViewByTag() but it could't resolve it. I asumed then, that it is not working or there no explanation for it.
Also i have tried to call them in a for loop
for (int i =1; i <100; i++){
String ViewGetter = "R.id.View"+i;
View v = this.findViewById(ViewGetter)
}
logocaly it doestn't work, because findViewById() needs an integer. Is there a way i can call them like this?
Making a loop for getting the Id in an Array doesn't work because the Ids aren't for some reason cronological.
EDIT: as sugestet in the comment, that i could use reflections, i tried it. It hasn't worked. But it may be because i have never worked with reflections before. If this is the anwser. than i would be glad to have an explanation for it.
You can use getChildAt()
for (int i = 0; i < view.getChildCount(); i++) {
view.getChildAt(i)...
}
I am trying to add rows in a tableLayout dynamically with click events trigerring some action for each row. The data is dynamic because it is coming from the server.
I have come upon two different ways to use the dynamic approach and registering the click events.
For dynamic approach the easiest way would be to put the rowAdditon() as a dedicate function like
tl.addView(row1);
tl.addView(row2);
and to integrate click events i would require each TableRow object for onCLickListener().
But i am not able to find the best way to integrate them together.
First thing that comes to my mind is a ArrayList. Is there any other easy way to acheive this.
i tried to implement this like the following
ArrayList a1 = new ArrayList();
DataBaseReceiverForConnectToTeachers received=new DataBaseReceiverForConnectToTeachers(this);
int count=received.getNoOfData();
for(int i=0;i<count;i++){
a1.add("t"+i);
}
for(int i=0;i<count;i++){
TableRow a1.get(i)=new TableRow(this);
//getting error in this line
//cannot resolve method get(int)
//a1 is already define in the scope
}
The solution was creating a for loop that runs for the size of the items in database.
Why you want to put into table view , add the same in linear layout by creating it in xml and whenever you want to add button call a method and keep the id of this button static so that you can make every button on click method separately
Let me know if my answer completes your query else I can provide you code for this
I'm trying to export all widgets on screen to a text. To do that, I'm cycling through all widgets inside the RelativeLayout I have on screen. How do I make sure the view I'm currently looking at is the rightmost (or last) in that line?
for(int i = 0; i < relLayout.getChildCount(); i++) {
View view = relLayout.getChildAt(i);
(...)
}
And then I'm checking if the view I'm looking at is a TextView, EditText, Spinner, etc. So far, so good. But in order to append the new lines to the string, I'd like to programmatically check if that view is the last in that row. Is there a clear/simple way to do that (other than enumerating the id's of those widgets, which I'm doing right now? :D)
Thanks in advance!
PS1: For the purpose of the question, assume there are no compilation errors in the code. I just don't know how to achieve this.
Fixed, but in a curious way.
In my case, all the last fields on screen had Align Parent End flagged. So I found online this would be the way to do it:
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) view.getLayoutParams();
int[] rules = params.getRules();
if ((rules[RelativeLayout.ALIGN_PARENT_END] == RelativeLayout.TRUE)) {
// Do something about it
}
Where RelativeLayout.ALIGN_PARENT_END stands for 21, according to http://developer.android.com/reference/android/widget/RelativeLayout.html
However, my code was returning false for that check. So I started displaying the value for the property, and it really was false. I tried this for a while, then I saw one of my edittexts returned true for it. But the activity xml showed this
android:layout_alignParentRight="true"
in addition to alignParentEnd. Which didn't make sense, since alignParentRight is supposed to be rule 11, according to the same link above.
So I devised this code to expose which rules where true, and put it in my code:
for(int x = 0; x < 22; x++) {
if (rules[x] == RelativeLayout.TRUE) Log.i("Property true:", String.valueOf(x));
}
Which gave me 11 for all fields, and 11 and 21 for the field that responded TRUE before. Remember, all fields had android:layout_alignParentEnd="true" in the activity xml.
So to fix my code, I'm now checking rules[RelativeLayout.ALIGN_PARENT_RIGHT] and it works, but I strongly believe those two properties are switched somehow. Can anybody confirm? Or please englighten me on where I screwed up, which is more likely :)
I've made an RadioGroup and I'm adding RadioButtons to it all programmatically.
The problem is, when running I can check all options and cannot uncheck them.
Here is my code:
optionsContainer = new RadioGroup(_myActivity);
this.setId(IdDispencer.DispenceID());
for (int i=0; i < options.size(); i++){
RadioButton asw = new RadioButton(_myActivity);
asw.setText(options.get(i));
asw.setId(IdDispencer.DispenceID());
optionsContainer.addView(asw);
}
P.S. 1 - I've tried to setup an array to hold the radio buttons as showed here:
Android RadioGroup checks more than one RadioButton?
But with no success.
P.S. 2 - This is very weird, the behavior is inconsistent. Sometimes it works sometimes it doesn't.
On my emulator running 2.3.3 it show this behavior but on my SGS1 with CM9 it shows the problematic behavior all the time.
I had the same problem and the solution I found is to set an id to each radio button (I didn't do it at all).
In your code, it seems that you give at each button the same id. Maybe the problem is here.
I've got an app that dynamically adds radiobuttons from json data. I don't really know how to find out which ones are selected though. There are the radioButton.isSelected() and the radioGroup.getCheckedRadioButtonId() but since it's dynamically created I cannot use names for all the objects.
Is there a way to add them to a group upon creation and cycle through all of them later? I've got multiple radiogroups and not all of them but most are supposed to be checked.
I'm using api lvl 7 (2.1) and I'm fairly new to this. Please explain in detail.
You can use an ArrayList.
ArrayList<RadioButton> radioButtons = new ArrayList<RadioButton>();
//create your new button and add it here.
radioButtons.add(radioButton);
Then to iterate through the list if you need to retrieve each button, use a for loop:
for (int i=0;i<radioButtons.size();i++){
RadioButton button = radioButtons.get(i);
//do what you need with the button
}
And if you need each RadioButton to have certain data, then you could do:
radioButton.setTag("tag");
and then when you are iterating through the loop, you can do button.getTag();
Or if you just want to get the selected one:
RadioButton button = (RadioButton)findViewById(radioGroup.getCheckedRadioButtonId());
I think I covered everything from your question and maybe more. Let me know if I missed anything or need to give any additional explanation.