0

My Android app has two layouts, one for <= normal, and one for >= large. I can re-use my corresponding activity 99.9% in both cases, but I need it to know different bits of information for the two layouts. I could of course write a giant if expression, but I'd rather let Android work its magic, since it's already doing it by loading the two different layouts.

So, can I embed pieces of information in the two XML files and retrieve them in my activity class? Or am I completely off the map and the right approach is completely different?

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
Morpheu5
  • 2,610
  • 6
  • 39
  • 72
  • Could you please provide an example of what you're trying to achieve? What pieces of information are those? – Egor Nov 15 '16 at 14:37
  • One example is: my main activity has a coordinator layout -- tabs -- and I want to have three tabs for smaller screens, with certain activities in them, and two tabs for larger screens, with different activities in them. The reason I'm looking into making variants of my layout is that I may need to make other UI changes, so I was hoping to cram these info inside the layout. – Morpheu5 Nov 15 '16 at 14:41

3 Answers3

1

Sure you can, just in the values directory define values for each size and retrieve them dynamically in your program.

/res/values-xxx
 -> a.xml
/res/values-yyy
 -> a.xml
...

here is an example:

<resources>    
    <integer name="maximum">100</integer>
    ...

</resources>

in your program just put:

int max = getContext().getResources().getInteger(R.integer.maximum);

for each size android will magically do the job and give you the correct value!

Pooya
  • 6,083
  • 3
  • 23
  • 43
  • This may be reasonable enough. Have a look at my comments below the original post and see if they help. – Morpheu5 Nov 15 '16 at 14:45
  • 1
    @Morpheu5 I used this method for GridLayout columns. I used 3 for larger devices and 2 for smaller device and it worked well – Pooya Nov 15 '16 at 14:47
1

If you're willing to go the custom View route, then yes, you can. What you have to do is create custom attributes and apply them to your custom views which are parsed when they are created. Here is a thread that goes in to a great bit of detail about it.

The Views themselves don't have to be special. You can say, have a view called a PropertyView view which extends FrameLayout and has a method called getProperty(). You then put the property in the XML like so:

<com.example.ProperyView
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   app:property="My Custom Property"/>

Then you would parse it using the methods described in that link.

EDIT:

Alternatively, there are other elements in the XML landscape that can be put in to buckets similar to how Layouts are. Any folder in the /res folder can have the same buckets that the Layouts can. That includes anything in the values, drawables, raw, or xml folders. You can reference these in your Layouts and the Android system will pick which ones you want. More example for how dimens work.

Community
  • 1
  • 1
DeeV
  • 35,865
  • 9
  • 108
  • 95
1

If you are using values to differentiate between the two layouts, then you can have different values that overload depending on screen size. See this answer.

You could do the same sort of thing with the layouts directory to create different layouts, but then use common subsections using the < include > tag to make the different views based on common sections.

Or a combination of the two. If you the want to change the behaivoir of the Activity/Fragment, you could key that on the presence of missing or present screen widgets.

Community
  • 1
  • 1
Simon Featherstone
  • 1,716
  • 23
  • 40