1

Possible Duplicate:
How to Define Callbacks in Android?

I was reading through the Internet about Callbacks and I understand that this has heavy weightage in an Android Ecosystem.

Can anyone state an example and explain what are callbacks are how do they work?

Community
  • 1
  • 1
Kyle Yeo
  • 2,270
  • 5
  • 31
  • 53
  • 1
    You can refer this link - How to Define Callbacks in Android? - http://stackoverflow.com/q/3398363/1441666 – Nirali Oct 25 '12 at 05:26
  • 1
    This is best explained in person. Talk to someone who knows how event driven models work.. – dcow Oct 25 '12 at 05:29
  • @DavidCowden I'm learning solo so I don't really have anyone who understands callbacks right now. But I'm reading up, hopefully I'll understand soon. – Kyle Yeo Oct 25 '12 at 05:54

1 Answers1

2

Do you mean callback using interface? if yes here's an example

this is callbackmenu.java

package com.example.test.callback;

public interface CallbackCalendar {
    public void onClick();
}

this is an example how you implement it

public class CallbackCell implements CallbackCalendar{

    @Override
    public void onClick() {
        Log.i("TAG", "IT WORKS!);
        addChild(2);
    }
}

this is an example that will allow you to access a view from another view like what I use in my calendar library, I create 3 view classes, calendar View, calendar row, calendar cell

with passing this callback from calendar view to calendar cell we can add view,set value or anything else in the calendar view from the calendar cell (calendar cell is a part of calendar row and calendar row is a part of calendar view) in this example I'm trying to set whenever the user click the cell we will add another view in the calendar view (main view)

this is the example of using callback in the calendar cell

public CalendarCell(Context context,int day,final CallbackCalendar callback)
{
    super(context);
    final int temp = day;
    this.context = context;
    this.setBackgroundColor(Color.parseColor("#FFFFFFFF"));
    LinearLayout.LayoutParams lpCalendarCell = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
    lpCalendarCell.weight = 1;
    this.setLayoutParams(lpCalendarCell);
    this.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            callback.onClick();
        }
    });
    addContainer(day);
}

so I set the callback in the calendar view and pass it to the calendar row and pass it again to the calendar cell and call the onClick in the calendar cell

I think that's all if you have any question feel free to write in the comment :)

ASH
  • 75
  • 1
  • 12
Niko Adrianus Yuwono
  • 11,012
  • 8
  • 42
  • 64