1

I know you can have different UI elements by declaring different layout folders like layout-large or layout-xlarge. However, I don't want to have to update two separate files every single time I make a change to my apps interface.

Is there any other way to have a button that only has visibility="gone" on small screens and normal visibility on large screens?

you786
  • 3,659
  • 5
  • 48
  • 74

2 Answers2

1

I separated the button into 2 XML files - one in layout-large that has visibility="visible" and one in layout that has visibility="gone", and then include the button in my layout/home.xml file. It worked.

you786
  • 3,659
  • 5
  • 48
  • 74
  • 1
    This is the simplest answer - the key being that you only have separate files for the button, not for the whole activity layout or anything like that. If you need to repeat this behavior a lot then I think you can include visibility in a style declaration. – atw13 Mar 26 '13 at 22:50
0

You can separate it into two XML layout files but then you have to maintain both. You can abstract it out by having one layout file and then give the button a style that's defined for both tablets and not.

I would set it to GONE and change it in code. That way you don't have two layout files.

However, I would not use Blumer's method of getConfiguration().screenLayout. Buckets are no longer the best way to handle different screen sizes. Dianne Hackborn explains why in a post, but it boils down to:

Based on developers’ experience so far, we’re not convinced that this limited set of screen-size buckets gives developers everything they need in adapting to the increasing variety of Android-device shapes and sizes. The primary problem is that the borders between the buckets may not always correspond to either devices available to consumers or to the particular needs of apps.

Instead, you should use their new numeric values--roughly sw600dp for 7" tablets and sw720dp for 10":

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mylayout);

    if (getResources().getBoolean(R.bool.sw600dp)) {
        ((Buton)findViewById(R.id.mybutton)).setVisibility(VISIBLE);
    }
}
yarian
  • 5,922
  • 3
  • 34
  • 48
  • 1
    Good point regarding buckets. Since yours does it right and covers the same approach, I've removed mine. – Blumer Mar 27 '13 at 03:18