Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8800349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:33:53+00:00 2026-06-14T00:33:53+00:00

I’m in the process of diving into Android development. Most of the projects I

  • 0

I’m in the process of diving into Android development. Most of the projects I was involved with, were in C#. Delegates were elements of C# I did use very often, I’ve used also stuff like passing data using class that extends EventArgs or properties with set and get. With my programming knowledge I think I will be able to get started with Android development pretty smoothly. The thing is I have completly no idea how to approach an implementing mechanism similar to C# delagte in Java.

Below I present some exemplary class that works in C# just fine and contains some elements of C# language that I would like to use in my future Android projects. Can someone provide me with a translation of this code? I would prefer that ‘cos working with my own example and its conversion would allow me to catch it all faster. Also, any valuable resources on the topic (not only delegates but genereal topic of converting C# into Java) would be apreciated.

CountdownTimer.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SampleDelegateApp
{
    public class CountdownTimer
    {
        Timer tmrTicks = new Timer();
        int secondsLeft = 0;
        int numberOfSecondsToCountdown = 0;

        public bool IsWorking
        {
            get { return tmrTicks.Enabled; }
        }

        public CountdownTimer(int seconds)
        {
            if (secondsLeft < 0) secondsLeft = 0; 
            numberOfSecondsToCountdown = seconds;
            secondsLeft = seconds;

            tmrTicks.Interval = 1000;
            tmrTicks.Tick += new EventHandler(tmrTicks_Tick);
            tmrTicks.Enabled = false;
        }

        void tmrTicks_Tick(object sender, EventArgs e)
        {
            secondsLeft--;
            if (secondsLeft >= 1) 
                WhenCountdownTimerTick(new CountdownTimerEventArgs(secondsLeft, numberOfSecondsToCountdown, false));
            else
            {
                Stop();
                WhenCountdownTimerTick(new CountdownTimerEventArgs(secondsLeft, numberOfSecondsToCountdown, true));
            }
        }

        public void Reset()
        {
            Stop();
            secondsLeft = numberOfSecondsToCountdown;
            if (secondsLeft < 0) secondsLeft = 0;
            WhenCountdownTimerTick(new CountdownTimerEventArgs(secondsLeft, numberOfSecondsToCountdown, false));
        }

        public void Stop()
        {
            tmrTicks.Enabled = false;
        }

        public void Start()
        {
            if (secondsLeft <= 0)
            {
                secondsLeft = 0;
                WhenCountdownTimerTick(new CountdownTimerEventArgs(secondsLeft, numberOfSecondsToCountdown, true));
            }
            else
            {
                tmrTicks.Enabled = true;
            }
        }

        public delegate void CountdownTimerTickEventHandler(object sender, CountdownTimerEventArgs ea);

        public event CountdownTimerTickEventHandler CountdownTimerTick;

        protected virtual void WhenCountdownTimerTick(CountdownTimerEventArgs ea)
        {
            if (CountdownTimerTick != null)
            {
                CountdownTimerTick(this, ea);
            }
        }
    }

    public class CountdownTimerEventArgs : EventArgs
    {

        public string timeString = "";
        public float procentOfTimeLeft = 0.0f;
        public bool countdownFinished = false;

        public CountdownTimerEventArgs(int secondsLeft, int SecondsToCountdown, bool isfinished)
        {
            countdownFinished = isfinished;
            timeString = string.Format("{0:00}:{1:00}", secondsLeft / 60, secondsLeft % 60);
        }
    }
}

frmTest.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SampleDelegateApp
{
    public partial class frmTest : Form
    {
        CountdownTimer ctmrTest;

        public frmTest()
        {
            InitializeComponent();
            ctmrTest = new CountdownTimer(-44);
            ctmrTest.CountdownTimerTick += new CountdownTimer.CountdownTimerTickEventHandler(ctmrTest_CountdownTimerTick); 
        }

        void ctmrTest_CountdownTimerTick(object sender, CountdownTimerEventArgs ea)
        {
            lblTimer.Text = ea.timeString;
            if (ea.countdownFinished) countdownEnd();
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            ctmrTest.Reset();
            ctmrTest.Start();
        }

        void countdownEnd()
        {
            MessageBox.Show("Finish");
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T00:33:57+00:00Added an answer on June 14, 2026 at 12:33 am

    I apparently used some words in my question that made it all unclear and/or persons who tried to help me completly missunderstood my needs as for this question. Putting word “delegate” here and there made it even more complexed. After one more question asked in other way i got my answer that made me able to answer my question:

    How to achieve c# event behavior in java (Android)

    I’m not saying that code below is up to the best coding standards, and example shown here got any use in real life project, but this is exactly what i was looking for. Below working example:

    tester.java

    public class tester{
        public static void main(String[] args) {
            test test1 = new test(); 
            test1.start();
        }
    }
    

    test.java

    public class test implements CountdownTextTickListener {
    
        public test() { }
    
        public void start() {
            CountdownText ctx = new CountdownText(100);
            ctx.setListener(this);
            ctx.Start();
        }
    
        @Override
        public void CountdownTextTickEventFired(Object sender, 
                            CountdownTextTickEventArgs eventArgs) {
                System.out.println(eventArgs.TimeString);
                if(eventArgs.isStopped) System.out.println("- END -");
        }
    }
    

    CountdownTextTickListener.java

    public interface CountdownTextTickListener {
         void CountdownTextTickEventFired(Object sender, CountdownTextTickEventArgs eventArgs);
    }
    

    CountdownTextTickEventArgs.java

    public class CountdownTextTickEventArgs {
    
        public String TimeString = "";
        public boolean isStopped = false;
    
        public CountdownTextTickEventArgs(int seconds, boolean isStoppedState) {
            TimeString = String.format("%02d:%02d",seconds/60, seconds % 60);
            isStopped = isStoppedState;
        }
    
    }
    

    CountdownText.java

    import java.util.Timer;
    import java.util.TimerTask;
    
    public class CountdownText {
    
        Timer tmrTicks = new Timer();
        int secondsLeft = 0;
        int numberOfSecondsToCountdown = 0;
        boolean isWorking = false;
        private CountdownTextTickListener listener = null;
    
        public boolean getIsWorking(){
            return isWorking;
        }
    
        public CountdownText(int seconds) {
            if (secondsLeft < 0) secondsLeft = 0; 
            numberOfSecondsToCountdown = seconds;
            secondsLeft = seconds;
        }
    
        void startTimer() {
            isWorking = true;
            fireEvent(secondsLeft, false);
            tmrTicks = new Timer();
            tmrTicks.scheduleAtFixedRate( new TimerTask(){
                @Override
                public void run(){
                    tickTimer();
                }
            }, 1000, 1000); 
        }
    
        private void stopTimer() {
            isWorking = false;
            tmrTicks.cancel();
        }
    
        private void tickTimer() {
             secondsLeft--;
             if (secondsLeft >= 1) 
             {
                 fireEvent(secondsLeft, false);
             }
             else
             {
                 Stop();
                 fireEvent(secondsLeft, true);
             }
        }
    
        public void Reset() {
            Stop();
            secondsLeft = numberOfSecondsToCountdown;
            fireEvent(secondsLeft, false);
        }
    
        public void Stop() {
            stopTimer();
        }
    
        public void Start() {
            if (secondsLeft <= 0)
            {
                secondsLeft = 0;
                fireEvent(secondsLeft, true);
            }
            else
            {
                startTimer();
            }
        }
    
        protected void fireEvent(int seconds, boolean isStoppedState) {
            if (listener != null) {
                Object sender = this; 
                CountdownTextTickEventArgs eventArgs = new CountdownTextTickEventArgs(seconds, isStoppedState);
                listener.CountdownTextTickEventFired(sender, eventArgs);
            }
        }
    
        public void setListener(CountdownTextTickListener listener) {
            this.listener = listener;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to understand how to use SyndicationItem to display feed which is
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I am currently running into a problem where an element is coming back from
I have thousands of HTML files to process using Groovy/Java and I need to
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.