setting text in an array of buttons - android

I'm trying to create a simple application in which there are 30 buttons and I need to initialize their text field.
I created this array of buttons:
Button[][] buttons_arr = new Button[10][3];
To change each button's text I did :
for(i=0..9) //psaudo
for (j=0..29) //psaudo
buttons_arr[i][j].setText(toString(some_int));
The last line is causing some problems. Why and what can I do to solve this issue ?

You are actually looping in 300 times instead of 30 times

try like this
for(i=0..9) //psaudo
for (j=0..2) //psaudo
buttons_arr[i][j].setText(""+some_int);

Try this:
Button[][] b=new Button[10][3];
for(int i=0;i<10;i++)
{
for(int j=0;j<3;j++)
{
b[i][j]=new Button(context);
b[i][j].setText("something");
}
}

I have not tried 2D arrays. But my experience with a similar problem seems to be that buttons_arr[i][j] is still uninitialized. You need to either create a new button:
buttons_arr[1][1] = new Button();
or
buttons_arr[1][1] = (Button)findViewById(R.id.buttonAtPosition1_1);

try this
for(int i=0;i<10;i++){
for(int j=0;j<3;j++)
buttons_arr[i][j].setText(your text);
}

Related

How can I change ImageButton size programmatically?

I'm generating ImageButtons programmatically and I'm not able to change the size of the items using XML. So, I would like to do it programmatically too.
How can I do that?
My code :
String[] separated = result.split("%");
ImageButton [] txt =new ImageButton[20];
for(int i=0;i<txt.length;i++)
{
String getname = separated[i];
txt[i]=new ImageButton(hphotos.this);
Picasso.with(getBaseContext()).load("http://www.mywebsite.com/" + getname).into(txt[i]);
txt[i].setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
scroll.addView(txt[i]);
Have you tried
yourView.setLayoutParams(new LayoutParams(width,height));
?

Dynamically changing TextViews with a for loop

I am trying to change the values of several TextView elements but iterating through an array list and adding these values. However I can't seem to find a way to change the R.id value that is used each time. For example:
for (int i=0; i<arrayList.size(); i++)
{
TextView t = (TextView) dialog.findViewById(R.id.value+(i));
t.setText(arrayList.get(i));
}
Where the values are in the format animals_eng1,animals_eng2 etc..
Any help appreciated.
Your best bet is to create an array containing the resource IDs of each text view and looping through them..
ex.
int[] textViewIds = new int[] { R.id.animals_eng1, R.id.animals_eng2, R.id.animals_eng3 };
Then in your activity you can loop through them and set the values like you desire
for (int i=0; i<arrayList.size(); i++)
{
((TextView)findViewById(textViewIds[i])).setText(arrayList.get(i));
}
You'll have to make sure your arrayList size is the same as the number of textview resource IDs you set or you'll end up with an out of bounds exception when looping
I don't know of a way to do exactly what you're asking, but here are two alternatives:
1.Create an array of Integers, and assign each element of the array to a different view id value
int[] ids = new int[arrayList.size()];
ids[0] = R.id.view0;
ids[1] = R.id.view1;
ids[2] = R.id.view2;
//...ids[n] = R.id.viewN; where n goes up to arrayList.size()
for (int i : ids){
((TextView)dialog.findViewById(ids[i])).setText(arrayList.get(i));
}
Note that the above method sorta defeats the point because you have to have a line for every TextView, if you want something more dynamic:
2.Tag your TextViews in your layout xml by adding android:tag="prefix0", for example, to each of your TextViews. Before the loop find the parent View of your layout, and then use the findViewWithTag method of that view within the for loop. From your code I'm guessing you're using a Dialog with a custom layout xml, so you'd first find the parent of that:
ViewGroup parent = dialog.findViewById(R.id.parent); //or whatever the name of your parent LinearLayout/RelativeLayout/whatever is
String commonPrefix = "prefix"; //or whatever you've tagged your views with
for (int i=0; i<arrayList.size(); i++){
TextView t = (TextView) parent.findViewWithTag(commonPrefix+i);
t.setText(arrayList.get(i));
}
One way to solve this would be putting your resource ids in an int array and getting the resource at index i.
TextView t = (TextView) dialog.findViewById(R.id.value+(i))
becomes
TextView t = (TextView) dialog.findViewById(resourceArray[i])
if all of your views are on the same container (and only them) , simply iterate over its children .
if not , you can add an array of ids (in res) and iterate over it .

How to assign values from an array to textviews in android

I have 4 textview such as t11,t12,t13,t14 and I have also 4 value in array val[4].
I want to store these values randomly in textviews. but I am getting little problem.
I have done following code:
TextView t11,t12,t13,t14;
Random r = new Random();
for (int i = 0; i < val.length; i++) {
int val[4]=r.nextInt(10);
Log.d("horror", "Randm Array of VAL:" +val[i]);
}
In the Log,there are 4 values displayed but how to display them in textviews.
I have coded but it does not work properly.
t1[i+1].setText("" +val[i]);
and
In this case,values are properly displayed, but i want to do code optimization.
t11.setText("" +val[0]);
t12.setText("" +val[1]);
t13.setText("" +val[2]);
t14.setText("" +val[3]);
Thanks in advance.
Every time you loop in for, you create another integer array. Take the definition of val out of the for loop.
you can store their references inside an array , it won't create new objects. so this should do the job
TextView [] textviews = {t11,t12,t13,t14};
for(int i =0;i<textviews.length;++i){
textviews[i].setText(val[i]);
}
For your TextView use something like,
TextView [] tv = {t11,t12,t13,t14};
and use tv for other going stuff... So now, you can getting it work by,
tv[i+1].setText("" +val[i]);

Android fill array with ImageViews

I want an array of imageviews, but I don't know how to fill it with an object of type imageview.
ImageView[] forAdapter = new ImageView[imageIds.size()];
for(int i = 0; i < imageIds.size(); i++)
{
ImageView mImageView = new ImageView(context);
forAdapter[i] = mImageView.setImageDrawable(((imagesPulled.get(imageIds.get(i)))));
}
this doesn't work because .setImageDrawable does not return an imageview, and I dont know of any object that actually does!
I considered using drawables in an array, but I'm ultimately setting an arrayadapter for a listview, and I cant make a R.layout with drawables (I'll get a class cast exception because the xml file is using an ImageView not drawable type),so I only have ImageViews to put into an array - unless I'm approaching the problem wrong.
Your code is almost there! I'm just gonna make a small change and comment on it below:
ImageView[] forAdapter = new ImageView[imageIds.size()];
for(int i = 0; i < imageIds.size(); i++)
{
forAdapter[i] = new ImageView(context);
forAdapter[i].setImageDrawable(imagesPulled.get(imageIds.get(i)));
}
First: If you want to initialize forAdapter[i], you don't need to create a new variable and assign it to it. Just do it there:
forAdapter[i] = new ImageView(context);
Setting the image (using setImageDrawable, setImageResource, etc) doesn't return anything. It's an operation you do on the image itself so all you gotta do is call the method from the variable you want to modify:
forAdapter[i].setImageDrawable(imagesPulled.get(imageIds.get(i)));
You're done :)
If you have any doubts just ask in the comments.
Hope it helps.
Your mImageView is ImageView itself.
So, forAdapter[i] = mImageView should work fine. If you use additional actions, you can do them before assignment.

How to reference an array of EditText created in main.xml

My app is getting a little bloated, I would like to 'compress' some code by changing some of my variables into arrays and using some for loops to process my variables.
My first target is initializing my EditText boxes
My original code (working fine) included:
EditText miBox1, spBox1 ;
EditText miBox2, spBox2 ;
...
miBox1 = (EditText)this.findViewById(R.id.miBox1);
spBox1 = (EditText)this.findViewById(R.id.spBox1);
miBox2 = (EditText)this.findViewById(R.id.miBox2);
spBox2 = (EditText)this.findViewById(R.id.spBox2);
I actually have twenty mi boxes and 20 sp boxes to setup
I am trying to create an array, and loop through them
In my main.xml I have renamed my EditText boxes to
miBox[1] spBox[1], etc.
I'm declaring in my activity with:
EditText[] miBox = new EditText[20] ;
EditText[] spBox = new EditText[20] ;
and putting this into a for loop
miBox[i] = (EditText)this.findViewById(R.id.miBox[i]);
spBox[i] = (EditText)this.findViewById(R.id.spBox[i]);
but Eclipse cannot resolve R.id.miBox[i]
How do I properly create an array of EditText in my xml so that
it is recognized?
Thanks
JD
int resID = getResources().getIdentifier("miBox" + i,
"id", getPackageName());
where i is your index
EditText editText = (EditText)findViewById(resId);
if you want to put it in an array, simply do this :
miBox[i] = (EditText)findViewById(resId);
the problem here is you have wrongly modified a few things.
the code should be like:
EditText[] miBox = new EditText[20] ;
EditText[] spBox = new EditText[20] ;
and putting this into a for loop
miBox[i] = (EditText)this.findViewById(R.id.miBox1);
spBox[i] = (EditText)this.findViewById(R.id.spBox1);
Here, the msitake is using miBox[i] since thats not the id you have used in your layout xml.
hope this helps.

Categories

Resources