am_dot_com

PDM2 20201110

Nov 10th, 2020 (edited)
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.14 KB | None | 0 0
  1. package com.joythis.android.distancebetweendates;
  2.  
  3. import androidx.appcompat.app.AppCompatActivity;
  4.  
  5. import android.content.Context;
  6. import android.content.Intent;
  7. import android.os.Bundle;
  8. import android.util.Log;
  9. import android.view.View;
  10. import android.widget.ArrayAdapter;
  11. import android.widget.Button;
  12. import android.widget.ListView;
  13. import android.widget.NumberPicker;
  14.  
  15. import java.util.ArrayList;
  16. import java.util.Calendar;
  17.  
  18. public class DateInputActivity extends AppCompatActivity {
  19.     Context mContext;
  20.  
  21.     NumberPicker.OnValueChangeListener mNpValueChangedHandler =
  22. new NumberPicker.OnValueChangeListener() {
  23.     @Override
  24.     public void onValueChange
  25.     (NumberPicker picker, int oldVal, int newVal) {
  26.         int y = mNpYear.getValue();
  27.         int m = mNpMonth.getValue();
  28.         int d = mNpDay.getValue();
  29.  
  30.         boolean bValidDate = AmCalendar.validDay(y,m,d);
  31.  
  32.         int iMaxDayForMonth = AmCalendar.
  33.                 numberOfDaysInMonth(y,m);
  34.  
  35.         mNpDay.setMaxValue(iMaxDayForMonth);
  36.  
  37.         String strMsg = "";
  38.         if (d>iMaxDayForMonth){
  39.             strMsg = getString(
  40.                     R.string.strFixedDay,
  41.                     d,
  42.                     iMaxDayForMonth
  43.             );
  44.             mUserMsgs.add(0, strMsg);
  45.         }
  46.  
  47.         if (!bValidDate){
  48.             strMsg = getResources().getString(
  49.                 R.string.strInvalidDate,
  50.                 y,m,d
  51.            );
  52.             mUserMsgs.add(0, strMsg);
  53.  
  54.            mNpDay.setValue(iMaxDayForMonth);
  55.            //mNpDay.setMaxValue(iMaxDayForMonth);
  56.            
  57.            mAd.notifyDataSetChanged();
  58.         }//
  59.     }//onValueChange
  60. };
  61.  
  62.     NumberPicker mNpYear, mNpMonth, mNpDay;
  63.     Button mBtnConfirmDate;
  64.  
  65.     ListView mLvFeedback;
  66.     ArrayList<String> mUserMsgs;
  67.     ArrayAdapter<String> mAd;
  68.  
  69.     Object[] maRelevant;
  70.  
  71.     Button.OnClickListener mBtnClickHandler =
  72.         new Button.OnClickListener() {
  73.             @Override
  74.             public void onClick(View v) {
  75.                 int iYear = mNpYear.getValue();
  76.                 int iMonth = mNpMonth.getValue();
  77.                 int iDay = mNpDay.getValue();
  78.  
  79.                 String strMsg =
  80.                     getResources().getString(
  81.                         R.string.strUserDate,
  82.                         iYear,
  83.                         iMonth,
  84.                         iDay
  85.                     );
  86.  
  87.                 mUserMsgs.add(0, strMsg);
  88.                 mAd.notifyDataSetChanged();
  89.  
  90.                 Intent responseForCallers = new Intent();
  91.                 responseForCallers.putExtra(
  92.                     KEY_DATE,
  93.                     strMsg
  94.                 );
  95.                 setResult(
  96.                     RESULT_OK,
  97.                     responseForCallers
  98.                 );
  99.                 finish();
  100.             }//onClick
  101.         };
  102.  
  103.     public final static String KEY_DATE = "KEY_DATE";
  104.  
  105.     @Override
  106.     protected void onCreate(Bundle savedInstanceState) {
  107.         super.onCreate(savedInstanceState);
  108.         setContentView(R.layout.date_input_cl);
  109.  
  110.         init();
  111.     }//onCreate
  112.  
  113.     Object[] getImportant(){
  114.         Object[] aRelevant = {
  115.             mNpYear,
  116.             mNpMonth,
  117.             mNpDay,
  118.             mBtnConfirmDate,
  119.             mLvFeedback
  120.         };
  121.         return aRelevant;
  122.     }//getImportant
  123.  
  124.     void init(){
  125.         mContext = this;
  126.  
  127.         mNpYear = findViewById(R.id.idNpYear);
  128.         mNpMonth = findViewById(R.id.idNpMonth);
  129.         mNpDay = findViewById(R.id.idNpDay);
  130.  
  131.         mBtnConfirmDate = findViewById(R.id.idBtnConfirmDate);
  132.         mLvFeedback = findViewById(R.id.idLvFeedback);
  133.  
  134.         maRelevant = this.getImportant();
  135.         boolean bAllAreOK = true;
  136.         for (Object o : maRelevant){
  137.             boolean bOK = o != null;
  138.             bAllAreOK = bAllAreOK && bOK;
  139.         }//for
  140.         if (!bAllAreOK){
  141.             Log.e(
  142.                 "@"+getClass().getName(),
  143.                 "1+ object(s) are null"
  144.             );
  145.             finish();
  146.             return;
  147.         }//if
  148.  
  149.         mNpYear.setMinValue(1);
  150.         mNpYear.setMaxValue(9999);
  151.         mNpYear.setValue(Calendar.getInstance().get(Calendar.YEAR));
  152.  
  153.         mNpMonth.setMinValue(1);
  154.         mNpMonth.setMaxValue(12);
  155.         mNpMonth.setValue(Calendar.getInstance().get(Calendar.MONTH)+1);
  156.  
  157.         mNpDay.setMinValue(1);
  158.         mNpDay.setMaxValue(31); //TODO
  159.         mNpDay.setValue(
  160.             Calendar.getInstance().
  161.             get(Calendar.DAY_OF_MONTH)
  162.         );
  163.  
  164.         mUserMsgs = new ArrayList<>();
  165.         mAd = new ArrayAdapter<>(
  166.             mContext,
  167.             android.R.layout.simple_list_item_1,
  168.             mUserMsgs
  169.         );
  170.         mLvFeedback.setAdapter(mAd);
  171.  
  172.         mBtnConfirmDate.setOnClickListener(
  173.             mBtnClickHandler
  174.         );
  175.  
  176.         //assigned the onChangeChangeListener to all the NumberPickers
  177.         NumberPicker[] aTrio = {mNpYear, mNpMonth, mNpDay};
  178.         for (NumberPicker np : aTrio) np.
  179.                 setOnValueChangedListener(mNpValueChangedHandler);
  180.     }//init
  181. }
  182.  
  183. //**
  184.  
  185. <?xml version="1.0" encoding="utf-8"?>
  186. <RelativeLayout
  187.     xmlns:android="http://schemas.android.com/apk/res/android"
  188.     android:layout_width="match_parent"
  189.     android:layout_height="match_parent">
  190.    
  191.     <LinearLayout
  192.         android:id="@+id/idLlDatesRequester"
  193.         android:orientation="horizontal"
  194.         android:layout_width="match_parent"
  195.         android:layout_height="wrap_content">
  196.        
  197.         <Button
  198.             android:id="@+id/idBtnRequestDate1"
  199.             android:text="@string/strBtnRequestDate1"
  200.             android:layout_weight="1"
  201.             android:layout_width="match_parent"
  202.             android:layout_height="wrap_content"/>
  203.         <Button
  204.             android:id="@+id/idBtnRequestDate2"
  205.             android:text="@string/strBtnRequestDate2"
  206.             android:layout_weight="1"
  207.             android:layout_width="match_parent"
  208.             android:layout_height="wrap_content"/>
  209.     </LinearLayout>
  210.    
  211.     <LinearLayout
  212.         android:orientation="horizontal"
  213.         android:id="@+id/idLlInfoRecvDates"
  214.         android:layout_below="@id/idLlDatesRequester"
  215.         android:layout_width="match_parent"
  216.         android:layout_height="wrap_content">
  217.  
  218.         <TextView
  219.             android:id="@+id/idTvDate1"
  220.             android:layout_weight="1"
  221.             android:layout_width="match_parent"
  222.             android:layout_height="wrap_content"/>
  223.         <TextView
  224.             android:id="@+id/idTvDate2"
  225.             android:layout_weight="1"
  226.             android:layout_width="match_parent"
  227.             android:layout_height="wrap_content"/>
  228.     </LinearLayout>
  229.    
  230.     <Button
  231.         android:id="@+id/idBtnComputeDistance"
  232.         android:text="@string/strBtnComputeDistance"
  233.         android:layout_below="@id/idLlInfoRecvDates"
  234.         android:layout_width="match_parent"
  235.         android:layout_height="wrap_content"/>
  236.    
  237.     <ListView
  238.         android:layout_below="@id/idBtnComputeDistance"
  239.         android:layout_width="match_parent"
  240.         android:layout_height="wrap_content"/>
  241.  
  242. </RelativeLayout>
Add Comment
Please, Sign In to add comment