Showing posts with label Android code for time picker.. Show all posts
Showing posts with label Android code for time picker.. Show all posts

Thursday, March 1, 2012

Android code for time picker

This program is for pick time in android.



Step 1) Your  main java file is here



package test.test;

import android.app.Activity;
import android.os.Bundle;
import android.text.method.KeyListener;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.TimePicker;

public class McatestActivity extends Activity {

       Button mybtn;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
     
        mybtn =(Button)findViewById(R.id.btn);
        final TimePicker tp=(TimePicker)findViewById(R.id.mytime);
        mybtn.setOnClickListener(new OnClickListener() {
                    
                     public void onClick(View arg0) {
                           // TODO Auto-generated method stub
                          
                           TextView mytv=(TextView)findViewById(R.id.txt);
                           mytv.setText("Now time is:" + tp.getCurrentHour()+":"+ tp.getCurrentMinute());
                          
                          
                          
                     }
              });
       
       
       
       
    }
}


Step 2) Main.xml file coding goes here

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/txt"
    />
  
   <TimePicker
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/mytime"
   />
  
  
    <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/btn"
    android:text="Select Date"
    />
</LinearLayout>