0

I'm writing a calendar app for Android. This app has a day view, week view and month view, very similar to the standard calendar apps.

All three views need to know what dates they are showing before the calls to onMeasure, onLayout, onDraw.

Now, I've been thinking whether I should create fully customized view classes, or use XML layouts as much as possible, and embed custom views inside the layouts only when I need to, but I hit a brick wall with the second option: again, since all layouts will have at least one sub-view that depends on the selected dates, and the only way I know to pass parameters to a view is by calling a custom constructor (as opposed to inflating where I can't pass params at all) - basically, I would HAVE to use completely custom views...

Am I correct about this? Is there no way to pass parameters to inflated views?

user884248
  • 2,134
  • 3
  • 32
  • 57
  • You can alter Views either by using attributes in the XML, or calling methods on the View instance. Depending on your scenario you might want to create a custom view with a new XML attribute, which you can read in the constructor. – fractalwrench Dec 23 '15 at 09:18
  • Are you looking for [custom attributes](https://developer.android.com/training/custom-views/create-view.html#customattr)? – schrieveslaach Dec 23 '15 at 09:18
  • @Schrieveslaach - no, because from what I understand, custom attributes are the reverse of what I'm looking for: they allow you to define attributes in the XML, as opposed to getting them from code. Xavier.S's answer was great, see below. – user884248 Dec 23 '15 at 09:54

3 Answers3

3

The method inflate() is for converting a layout.xml to a View instance. You can do that but much more work is needed, and passing dynamic data seems impossible.

But I think your brick wall is not a really brick wall:

since all layouts will have at least one sub-view that depends on the selected dates.

I figure that you wanna set data before the view shows, so you can:

    View view = LayoutInflater.from({context}).inflate(R.layout.{XML_name}, null);
    TextView tv_year = (TextView)view.findViewById(R.id.tv_year);
    tv_year.setText("2015");
    ...
    {root_view}.addView(view);

{context} can be your Activity, {XML_name} is what you wanna use, and {root_view} is where you wanna add your date view.

Xavier.S
  • 319
  • 2
  • 7
  • such a simple and precise answer. Thank you. I feel a little bit silly about not realizing that the view does not get drawn immediately after inflating. – user884248 Dec 23 '15 at 09:26
  • Disagree. You can inflate custom layout class - no problem: declare it in my_layout_component.xml tag name like . – Zon May 30 '17 at 04:22
  • @Zon I've modified my answer to express more accurately. Thanks. – Xavier.S Sep 26 '18 at 09:03
0

you can wirte a containerView to add dayView,weekView and monthView,a callback when dayView is click or scroll to notify weekview and monthview inflate diff view or refresh adapter

Scofield
  • 1
  • 1
0

This is a good practice: separating views from functional code makes it more readable. You can definetely inflate custom classes from your custom_layout.xml into your code. To do so, set your custom class (extended from some view) as the name of a tag:

<com.my_site.MyView>
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/my_view"
</com.my_site.MyView>

If your custom view class constructor accepts extra parameters, you will also need custom LayoutInflator factory, where you can set these parameters. To make this approach common for different cases you can always pass a single MyParameters class instance that will accept parameter values of different types and give them back in target constructor.

In my case I found it easier to pass all customization away from constructor to separate methods to call them after MyView is instantiated.

Zon
  • 18,610
  • 7
  • 91
  • 99