5

The content of xml are

<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/MainFrame"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<AbsoluteLayout
    android:id="@+id/AbsoluteLayout1"
    android:layout_width="match_parent"
    android:layout_height="172dp"
    android:layout_x="12dp"
    android:layout_y="26dp"
    android:visibility="invisible" >

</AbsoluteLayout>

  <AbsoluteLayout
    android:id="@+id/AbsoluteLayout2"
    android:layout_width="match_parent"
    android:layout_height="172dp"
    android:layout_x="20dp"
    android:layout_y="184dp" android:visibility="invisible">

</AbsoluteLayout>

</AbsoluteLayout>

Here's the main code

String layoutid;
int ctr = 1;
AbsoluteLayout [] mainlayout = new AbsoluteLayout[12];

   while (ctr<3)
   {
        layoutid = "AbsoluteLayout" + ctr;
        mainlayout[ctr] = (AbsoluteLayout)findViewById(R.id.layoutid);
        ctr++;
   }

We need to make a loop to make

 ctr = 1 
 AbsoluteLayout + ctr = AbsoluteLayout1 
 ctr++; 

 AbsoluteLayout + ctr = AbsoluteLayout2

we want to declare the AbsoluteLayout1 and AbsouluteLayout2 but it doesn't work. We know that the R.id.layoutid is the culprit. So how can we resolve it?

Marc Quebrar Tan
  • 497
  • 2
  • 6
  • 19
  • R.id.xxx is an int value not string to manipulate like this k – Athul Harikumar Sep 01 '12 at 11:32
  • Do all the layouts need to have different ids? It is fine to use the same id for multiple layouts as long as you put just one of them in the view-hierachy. And you could also think about generating the layouts programmatically instead of finding them inside the xml generated layout. Besides that: `AbsoluteLayout` is discouraged to use [since ages](https://groups.google.com/forum/?fromgroups=#!topic/android-developers/QA-Wax5nHpQ) since absolute layouts don't work for variable screen sizes of Android devices. – zapl Sep 01 '12 at 13:27
  • `AbsoluteLayout` has been deprecated for nearly four years. Nobody should be using it. – CommonsWare Sep 01 '12 at 13:38

2 Answers2

17

I solved it using getIdentifier method

Button[] buttons; 
for(int i=0; i<buttons.length; i++) {
{
   String buttonID = "sound" + (i+1);

   int resID = getResources().getIdentifier(buttonID, "id", getPackageName());
   buttons[i] = ((Button) findViewById(resID));
   buttons[i].setOnClickListener(this);
}
Marc Quebrar Tan
  • 497
  • 2
  • 6
  • 19
0

the id is an intiger value not string: hope u ll get an idea from below code

 while (ctr<3)
{
   int layoutid;
    if(ctr==1)
    { layoutid = R.id.AbsoluteLayout1;}
    else{
     layoutid = R.id.AbsoluteLayout2;}
    mainlayout[ctr] = (AbsoluteLayout)findViewById(layoutid);
    ctr++;
}
---------------------------------------------

all these won post an error as they are handled as int itself manipulate as you want you cant use this as it is as

int[] ctra={R.id.xx,R.id.xxx};
  int i=0;
 while (ctr<3)
{
mainlayout[i]=(AbsoluteLayout)findViewById(ctra[i]);
i++;}
Athul Harikumar
  • 2,501
  • 18
  • 16
  • We need to make a loop to make: ctr = 1 AbsoluteLayout + ctr = AbsoluteLayout1 ctr++; AbsoluteLayout + ctr = AbsoluteLayout2 – Marc Quebrar Tan Sep 01 '12 at 11:44
  • please understand id is an int value not string, you ll have to use switch case or similar to switch the layout or else you can store all the layout id in an int array and set mainlayout[ctr] – Athul Harikumar Sep 01 '12 at 11:49
  • Actually we have 50 absolute layouts. So in your procedure we will have to declare 50 if else statements? – Marc Quebrar Tan Sep 01 '12 at 11:57
  • check the second code but you will have to add all id's in the array there is no other way out (its done so as you dont bump into id not found) – Athul Harikumar Sep 01 '12 at 12:01
  • so if you have 1000 layouts, you're going to declare it one by one? – Marc Quebrar Tan Sep 01 '12 at 16:33
  • of course all the id s are integers referring to location and you ll have to atleast put them in an array so you can call them -- int[] ctra={R.id.xx,R.id.xxx}; here-- – Athul Harikumar Sep 03 '12 at 04:40