Saturday 2 April 2016

How to disable ALL buttons in a layout - Android

How to disable All buttons in a layout - Android





Unfortunately there is not an elegant way to do this (e.g. one line of simple code). However below is a method that I defined for myself that does the job perfectly:


public void disableButtons(Layout layout) {

    // Get all touchable views
    ArrayList<View> layoutButtons = layout.getTouchables();

    // loop through them, if they are instances of Button, disable them.
    for(View v : layoutButtons){
        if( v instanceof Button ) {
            ((Button)v).setEnabled(false);
        }
    }
}
So the trick is to retrieve all touchables into an ArrayList, then loop through them while check if it is an instance of the Button class, if it is, disable it!

3 comments:

  1. I'm getting a "cannot resolve" error on getTouchables()

    can you explain how you did that?

    ReplyDelete
    Replies
    1. You need to findViewById(). Example without error :
      View rootView = findViewById(R.id.rootView);
      ArrayList touchables = rootView.getTouchables();

      Delete
  2. Borgata Hotel Casino & Spa - DRMCD
    Borgata Hotel Casino 목포 출장안마 & Spa, Atlantic City. 구리 출장마사지 08401 08401 08401. Website. 여수 출장마사지 https://www.borgata.com/. Address. 604 거제 출장마사지 S. Atlantic City, 거제 출장마사지 NJ 08401 08401

    ReplyDelete