Search code examples
javaandroidclassandroid-dialogfragment

Reading Selected Date without DatePickeView in android studio


I am Parvanshu Sharma and I am a beginner to programming, I am making an application in which I have done that on button click a date Picker dialog will appear and the button text will be set to the selected by the user, Currently there are 3 buttons for this and I even have a realtime database for uploading this date on Firebase, but now I am having problems in getting the every unique date and uploading it, I am not able to get the selected date.

And as my every stackoverflow question has-

MainActivity.java (Its too big so I haven't shown imports)

    public class MainActivity extends AppCompatActivity implements DatePickerDialog.OnDateSetListener, AdapterView.OnItemSelectedListener {
    
        String CurrentDateString;
        TextView mainDate;
        Integer OrderQuantity = 3;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            Button SelectDate1 = findViewById(R.id.SelectDateButton1);
            Button SelectDate2 = findViewById(R.id.SelectDateButton2);
            Button SelectDate3 = findViewById(R.id.SelectDateButton3);
    
            SelectDate1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    DialogFragment datePicker = new DatePickerFragment();
                    datePicker.show(getSupportFragmentManager(), "Pick item order date");
    
                    mainDate = SelectDate1;
                }
            });
    
            SelectDate2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    DialogFragment datePicker = new DatePickerFragment();
                    datePicker.show(getSupportFragmentManager(), "Pick item order date");
    
                    mainDate = SelectDate2;
                }
            });
    
            SelectDate3.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    DialogFragment datePicker = new DatePickerFragment();
                    datePicker.show(getSupportFragmentManager(), "Pick item order date");
    
                    mainDate = SelectDate3;
                }
            });
    
            ArrayAdapter<CharSequence> FoodAdapter = ArrayAdapter.createFromResource(this, R.array.FoodList, android.R.layout.simple_spinner_item);
            FoodAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    
            Spinner SelectItem1 = findViewById(R.id.SelectItem1);
            SelectItem1.setAdapter(FoodAdapter);
            SelectItem1.setOnItemSelectedListener(this);
    
            Spinner SelectItem2 = findViewById(R.id.SelectItem2);
            SelectItem2.setAdapter(FoodAdapter);
            SelectItem2.setOnItemSelectedListener(this);
    
            Spinner SelectItem3 = findViewById(R.id.SelectItem3);
            SelectItem3.setAdapter(FoodAdapter);
            SelectItem3.setOnItemSelectedListener(this);
    
            ArrayAdapter<CharSequence> QuantityAdapter = ArrayAdapter.createFromResource(this, R.array.Quantity, android.R.layout.simple_spinner_item);
            QuantityAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    
            Spinner Quantity1 = findViewById(R.id.SelectQuantity1);
            Quantity1.setAdapter(QuantityAdapter);
            Quantity1.setOnItemSelectedListener(this);
    
            Spinner Quantity2 = findViewById(R.id.SelectQuantity2);
            Quantity2.setAdapter(QuantityAdapter);
            Quantity2.setOnItemSelectedListener(this);
    
            Spinner Quantity3 = findViewById(R.id.SelectQuantity3);
            Quantity3.setAdapter(QuantityAdapter);
            Quantity3.setOnItemSelectedListener(this);
    
            Button DoneButton = findViewById(R.id.DoneButton);
    
            EditText PersonName = findViewById(R.id.PersonName);
            EditText PersonPhone = findViewById(R.id.PersonPhone);
            EditText PersonAddress = findViewById(R.id.PersonAddress);
    
            FirebaseDatabase database = FirebaseDatabase.getInstance();
    
            DoneButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    DatabaseReference Name = database.getReference(PersonPhone.getText().toString() + "/Name");
                    Name.setValue(PersonName.getText().toString());
    
                    DatabaseReference Phone = database.getReference(PersonPhone.getText().toString() + "/Phone");
                    Phone.setValue(PersonPhone.getText().toString());
    
                    DatabaseReference Address = database.getReference(PersonPhone.getText().toString() + "/Address");
                    Address.setValue(PersonAddress.getText().toString());
    
                    if (Quantity1.getSelectedItem().toString().equals("0")) {
                        OrderQuantity -= 1;
                    }
    
                    if (Quantity2.getSelectedItem().toString().equals("0")) {
                        OrderQuantity -= 1;
                    }
    
                    if (Quantity3.getSelectedItem().toString().equals("0")) {
                        OrderQuantity -= 1;
                    }
    
                    DatabaseReference OrderQuantities = database.getReference(PersonPhone.getText().toString()+"/OrderQuantity");
                    OrderQuantities.setValue(OrderQuantity);
    
                    if (Quantity1.getSelectedItem().toString() != "0") {
                        //I want some solution HERE
                    }
                }
            });
        }
    
        @Override
        public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
            Calendar c = Calendar.getInstance();
            c.set(Calendar.YEAR, year);
            c.set(Calendar.MONTH, month);
            c.set(Calendar.DAY_OF_MONTH, dayOfMonth);
    
            SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy");
            String CurrentDateString = format.format(c.getTime());
            mainDate.setText(CurrentDateString);
        }
    
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        }
    
        @Override
        public void onNothingSelected(AdapterView<?> parent) {
        }
    }```
    
    and here goes my activity_main.xml
    ```<?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
    
        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">
    
                <TextView
                    android:id="@+id/NameTextView"
                    android:layout_width="93dp"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="20sp"
                    android:layout_marginLeft="20sp"
                    android:layout_marginTop="10sp"
                    android:text="Name"
                    android:textColor="@color/black"
                    android:textSize="28sp"
                    android:textStyle="bold" />
    
                <EditText
                    android:id="@+id/PersonName"
                    android:layout_width="320dp"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="20sp"
                    android:layout_marginLeft="20sp"
                    android:ems="10"
                    android:hint="Name"
                    android:inputType="textPersonName" />
    
                <TextView
                    android:id="@+id/PhoneTextView"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="20sp"
                    android:text="Mobile Number"
                    android:textColor="@color/black"
                    android:textSize="28sp"
                    android:textStyle="bold" />
    
                <EditText
                    android:id="@+id/PersonPhone"
                    android:layout_width="325dp"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="20sp"
                    android:ems="10"
                    android:hint="Mobile number"
                    android:inputType="phone" />
    
                <TextView
                    android:id="@+id/AddressTextView"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="20sp"
                    android:text="Address"
                    android:textColor="@color/black"
                    android:textSize="28sp"
                    android:textStyle="bold" />
    
                <EditText
                    android:id="@+id/PersonAddress"
                    android:layout_width="328dp"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="20sp"
                    android:ems="10"
                    android:hint="Delivery Address"
                    android:inputType="textPostalAddress" />
    
                <TextView
                    android:id="@+id/textView4"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_margin="20sp"
                    android:text="                         Order Details"
                    android:textColor="@color/black"
                    android:textSize="20sp"
                    android:textStyle="bold" />
    
                <TableLayout
    
                    android:id="@+id/MainTable"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="center"
                    android:stretchColumns="0,1,2"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent">
    
                    <TableRow
                        android:layout_width="fill_parent"
                        android:layout_height="0dp"
                        android:layout_margin="1dp"
                        android:layout_weight="1"
                        android:background="#000000"
    
                        >
    
                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:layout_column="0"
                            android:layout_margin="1dp"
                            android:background="#FFFFFF"
                            android:gravity="center"
                            android:text=" Date "
                            android:textAppearance="?android:attr/textAppearanceLarge"
                            android:textStyle="bold" />
    
                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="match_parent"
                            android:layout_column="1"
                            android:layout_margin="1dp"
                            android:background="#FFFFFF"
                            android:gravity="center"
                            android:text="Item"
                            android:textAppearance="?android:attr/textAppearanceLarge"
                            android:textStyle="bold" />
    
                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="match_parent"
                            android:layout_column="2"
                            android:layout_margin="1dp"
                            android:background="#FFFFFF"
                            android:gravity="center"
                            android:text="Quantity"
                            android:textAppearance="?android:attr/textAppearanceLarge"
                            android:textStyle="bold" />
                    </TableRow>
    
                    <TableRow
                        android:layout_width="fill_parent"
                        android:layout_height="0dp"
                        android:layout_margin="1dp"
                        android:layout_weight="1"
                        android:background="#000000">
    
                        <Button
                            android:id="@+id/SelectDateButton1"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:text="Select" />
    
                        <Spinner
                            android:id="@+id/SelectItem1"
                            android:layout_width="40dp"
                            android:layout_height="43dp"
                            android:layout_marginStart="3dp"
                            android:layout_marginLeft="3dp"
                            android:layout_marginEnd="2dp"
                            android:layout_marginRight="2dp"
                            android:background="@color/white"
                            android:gravity="center"
                            android:text="Select Item" />
    
                        <Spinner
                            android:id="@+id/SelectQuantity1"
                            android:layout_width="40dp"
                            android:layout_height="43dp"
                            android:background="@color/white"
                            android:gravity="center" />
                    </TableRow>
    
                    <TableRow
                        android:layout_width="fill_parent"
                        android:layout_height="0dp"
                        android:layout_margin="1dp"
                        android:layout_weight="1"
                        android:background="#000000">
    
                        <Button
                            android:id="@+id/SelectDateButton2"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:text="Select" />
    
                        <Spinner
                            android:id="@+id/SelectItem2"
                            android:layout_width="40dp"
                            android:layout_height="43dp"
                            android:layout_marginStart="3dp"
                            android:layout_marginLeft="3dp"
                            android:layout_marginEnd="2dp"
                            android:layout_marginRight="2dp"
                            android:background="@color/white"
                            android:gravity="center"
                            android:text="Select Item" />
    
                        <Spinner
                            android:id="@+id/SelectQuantity2"
                            android:layout_width="40dp"
                            android:layout_height="43dp"
                            android:background="@color/white"
                            android:gravity="center" />
    
                    </TableRow>
    
                    <TableRow
                        android:layout_width="fill_parent"
                        android:layout_height="0dp"
                        android:layout_margin="1dp"
                        android:layout_weight="1"
                        android:background="#000000">
    
                        <Button
                            android:id="@+id/SelectDateButton3"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:text="Select" />
    
                        <Spinner
                            android:id="@+id/SelectItem3"
                            android:layout_width="40dp"
                            android:layout_height="43dp"
                            android:layout_marginStart="3dp"
                            android:layout_marginLeft="3dp"
                            android:layout_marginEnd="2dp"
                            android:layout_marginRight="2dp"
                            android:background="@color/white"
                            android:gravity="center" />
    
                        <Spinner
                            android:id="@+id/SelectQuantity3"
                            android:layout_width="40dp"
                            android:layout_height="43dp"
                            android:background="@color/white"
                            android:gravity="center" />
    
                    </TableRow>
    
                </TableLayout>
    
                <Button
                    android:id="@+id/DoneButton"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Done" />
    
            </LinearLayout>
        </ScrollView>
    
    
    </androidx.constraintlayout.widget.ConstraintLayout>

and I have one more class for creating DatePickerDialog, the source from where I learnt and implemented that how to make DatePickerDialog is Here

which goes here- (DatePickerFragment.java)

    public class DatePickerFragment extends DialogFragment {
        @NonNull
        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            Calendar c = Calendar.getInstance();
            int year = c.get(Calendar.YEAR);
            int month = c.get(Calendar.MONTH);
            int day = c.get(Calendar.DAY_OF_MONTH);
            return new DatePickerDialog(getActivity(), (DatePickerDialog.OnDateSetListener) getActivity(), year, month, day);
        }
    }

Solution

  • Tried using an interface. You will have all 3 dates in your main activity, now you can do whatever with them

    public class DatePickerFragment extends DialogFragment {
    
        private static applyDate mInterface;
        private static int buttonNumberHere;
    
        public interface applyDate {
            void setDate(int selectedYear, int selectedMonth, int selectedDay, int buttonNumber);
        }
    
        public static DatePickerFragment newInstance(int buttonNumber, applyDate context) 
        {
        
            DatePickerFragment fragment = new DatePickerFragment();
            mInterface = ((applyDate) context);
            buttonNumberHere = buttonNumber;
            return fragment;
        }
    
        @NonNull
        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            Calendar c = Calendar.getInstance();
            int year = c.get(Calendar.YEAR);
            int month = c.get(Calendar.MONTH);
            int day = c.get(Calendar.DAY_OF_MONTH);
            return new DatePickerDialog(getContext(), datePickerListener, year, month, day);
        }
        
    
        private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {
    
            public void onDateSet(DatePicker view, int selectedYear,
                              int selectedMonth, int selectedDay) {
                mInterface.setDate(selectedYear, selectedMonth, selectedDay, buttonNumberHere);
            }
        };
    

    In MainActivity

    public class MainActivity extends AppCompatActivity implements applyDate, AdapterView.OnItemSelectedListener {
        
            String CurrentDateString;
            TextView mainDate;// Idk what is this
            Integer OrderQuantity = 3;
            String itemOneDate;
            String itemTwoDate;
            String itemThreeDate;
        
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
        
                Button SelectDate1 = findViewById(R.id.SelectDateButton1);
                Button SelectDate2 = findViewById(R.id.SelectDateButton2);
                Button SelectDate3 = findViewById(R.id.SelectDateButton3);
        
                SelectDate1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        DatePickerFragment datePicker = DatePickerFragment.newInstance(1, MainActivity.this);
                        datePicker.show(getSupportFragmentManager(), "Pick item order date");
        
                        mainDate = SelectDate1;
                    }
                });
        
                SelectDate2.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        DatePickerFragment datePicker = DatePickerFragment.newInstance(2, MainActivity.this);
                        datePicker.show(getSupportFragmentManager(), "Pick item order date");
        
                        mainDate = SelectDate2;
                    }
                });
        
                SelectDate3.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        DatePickerFragment datePicker = DatePickerFragment.newInstance(3, MainActivity.this);
                        datePicker.show(getSupportFragmentManager(), "Pick item order date");
        
                        mainDate = SelectDate3;
                    }
                });
        
                ArrayAdapter<CharSequence> FoodAdapter = ArrayAdapter.createFromResource(this, R.array.FoodList, android.R.layout.simple_spinner_item);
                FoodAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        
                Spinner SelectItem1 = findViewById(R.id.SelectItem1);
                SelectItem1.setAdapter(FoodAdapter);
                SelectItem1.setOnItemSelectedListener(this);
        
                Spinner SelectItem2 = findViewById(R.id.SelectItem2);
                SelectItem2.setAdapter(FoodAdapter);
                SelectItem2.setOnItemSelectedListener(this);
        
                Spinner SelectItem3 = findViewById(R.id.SelectItem3);
                SelectItem3.setAdapter(FoodAdapter);
                SelectItem3.setOnItemSelectedListener(this);
        
                ArrayAdapter<CharSequence> QuantityAdapter = ArrayAdapter.createFromResource(this, R.array.Quantity, android.R.layout.simple_spinner_item);
                QuantityAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        
                Spinner Quantity1 = findViewById(R.id.SelectQuantity1);
                Quantity1.setAdapter(QuantityAdapter);
                Quantity1.setOnItemSelectedListener(this);
        
                Spinner Quantity2 = findViewById(R.id.SelectQuantity2);
                Quantity2.setAdapter(QuantityAdapter);
                Quantity2.setOnItemSelectedListener(this);
        
                Spinner Quantity3 = findViewById(R.id.SelectQuantity3);
                Quantity3.setAdapter(QuantityAdapter);
                Quantity3.setOnItemSelectedListener(this);
        
                Button DoneButton = findViewById(R.id.DoneButton);
        
                EditText PersonName = findViewById(R.id.PersonName);
                EditText PersonPhone = findViewById(R.id.PersonPhone);
                EditText PersonAddress = findViewById(R.id.PersonAddress);
        
                FirebaseDatabase database = FirebaseDatabase.getInstance();
        
                DoneButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
    
                        //Idk how to set date but now you have all three dates
    
                        DatabaseReference dateOne = database.getReference(itemOneDate + "/DateOne");
                        dateOne.setValue(itemOneDate);
    
                        DatabaseReference dateTwo = database.getReference(itemOneDate + "/DateTwo");
                        dateTwo.setValue(itemTwoDate);
    
                        DatabaseReference dateThree = database.getReference(itemThreeDate + "/DateThree");
                        dateThree.setValue(itemThreeDate);
    
                        DatabaseReference Name = database.getReference(PersonPhone.getText().toString() + "/Name");
                        Name.setValue(PersonName.getText().toString());
        
                        DatabaseReference Phone = database.getReference(PersonPhone.getText().toString() + "/Phone");
                        Phone.setValue(PersonPhone.getText().toString());
        
                        DatabaseReference Address = database.getReference(PersonPhone.getText().toString() + "/Address");
                        Address.setValue(PersonAddress.getText().toString());
        
                        if (Quantity1.getSelectedItem().toString().equals("0")) {
                            OrderQuantity -= 1;
                        }
        
                        if (Quantity2.getSelectedItem().toString().equals("0")) {
                            OrderQuantity -= 1;
                        }
        
                        if (Quantity3.getSelectedItem().toString().equals("0")) {
                            OrderQuantity -= 1;
                        }
        
                        DatabaseReference OrderQuantities = database.getReference(PersonPhone.getText().toString()+"/OrderQuantity");
                        OrderQuantities.setValue(OrderQuantity);
        
                        if (Quantity1.getSelectedItem().toString() != "0") {
                            //I want some solution HERE
                        }
                    }
                });
            }
    
            @Override
            public void setDate(int selectedYear, int selectedMonth, int selectedDay, int buttonNumber){
                Calendar c = Calendar.getInstance();
                c.set(Calendar.YEAR, selectedYear);
                c.set(Calendar.MONTH, selectedMonth);
                c.set(Calendar.DAY_OF_MONTH, selectedDay);
    
                SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy");
                String CurrentDateString = format.format(c.getTime());
                mainDate.setText(CurrentDateString);
    
                if (buttonNumber == 1){
                    itemOneDate = CurrentDateString;
                }
                else if (buttonNumber == 2){
                    itemTwoDate = CurrentDateString;
                }
                else if (buttonNumber == 3){
                    itemThreeDate = CurrentDateString;
                }
            }
    }