Timer in Android Studio Java
XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
>
<TextView
android:layout_width="wrap_content"
android:id="@+id/timerText"
android:layout_height="wrap_content"
android:layout_marginHorizontal="30dp"
android:text="..."
android:textSize="50dp"
/>
<Button
android:id="@+id/timerButton"
android:layout_marginVertical="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="baslat"
/>
</LinearLayout>
</RelativeLayout>
JAVA
package com.example.timerjava;
import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.os.CountDownTimer; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends AppCompatActivity { private Button timerBtn; private TextView timerTxt; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); timerBtn = findViewById(R.id.timerButton); timerTxt = findViewById(R.id.timerText); timerBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { timerBtn.setEnabled(false); new CountDownTimer(60000,1000){ @Override public void onTick(long l){ timerTxt.setText(""+l/1000); } @Override public void onFinish() { Toast.makeText(MainActivity.this, "sayım bitti", Toast.LENGTH_SHORT).show(); timerBtn.setEnabled(true); } }.start(); } }); } }
Comments
Post a Comment