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

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.

Related

How to Set Id in EditText programmatically in Android

I have this button on GridLayout called addnewTask. When you create this button, it will create an EditText.
private GridLayout gridLayout;
int rowIndex = 3;
int colIndex = 1;
int i=0;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_set_task);
gridLayout = (GridLayout) findViewById(R.id.taskLayout);
}
This function to create EditText when the button is clicked -->
public void addView(View view) {
i++;
String tname = "task" + Integer.toString(i);
EditText editText = new EditText(this);
GridLayout.LayoutParams param = new GridLayout.LayoutParams();
param.height = ViewGroup.LayoutParams.WRAP_CONTENT;
param.width = GridLayout.LayoutParams.MATCH_PARENT;
param.rowSpec = GridLayout.spec(rowIndex);
param.columnSpec = GridLayout.spec(colIndex);
editText.setLayoutParams(param);
if (rowIndex > 3) {
editText.setTag(tname);
}
gridLayout.addView(editText);
rowIndex++;
}
My problem is that i want to set the android:id of EditText i created.
like this: When the button is clicked, EditText is created, in row 3, column 1 and id name task1.
When the button is clicked again, another EditText is created, in row 4, column 1 and id name task2.
When the button is clicked again, another EditText is created, in row 5, column 1 and id name task3.
ANS SO ON.....
Ids in android aren't strings - they are always numbers. Even if you write in xml #+id/textId, a number is generated for this text. You can see that in your R file.
What you can do is assign id to your edit texts by using editText.setId(int) method. If you want to be able to easily refer to the edit texts, you can either:
assign the ids sequentially: 1, then 2, 3 etc. Then id of the item would be (row-1) * <columnsCount> + column) (so if you have 3 columns, then second item in fifth row would have id 4 * 3 + 2)
create a map field of type Map<String, Integer>, and again assigns ids sequentially, and save them in.
String tname = "task" + Integer.toString(i);
EditText editText = new EditText(this);
editText.setId(i);
idsMap.put(tname, i);
You then get edittext's id by calling idsMap.get("task3")
Third option is to just keep reference to your EditText in a map: you'd then have a Map<String, EditText> map, and then call
String tname = "task" + Integer.toString(i);
EditText editText = new EditText(this);
editTextsMap.put(tname, editText);
You can keep references of these edit text in an array representing cells of your grid.
declare arraylist like this:
ArrayList<EditText> etArray = new ArrayList<>();
and keep reference to your EditText in array list at the end of your addView method like this:
etArray.add(i,edittext);
now refer these view like this:
etArray.get(i);
this way you will be able to refer them for accessing text.
assigning ids dynamically can cause problems as id is an integer and your assigned ids may cause conflict with system assigned ids to other components.
You can't set id as a String. You can only assign integer as Id. But if you want to use String as id for the ease of use then - in res/values/ids.xml file
<item name="edit_text_hello" type="id"/>
And then use it as:
edText.setId(R.id.edit_text_hello);
So you can do what you need.

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));
?

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 eclipse random string array for setText?

If I understand correctly
Random ran = new Random();
String[] ButtonText = null;
Resources res = getResources();
ButtonText = res.getStringArray(R.array.ButtonText_array);
String strRandom = ButtonText[ran.nextInt(ButtonText.length)];
System.out.println("Random string is : "+strRandom);
Is a way to take my string-array items and put them in random order and now I'm wanting to setText of several buttons with individual items from the strRandom. The following is for the setText of a button
Button gm1 = (Button) findViewById(R.id.gm1);
gm1.setText();
But I dont know how to put in the strRandom items into the setText part and since I dont need it displaying what do I need to alter here.
System.out.println("Random string is : "+strRandom);
I really am not understanding the question...
If you're just asking how to set the text to a random string, do it just as you did with the println() statement,
gm1.setText(strRandom);
or
gm1.setText(ButtonText[ran.nextInt(ButtonText.length)]);
Just a side note: by convention, variables are done in camelCase, reserve AllCaps for class names. (e.g. ButtonText should be buttonText). You'll notice the SO formatter formats ButtonText as if it were a class, not an array.
gm1.setText((CharSequence)("Random string is : " + strRandom));
You need to cast from String to CharSequence

android dynamical binding

I want to work dynamically therefore I want to bind text views dynamically I think an example would explain me the best
assuming I want to bind 7 image views i can do it like this :
Country = (EditText)findViewById(R.id.CountryEditText);
City = (EditText)findViewById(R.id.CityEditText);
LivinigCreture = (EditText)findViewById(R.id.LivingCretureE);
Nature =(EditText)findViewById(R.id.NatureEditText);
Inanimate = (EditText)findViewById(R.id.InanimateEditText);
KnowenPersonality = (EditText)findViewById(R.id.KnowenPersonalityEditText);
Occupation = (EditText)findViewById(R.id.OccupationEditText);
but lets change 7 with NUMOFFILEDS as a final where i want to do the previous ?
myImages = new ImageView [7];
for (int i = 0; i<7;i++,????)
myImages[i] = (ImageView)findViewById(R.id.initialImageView01);
notice : in my R file the R.id.initialImageView01 - R.id.initialImageView07 are not generate in a cont gap between them therefore I don't know how to make this architecture possible .
and if there's a way can someone show me an example how to work dynmiclly (like using jsp on android combined way or something ?)
id its possiable to do so constant times is it possible to build an the same xml constant num of times like jsp does
thank u pep:)
You can store the IDs themselves in an array at the beginning of your Activity; that way you'll only need to write them once and you can index them afterwards.
Something like:
int[] initialImageViewIds = {
R.id.CountryEditText,
R.id.CityEditText,
R.id.LivingCretureE,
R.id.NatureEditText,
R.id.InanimateEditText,
R.id.KnowenPersonalityEditText,
R.id.OccupationEditText
};
Then you can access them with:
myImages = new ImageView [7];
for (int i = 0; i<7;i++) {
myImages[i] = (ImageView)findViewById(initialImageViewIds[i]);
}
If that's not enough and you really want to get the IDs dynamically, I suppose you can use reflection on the R.id class, possibly with something like R.id.getClass().getFields() and iterate on the fields to check if their names interest you. Check reference for the Class class, too.

Categories

Resources