Question

public class Game_collecting_view extends View 
{
    Button image_boy;
    private static final int BOY_DIAMETER = 200; // initial spot size
    int boy_width =0;
    int boy_height =0;

   public void setGame_collecting(Game_collecting mGame_collecting)
   {
       this.mGame_collecting = mGame_collecting;
   }    

   // constructs a new View
   public Game_collecting_view(Context context, RelativeLayout parentLayout)
   {    
       super(context);
      resources = context.getResources(); // save Resources for loading external values
      layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      // get references to various GUI components
      relativeLayout = parentLayout;
      spotHandler = new Handler(); // used to add spots when game starts

   } 

   @Override
   protected void onSizeChanged(int width, int height, int oldw, int oldh)
   {
      viewWidth = width; // save the new width
      viewHeight = height; // save the new height
   } 

   public void set_boy()
   {
          final Button boy = (Button) layoutInflater.inflate(R.layout.untouched, null);
          boy.setX(viewWidth /2); 
          boy.setY(viewHeight - BOY_DIAMETER); 
          boy.setPadding(0,0,0,0);
          boy.setBackgroundResource(R.drawable.blue);         
          boy.setLayoutParams(new RelativeLayout.LayoutParams(BOY_DIAMETER, BOY_DIAMETER));
          relativeLayout.addView(boy); // add spot to the screen

          Toast.makeText(getContext(), "set_boy\nviewWidth=" +viewHeight +"\nviewHeight=" +viewHeight, Toast.LENGTH_SHORT).show();

          boy.setOnClickListener
          ( 
             new OnClickListener()
             {            
                public void onClick(View v)
                {
                   touchedSpot(boy); 
                } 
             }  
          ); 
   }

   public void resume(Context context)
   {
         resetGame();
   } 

   public void resetGame()
   {
      for (int i = 1; i <= INITIAL_SPOTS; ++i)
      {
          spotHandler.postDelayed(addSpotRunnable, i * SPOT_DELAY);
          generate_text();
      }          
      set_boy();
   } 

   private Runnable addSpotRunnable = new Runnable()
   {
      public void run()
      {
         addNewSpot(); // add a new spot to the game
      } 
   }; 

Objective:

I would like to set the boy icon at the bottom middle of the screen. The boy icon is set at this way for later dynamic interface (swipe the screen to the right the boy icon will move to the right, vice versa)

Observation:

The toast reports both the viewWidth and viewHeight =0, and the boy icon appears at 0,0 (left upper corner). If I set the setY(viewHeight + BOY_DIAMETER), the boy icon will be located at (0, 200).

Question:

I would like to ask why the viewWidth and viewHeight both report 0. How could the onSizeChanged be called immediately such that the boy icone could be set at the bottom center of the screen?

Thanks!!

Was it helpful?

Solution

The location is null until Android has calculated their positions. You can retrieve the height and width like this:

image_boy.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

        @Override
        public void onGlobalLayout() {
            int height = image_boy.getHeight();
            int width = image_boy.getWidth(); 
            //these values won't be null
        }
    });

OTHER TIPS

set layout param for setting the view position

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top