0

I want to get the id's of my imageviews in my ObjectsClass which are in level1.xml and I have inflated this layout in GamePlayActivity...MY ObjectClass is not activity then how to get array of id's in that class ...here is my level.xml say there are 15 ImageView I have just shown few ....

     <RelativeLayout  xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@drawable/gmw_01"
 android:onClick="onClick"
 android:layout_height="wrap_content" 
android:layout_width="wrap_content"
 android:id="@+id/relativeLayout1" >
 <ImageView 
android:onClick="objectClick" 
android:layout_height="wrap_content" 
android:layout_width="wrap_content" 
android:id="@+id/imageView1"
 android:src="@drawable/bb01" 
android:layout_marginLeft="998dp" 
android:layout_marginTop="593dp" 
android:layout_alignParentTop="true"
 android:layout_alignParentLeft="true"/>
 <ImageView 
android:onClick="objectClick"
 android:layout_height="wrap_content" 
android:layout_width="wrap_content"
 android:id="@+id/imageView2"
 android:src="@drawable/bb02"
 android:layout_marginLeft="20dp" 
android:layout_marginTop="39dp" 
android:layout_alignParentTop="true"
 android:layout_alignParentLeft="true"/>
 <ImageView
 android:onClick="objectClick"
 android:layout_height="wrap_content" 
android:layout_width="wrap_content"
 android:id="@+id/imageView3"
 android:src="@drawable/bb03" 
android:layout_marginLeft="497dp" 
android:layout_marginTop="153dp"
 android:layout_alignParentTop="true" 
android:layout_alignParentLeft="true"/>

and here is my GameplayActivity

 public class GamePlayActivity extends Activity {

    static int ObjectsFound;


     @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.gameplay);


            // ViewGroup where n number of view is going to added
            ViewGroup layout= (ViewGroup) findViewById(R.id.GamePlayScreen);

             // inflating the layout depending on the level 
            View level = View.inflate(this, LevelSelectionActivity.levelscreen, null);

            ** getRandom();    // here I want to call that random because this should be done     before my level is added on the view**

    // adding level bg for the respective selected level
            layout.addView(level);
}   

 public void objectClick(View objectClicked)
     {
         Toast msg;
         int Object = objectClicked.getId();
         ImageView img= (ImageView)findViewById(objectClicked.getId());
        switch (Object) {
        case R.id.imageView1:   
             img.setVisibility(View.INVISIBLE);
             msg = Toast.makeText(GamePlayActivity.this, "Bubble Found", Toast.LENGTH_SHORT);
             msg.setGravity(Gravity.CENTER, msg.getXOffset() / 2, msg.getYOffset() / 2);
             msg.show();

            break;

        }
}

and the ObjectClass

    public class Objects {


     int ObectId[];

     Objects(Context context)
     {
         super();
         for(int i=0;i<15;++i)
         {
             **ObectId[i]=R.id.;**     // what to get it over here  ? ? ? 
         }

     }

     public void randomize() {
   Random generator = new Random();
   for(int i = 0; i<8 ; i++) {
       while(true) {
           **View v = findViewById(generator.nextInt(Objectid.length));**
           if(!v.isClickable()) {
                v.setClickable(false);
                break;
           }
       }
   }
}


}

at the end I want random number of objects to be unclickable i.e. out of 15 objects everytime user can have only 8 objects clickable on the screen so before my level starts i.e. before I inflate in my gameplayActivity as seen above I want to get random 8 clickable and vice versa...

Adding to that it should work for all level ...right now I have one level1.xml how to achieve it for all levels... do i need to give same id's of image view in all layout ?

3 Answers 3

1

Sure you can, all you need to do is make the ImageView with the same id name, then you can always access them with the same int id.

Don't forget to re-init them after load new layout.

Sign up to request clarification or add additional context in comments.

2 Comments

Yah but how should I retrieve imageIds over here in array ObectId[i]=R.id.imageview[i]; will this work ?
No, you should be ImageView imgview = currentLayout.findViewById(R.id.imageview1);
1

try some thing like this

 int resA = getResources().getIdentifier("your image view", "your layout", getPackageName());

hope this help..

1 Comment

will this give me array of imageview ids from my layout.xml ??
1

Are there any views inside that RelativeLayout besides each ImageView you're interested in? If not, just use the getChildCount() and getChildAt() methods on the layout to obtain the views you need. Any ViewGroup already stores its children in an array, so there's no reason to duplicate the behavior if those are the only views contained there:

//Somewhere in your code get a reference to the layout
RelativeLayout layout = (RelativeLayout)findViewById(R.id.relativeLayout1);

//Then, update your view selection line like so
**View v = layout.getChildAt(generator.nextInt(layout.getChildCount()));**

HTH

2 Comments

No there no views inside Relative layout other than Imageview but now one problem is that how will I get findViewId work because I am not extending any views in my Objects class ?
Pass the layout reference to your objects class instead of a Context. As far as I can tell, you don't have any need for findViewById or a Context in that class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.