• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Creating a Calendar Layout

Sycobob

Well-Known Member
Hi guys, I'm looking for a bit of advice here. I'm starting a calendar app and I'm trying to figure out what the best way to create the layout is. I don't want to use the built in CalendarView as it's only in API >11 which limits the potential users too much.

I've toyed around with a GridView with and Adapter behind it. Truth be told I'm still figuring out how that one works, but it seems the most promising.

I tried a nested LinearLayout of TextViews but that requires accessing each TextView be it's ID. This seems too inefficient.

I'm still searching and trying things, but if anyone can nudge me in the right direction, that would great.
 
Have you considered a table layout? I think you best best would be to dynamically create the layouts... for example..

Here is an example similar to on of my applications:


Code:
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
		
     /* INFLATE MAIN TABLE LAYOUT */
     ViewGroup root = (ViewGroup) inflater.inflate(R.layout.table_root, null);
     TableLayout tbl = (TableLayout) root.findViewById(R.id.tblLayout);

      this.setContentView(root);

      ViewGroup row = (ViewGroup) inflater.inflate(R.layout.table_row, root, false);

    /* GENERIC LOOP FOR CREATING TABLE */
    
    int count = 1;

    for (int day = 0; day < calendarCount; day++)
    {
       row.addView(day);

       if (count <= weekCount) {
          root.addView(row);
          row = (ViewGroup) inflater.inflate(R.layout.popup_row, root, false);
       }
	
       count++;

     }


I tried to give you an example relating to creating a calendar like grid but this is not all the code you will need. I hope this gives you enough to get yourself started :) If you still have trouble... post your code so we can reference it to give you a hand. Good Luck!
 
Thanks for the info! A dynamic TableLayout is indeed what I settled on. I'm currently trying to decide the best way to 'address' the cells. When a user clicks a date, I have to figure out what day was clicked. Currently I'm using on click listeners for each cell and just getting the text to see what day was clicked.

This seems a little inefficient since I have to define the listeners differently based on whether the cell is part of the current month, previous month or next month (I fill in each row completely, padding it with the next or previous month when the current month ends or begins in the middle of a row).
 
I actually had a simular issue and I just did a quick hack job where I used the contentDescription for a ImageButton and assigned it a value using "setContentDescription".

So in your loop where you create the days you can do this:

Code:
    imgbutton.setOnClickListener(this);
    imgbutton.setContentDescription(intDay);

Then in code you can recover it like this:


Code:
public void onClick(View v) {

	ImageButton b = (ImageButton) v;
	int dayInt = Integer.valueOf(b.getContentDescription().toString());
	
}

Now, to only have one onClick listener you will need to create a class that implements OnClickListener and have the loop to create the table inside this class onCreate.
 
Stepping back to the first issue again, I'm looking to optimize it a little more. I have no problem with getting my layout set up and interacting with it, but I notice something that bugs me.

Since I have no way to address a specific cell after it's been added, I can't reuse any of the widgets I fill it with. This means I have to redraw the whole TableLayout to make any changes. For example, say the use is looking at October, and wants to see November. The user presses the nextMonth button and behind the scenes I have to remove all children of the TableLayout and recalculate the entire table.

Any thoughts on how to improve this design, or is this a trivial enough calculation that I shouldn't worry about it?
 
Found a way around scrapping and recreating the layout each time. I overlooked the ViewGroup.getChildAt method. It definitely made a big difference in performance. I build the layout once, then just change the text when needed. View.setVisibility is still slow though.
 
I've more or less finished all the layout stuff I've been trying to figure out. Now I'm on the actual content side things. I'm not really sure what the best method for reading calendar entries is. There's an undocumented content provider in earlier versions of android, but at some point that gets changed and causes problems. On the other hand there's the GData API. Seems like the content provider is much easier, but the API is more 'stable'.

Any thoughts on the best method for reading, writing, and deleting calendar data?
 
The GData API is probably your best long term solution. The undocumented APIs have a tendency to disappear at inopportune moments.
 
Thanks for the tip. That's what I was looking at originally, but it seems absolutely ludicrous to me to need an internet connection to make changes to the calendar on your phone. I think I'm going to just use the undocumented APIs. They can't change past APIs so all I need to do it check the Android version on the device and use the appropriate calls to the calendar provider. I think it becomes official in 2.3 or something.
 
Back
Top Bottom