Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] android – Can you get the tag of a date picker

Posted on November 11, 2022 By

Solution 1 :

By calling datePicker.getTag(), you are getting the wrong tag; which is the DatePicker tag, not the DatePickerFragment tag.

To see the right tag, just use getTag() inside your custom DatePickerFragment

getTag() inside my Fragement class works fine. But how to I pass the
tag back to my activity where I called show() My Fragement currently
only contains the onCreateDialog method where I return a new
DatePickerDialog instance

You can pass a listener to your DatePickerFragment constructor, and trigger its callback with the Fragment tag whenever you set a date.

public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {

    interface OnDateAdjustedListener {
        void onDateAdjusted(String tag);
    }

    private OnDateAdjustedListener mOnDateAdjustedListener;


    // Constructor with a Listener
    public DatePickerFragment(OnDateAdjustedListener onDateAdjustedListener) {
        mOnDateAdjustedListener = onDateAdjustedListener;
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        // .... Handle onCreateDialog

    }


    @Override
    public void onDateSet(DatePicker datePicker, int year, int month, int dayOfMonth) {

        // .... Handle onDateSet

        // callback with fragment tag 
        mOnDateAdjustedListener.onDateAdjusted(getTag());
    }


}

And in your activity modify the constructor, and implement the interface

public class MainActivity extends AppCompatActivity implements View.OnClickListener, DatePickerFragment.OnDateAdjustedListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        DatePickerFragment dateFragment = new DatePickerFragment(this);


    }

    @Override
    public void onDateAdjusted(String tag) {
        Toast.makeText(this, tag, Toast.LENGTH_SHORT).show();
    }
}

Problem :

I am opening different date picker fragments in my android app.

The fragement is opened like this.

DialogFragment datePicker = new DatePickerFragment();
datePicker.show(getSupportFragmentManager(), "MY_TAG");

The I implemented the onDateSet method which has the picked date and a DatePicker object. Now I tried to get the tag from the DatePicker object to check which picker has been shown.

There is a method datePicker.getTag(), but this returns null.

Is there a way to get the tag which has been commited in the show method?

READ  [FIXED] android - Can we have a 2 different textStyle inside styles.xml?
Powered by Inline Related Posts

Comments

Comment posted by CodingLeo

getTag()

Android Tags:android, datepicker

Post navigation

Previous Post: [FIXED] android – How to achieve ionic permissions on first load
Next Post: [FIXED] android – How do i put online database into local database

Related Posts

[FIXED] android – How to update listview Android
[FIXED] android – Error: Execution failed for task app:mergeDebugResources Android
[FIXED] Android Studio , how to control a website through a mobile app Android
[FIXED] android – How to delete file in native code passed from Java code? Android
[FIXED] java – Android: Listener for Boolean change Android
[FIXED] viewmodel – android fragment livedata remove observer not working Android

Archives

  • April 2023
  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • Can Vicks humidifier be used without filter?
  • What color is Spanish green?
  • How old is Jamie in The War That Saved My Life?
  • When should I use scalp massager for hair growth?
  • Can I put polyurethane over liming wax?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme