Custom Year Picker in Android

 

MainActivity.java

import java.util.Calendar;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.NumberPicker;

public class MainActivity extends Activity {
 Button b;
    static Dialog d ;
    int year = Calendar.getInstance().get(Calendar.YEAR);
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
       b = (Button) findViewById(R.id.year);
       b.setText(""+year);
         b.setOnClickListener(new OnClickListener()
         {

            @Override
            public void onClick(View v) {
                 showYearDialog();
         }
         });
   }
    

    public void showYearDialog()
    {

         final Dialog d = new Dialog(MainActivity.this);
         d.setTitle("Year Picker");
         d.setContentView(R.layout.yeardialog);
         Button set = (Button) d.findViewById(R.id.button1);
         Button cancel = (Button) d.findViewById(R.id.button2);
        TextView year_text=(TextView)d.findViewById(R.id.year_text);
        year_text.setText(""+year);
         final NumberPicker nopicker = (NumberPicker) d.findViewById(R.id.numberPicker1);
         
         nopicker.setMaxValue(year+50);
         nopicker.setMinValue(year-50);
         nopicker.setWrapSelectorWheel(false);
         nopicker.setValue(year);
         nopicker.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
         
         set.setOnClickListener(new OnClickListener()
         {
          @Override
          public void onClick(View v) {
              b.setText(String.valueOf(nopicker.getValue()));
              d.dismiss();
           }    
          });
         cancel.setOnClickListener(new OnClickListener()
         {
          @Override
          public void onClick(View v) {
              d.dismiss();
           }    
          });
       d.show();


    }
  
}

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:paddingBottom="@dimen/activity_vertical_margin"

    android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    tools:context=".MainActivity" >

    <Button

        android:id="@+id/year"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

       android:layout_gravity="center_vertical"

        android:text="Year" />

</LinearLayout>

/res/layout/yeardialog.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:orientation="vertical"

android:layout_height="match_parent" >

<LinearLayout android:layout_width="match_parent" android:orientation="horizontal" android:background="@color/colorAccent" android:layout_height="wrap_content" > <TextView android:id="@+id/year_text" android:layout_margin="10dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textStyle="bold" android:textColor="@android:color/white" android:text="demo"/> </LinearLayout>    <NumberPicker

android:id="@+id/numberPicker1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="center_horizontal"

/>

<LinearLayout

android:layout_width="match_parent"

android:orientation="horizontal"

android:layout_height="wrap_content" >

<Button

android:id="@+id/button2"

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="1"

android:text="Cancel" />

<Button

android:id="@+id/button1"

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="1"

android:text="Set" />

</LinearLayout>

</LinearLayout>
Tagged , , , ,

1 thought on “Custom Year Picker in Android

Leave a Reply

Your email address will not be published. Required fields are marked *