6

I used the code from Reuben Scratton's new answer in this question. When I paste it into my code, I get red squigglies underneath addOnGlobalLayoutListener(new OnGlobalLayoutListener(), onGlobalLayout(), and activityRootView (after heightDiff). Please help me figure out what is wrong.

Thanks Here's my code on the .java page

public class Details extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_details);
    Intent intent = getIntent();        
}


final View activityRootView = findViewById(R.id.details);
activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

    @Override
    public void onGlobalLayout() {
        int heightDiff = activityRootView.getRootView().getHeight() - activityRootView.getHeight();
        if (heightDiff > 100) { // if more than 100 pixels, its probably a keyboard...
            ImageButton backButton = (ImageButton)findViewById(R.id.back); 
            backButton.setImageResource(R.drawable.hide_keyboard);
        }
     }
});
Community
  • 1
  • 1
Eichhörnchen
  • 592
  • 2
  • 12
  • 27
  • 17
    Please don't try to learn programming Java and Android through copy/paste. It's not a successful approach. Go through the basics and start with "Hello World" – Simon Jan 12 '13 at 22:14

1 Answers1

18

You need to add that code inside a method, like onCreate().

public class Details extends Activity {
    View activityRootView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_details);
        Intent intent = getIntent();        

        activityRootView = findViewById(R.id.details);    
        activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                int heightDiff = activityRootView.getRootView().getHeight() - activityRootView.getHeight();
                if (heightDiff > 100) { // if more than 100 pixels, its probably a keyboard...
                    ImageButton backButton = (ImageButton)findViewById(R.id.back); 
                    backButton.setImageResource(R.drawable.hide_keyboard);
                }
             }
        });
    }
}
Sam
  • 86,580
  • 20
  • 181
  • 179
  • 2
    in my project the onGlobalLayout is called in a method in a normal class, and this method is called in onCreate method in a fragment, my problem is that when i debug the application, the block of the code under the onGlobalLayout is unreachable. – Fakher Aug 26 '15 at 10:18
  • what if I'm using databinding? @Sam – Dr4ke the b4dass Jun 14 '21 at 20:49
  • @Dr4ketheb4dass You can call binding component's getRoot() method for root view of the layout. – vilpe89 May 11 '23 at 07:32