Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.joythis.android.distancebetweendates;
- import androidx.appcompat.app.AppCompatActivity;
- import android.content.Context;
- import android.content.Intent;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.widget.ArrayAdapter;
- import android.widget.Button;
- import android.widget.ListView;
- import android.widget.NumberPicker;
- import java.util.ArrayList;
- import java.util.Calendar;
- public class DateInputActivity extends AppCompatActivity {
- Context mContext;
- NumberPicker.OnValueChangeListener mNpValueChangedHandler =
- new NumberPicker.OnValueChangeListener() {
- @Override
- public void onValueChange
- (NumberPicker picker, int oldVal, int newVal) {
- int y = mNpYear.getValue();
- int m = mNpMonth.getValue();
- int d = mNpDay.getValue();
- boolean bValidDate = AmCalendar.validDay(y,m,d);
- int iMaxDayForMonth = AmCalendar.
- numberOfDaysInMonth(y,m);
- mNpDay.setMaxValue(iMaxDayForMonth);
- String strMsg = "";
- if (d>iMaxDayForMonth){
- strMsg = getString(
- R.string.strFixedDay,
- d,
- iMaxDayForMonth
- );
- mUserMsgs.add(0, strMsg);
- }
- if (!bValidDate){
- strMsg = getResources().getString(
- R.string.strInvalidDate,
- y,m,d
- );
- mUserMsgs.add(0, strMsg);
- mNpDay.setValue(iMaxDayForMonth);
- //mNpDay.setMaxValue(iMaxDayForMonth);
- mAd.notifyDataSetChanged();
- }//
- }//onValueChange
- };
- NumberPicker mNpYear, mNpMonth, mNpDay;
- Button mBtnConfirmDate;
- ListView mLvFeedback;
- ArrayList<String> mUserMsgs;
- ArrayAdapter<String> mAd;
- Object[] maRelevant;
- Button.OnClickListener mBtnClickHandler =
- new Button.OnClickListener() {
- @Override
- public void onClick(View v) {
- int iYear = mNpYear.getValue();
- int iMonth = mNpMonth.getValue();
- int iDay = mNpDay.getValue();
- String strMsg =
- getResources().getString(
- R.string.strUserDate,
- iYear,
- iMonth,
- iDay
- );
- mUserMsgs.add(0, strMsg);
- mAd.notifyDataSetChanged();
- Intent responseForCallers = new Intent();
- responseForCallers.putExtra(
- KEY_DATE,
- strMsg
- );
- setResult(
- RESULT_OK,
- responseForCallers
- );
- finish();
- }//onClick
- };
- public final static String KEY_DATE = "KEY_DATE";
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.date_input_cl);
- init();
- }//onCreate
- Object[] getImportant(){
- Object[] aRelevant = {
- mNpYear,
- mNpMonth,
- mNpDay,
- mBtnConfirmDate,
- mLvFeedback
- };
- return aRelevant;
- }//getImportant
- void init(){
- mContext = this;
- mNpYear = findViewById(R.id.idNpYear);
- mNpMonth = findViewById(R.id.idNpMonth);
- mNpDay = findViewById(R.id.idNpDay);
- mBtnConfirmDate = findViewById(R.id.idBtnConfirmDate);
- mLvFeedback = findViewById(R.id.idLvFeedback);
- maRelevant = this.getImportant();
- boolean bAllAreOK = true;
- for (Object o : maRelevant){
- boolean bOK = o != null;
- bAllAreOK = bAllAreOK && bOK;
- }//for
- if (!bAllAreOK){
- Log.e(
- "@"+getClass().getName(),
- "1+ object(s) are null"
- );
- finish();
- return;
- }//if
- mNpYear.setMinValue(1);
- mNpYear.setMaxValue(9999);
- mNpYear.setValue(Calendar.getInstance().get(Calendar.YEAR));
- mNpMonth.setMinValue(1);
- mNpMonth.setMaxValue(12);
- mNpMonth.setValue(Calendar.getInstance().get(Calendar.MONTH)+1);
- mNpDay.setMinValue(1);
- mNpDay.setMaxValue(31); //TODO
- mNpDay.setValue(
- Calendar.getInstance().
- get(Calendar.DAY_OF_MONTH)
- );
- mUserMsgs = new ArrayList<>();
- mAd = new ArrayAdapter<>(
- mContext,
- android.R.layout.simple_list_item_1,
- mUserMsgs
- );
- mLvFeedback.setAdapter(mAd);
- mBtnConfirmDate.setOnClickListener(
- mBtnClickHandler
- );
- //assigned the onChangeChangeListener to all the NumberPickers
- NumberPicker[] aTrio = {mNpYear, mNpMonth, mNpDay};
- for (NumberPicker np : aTrio) np.
- setOnValueChangedListener(mNpValueChangedHandler);
- }//init
- }
- //**
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- <LinearLayout
- android:id="@+id/idLlDatesRequester"
- android:orientation="horizontal"
- android:layout_width="match_parent"
- android:layout_height="wrap_content">
- <Button
- android:id="@+id/idBtnRequestDate1"
- android:text="@string/strBtnRequestDate1"
- android:layout_weight="1"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"/>
- <Button
- android:id="@+id/idBtnRequestDate2"
- android:text="@string/strBtnRequestDate2"
- android:layout_weight="1"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"/>
- </LinearLayout>
- <LinearLayout
- android:orientation="horizontal"
- android:id="@+id/idLlInfoRecvDates"
- android:layout_below="@id/idLlDatesRequester"
- android:layout_width="match_parent"
- android:layout_height="wrap_content">
- <TextView
- android:id="@+id/idTvDate1"
- android:layout_weight="1"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"/>
- <TextView
- android:id="@+id/idTvDate2"
- android:layout_weight="1"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"/>
- </LinearLayout>
- <Button
- android:id="@+id/idBtnComputeDistance"
- android:text="@string/strBtnComputeDistance"
- android:layout_below="@id/idLlInfoRecvDates"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"/>
- <ListView
- android:layout_below="@id/idBtnComputeDistance"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"/>
- </RelativeLayout>
Add Comment
Please, Sign In to add comment