Android TimePicker Example Code

Android TimePicker Example Code             
   
             Android TimePicker gadget is utilized to choose date. It permits you to choose time by hour and moment. You can't choose time by seconds.

The android.widget.TimePicker is the subclass of FrameLayout class.

Android TimePicker Example

We should see a straightforward illustration of android time picker.

activity_main.xml

File: activity_main.xml

  1. <RelativeLayout xmlns:androclass="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     tools:context=".MainActivity" >  
  6.   
  7.     <TimePicker  
  8.         android:id="@+id/timePicker1"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_alignParentTop="true"  
  12.         android:layout_centerHorizontal="true"  
  13.         android:layout_marginTop="86dp" />  
  14.   
  15.     <TextView  
  16.         android:id="@+id/textView1"  
  17.         android:layout_width="wrap_content"  
  18.         android:layout_height="wrap_content"  
  19.         android:layout_alignLeft="@+id/timePicker1"  
  20.         android:layout_alignParentTop="true"  
  21.         android:layout_marginTop="17dp"  
  22.         android:text="Current Time:" />  
  23.   
  24.     <Button  
  25.         android:id="@+id/button1"  
  26.         android:layout_width="wrap_content"  
  27.         android:layout_height="wrap_content"  
  28.         android:layout_alignLeft="@+id/timePicker1"  
  29.         android:layout_below="@+id/timePicker1"  
  30.         android:layout_marginLeft="37dp"  
  31.         android:layout_marginTop="55dp"  
  32.         android:text="Change Time" />  
  33.   
  34. </RelativeLayout>  

Activity class

File: MainActivity.java
package com.dheeruapps.timepicker1;  
  
import android.os.Bundle;  
import android.app.Activity;  
import android.view.Menu;  
import android.view.View;  
import android.view.View.OnClickListener;  
import android.widget.Button;  
import android.widget.TextView;  
import android.widget.TimePicker;  
import android.widget.Toast;  
  
public class MainActivity extends Activity {  
    TextView textview1;  
    TimePicker timepicker1;  
    Button changetime;  
      
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
          
        textview1=(TextView)findViewById(R.id.textView1);  
        timepicker1=(TimePicker)findViewById(R.id.timePicker1);  
        //Uncomment the below line of code for 24 hour view  
        timepicker1.setIs24HourView(true);  
        changetime=(Button)findViewById(R.id.button1);  
          
        textview1.setText(getCurrentTime());  
          
        changetime.setOnClickListener(new OnClickListener(){  
            @Override  
            public void onClick(View view) {  
                 textview1.setText(getCurrentTime());  
            }  
        });  
          
    }  
  
    public String getCurrentTime(){  
        String currentTime="Current Time: "+timepicker1.getCurrentHour()+":"+timepicker1.getCurrentMinute();  
        return currentTime;  
    }  
    @Override  
    public boolean onCreateOptionsMenu(Menu menu) {  
        // Inflate the menu; this adds items to the action bar if it is present.  
        getMenuInflater().inflate(R.menu.activity_main, menu);  
        return true;  
    }  
  
}  













0 comments:

Post a Comment

My Instagram