Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getRight, getLeft, getTop returning zero

Tags:

android

I am using following code. But all methods are returning zero value. I know that to get the coordinates of the view our view should be drawn. That why i am using the code in onResume method but still not working. Any Idea?

 @Override
public void onResume(){
    super.onResume();
    System.out.println("Onresume"); 
    System.out.println("tab1 - left" + btn_Tab7 .getLeft());
    System.out.println("tab1 - Top" + btn_Tab7.getTop());
    System.out.println("tab1 - right" + btn_Tab7.getRight());
    System.out.println("tab1 - bottom" + btn_Tab7.getBottom()); 
}
like image 257
user1154390 Avatar asked Aug 21 '12 10:08

user1154390


1 Answers

in onResume its too early to call getLeft, getRight ... Do it in onWindowFocusChanged

@Override
public void onWindowFocusChanged (boolean hasFocus){
    super.onWindowFocusChanged(hasFocus);
    if(hasFocus){
        System.out.println("onWindowFocusChanged"); 
        System.out.println("tab1 - left" + btn_Tab7 .getLeft());
        System.out.println("tab1 - Top" + btn_Tab7.getTop());
        System.out.println("tab1 - right" + btn_Tab7.getRight());
        System.out.println("tab1 - bottom" + btn_Tab7.getBottom());
    }
}

Documentation from onResume:

Keep in mind that onResume is not the best indicator that your activity is visible to the user; a system window such as the keyguard may be in front. Use onWindowFocusChanged(boolean) to know for certain that your activity is visible to the user (for example, to resume a game).

like image 191
vikki Avatar answered Nov 02 '22 16:11

vikki