Time Picker in Android.
Welcome to AndroidEasy. Today we will see how to create TimePicker in android application in easiest way.
Step 1: Create a new project in Android Studio.( Select File ⇒ New ⇒ Android Project and give
activity name as TimePicker.)
Step 2: Open timePicker_activity.xml file and Paste below code into it.
Step 3: Open TimePicker.java file and type below code.
Step 4: And we are done with this. Now run the project.
Thank Youππ.
Step 1: Create a new project in Android Studio.( Select File ⇒ New ⇒ Android Project and give
activity name as TimePicker.)
Step 2: Open timePicker_activity.xml file and Paste below code into it.
<?xml version="1.0"
encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity">
<TimePicker
android:id="@+id/calendarView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="0dp" />
<Button
android:id="@+id/btn_get"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Get Time" />
<TextView
android:id="@+id/tv_set"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text="Hello World!" />
</LinearLayout>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity">
<TimePicker
android:id="@+id/calendarView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="0dp" />
<Button
android:id="@+id/btn_get"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Get Time" />
<TextView
android:id="@+id/tv_set"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text="Hello World!" />
</LinearLayout>
Step 3: Open TimePicker.java file and type below code.
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;
public class TimePicker extends AppCompatActivity
{
TimePicker time_pick;
Button btn_get; TextView tv_setTime; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.timePicker_activity); time_pick=findViewById(R.id.calendarView); btn_get=findViewById(R.id.btn_get); tv_setTime=findViewById(R.id.tv_set); btn_get.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int hr=time_pick.getHour(); int mins=time_pick.getMinute(); StringBuilder builder=new StringBuilder(); builder.append(hr); builder.append(mins);tv_setTime.setText(builder.toString()); } }); } }
Step 4: And we are done with this. Now run the project.
Thank Youππ.
Nice... Helpful!
ReplyDeleteEasily explained ma'am✌π
ReplyDeleteHelpful....!!!!
Nice ma'am π
ReplyDeleteVery well explained the concept of Timepicker,a systematic step by step explanation along with images and code!!!ππ
ReplyDeleteThnq
DeleteNice one ma'am
ReplyDeleteThnq sir
DeleteNice
ReplyDeleteSamaj main to aagaya..... But now i can't change my lineπ π
ReplyDeleteπ
Delete