Home > Software design >  How can I create widget with timer?
How can I create widget with timer?

Time:07-14

I am working on one application with has a widget functionality So I have designed a widget but tried with timer.

widget is working on background after kill the app.so can you guide me to set current time on my widget.

CodePudding user response:

You can use timer as chronometer. it work like countdown timer and you don't need background service for that.

You Can Implement like this :

Widget XML

           <Chronometer
            android:id="@ id/chronometer"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginVertical="6dp"
            android:textColor="@color/white"
            android:textSize="20sp"
            android:textStyle="bold" />

AppWidgetProvider

val view = RemoteViews(context?.packageName, R.layout.timer_widget)
val startTime: Long = SystemClock.elapsedRealtime()
view.setChronometer(R.id.chronometer,startTime 120000,"%tH:%tM:%tS",true)
view.setChronometerCountDown(R.id.chronometer,true)//if you want to reverse countdown
appWidgetManager?.updateAppWidget(appWidgetId, view)
  • Related