How to make a DatePicker with DialogFragment and FragmentManager? - android

So I have been at this for a while, and I cannot seem to figure it out. I am fairly new at Android development, so bear with me please. I wasn't too familiar with creating a Datepicker and I learned to do it the deprecated way just to get the hang of it. Used this tutorial to get me up to speed:
http://developer.android.com/resources/tutorials/views/hello-datepicker.html
But now I need to change it, mainly to not use deprecated code, so I looked all around, and I found 2 tutorials, mainly this one though:
http://www.kylebeal.com/2011/11/android-datepickerdialog-and-the-dialogfragment/
Problem is, there is code that does not make sense. Mainly this part:
//create new DateDialogFragment
DateDialogFragment ddf = DateDialogFragment.newInstance(this, R.string.set_date, date);
ddf.setDateDialogFragmentListener(new DateDialogFragmentListener() {
#Override
public void dateDialogFragmentDateSet(Calendar date) {
// update the fragment
mDateDetailFragment.updateDate(date);
}
});
ddf.show(getSupportFragmentManager(), "date picker dialog fragment");
That is suppose to be what is running in the activity class that it runs in. So I kind of get whats happening throughout the rest of the code, and feel free to look at the rest as it will probably help. The code is essentially following almost the same route as the original showDialog() code. Implementing DatePickerDialog.onDateSetListener, and returning a new DatePickerDialog, with the obvious exception thats its using a DialogFragment. But that is where I have gotten lost.
Part of the reason is because I am not really sure where onCreateDialog() returns that new DatePickerDialog. If you look at the code I said does not make sense, both mDateDetailFragment and getSupportFragmentManager() were never instantiated or appeared anywhere else, so I cannot get the tutorial code to even work. The Android Doc also mentions that instead of using showDialog() I should be using both DialogFragment and FragmentManager, which is also not done here. Only DialogFragment.
So like the title, how exactly can I make a DatPicker in the non-depracted form? I would please ask to NOT send me to a link and just say look here. I have done that. Believe me. If not, at least explain what I have missed that makes this make sense. Also, if anyone can actually tell me where the onCreateDialog() method sends the DatePickerDialog, that would be extremely helpful as well. I am just trying to make sense of all this, rather than copy and paste code (not that it works anyways). Thanks a lot for taking the time out to read this, and if any clarifications needed, please ask.

Going in order of your post... \
1) the onCreateDialog returns the dialog to the DialogFragment.
2) I don't know about the mDateDetailFragment.updateDate(date); line... I think he might have left some code out. EDIT Found it, it's in one of the classes in the zip file for his complete demo.
3) You are using the fragment manager (it's the getSupportFragmentManager() part):
ddf.show(getSupportFragmentManager(), "date picker dialog fragment");
Ok, that said, here's how I've done it (I'm using a DateSlider, but subbing in a Datepicker in it's place should be simple enough for you).
In the fragment/activity calling the DialogFragment:
DialogFragment newFragment = new DateDialog();
newFragment.show(getFragmentManager(), "dialog");
And my DialogFragment:
public class DateDialog extends DialogFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
private DateSlider.OnDateSetListener mDateSetListener = new DateSlider.OnDateSetListener() {
public void onDateSet(DateSlider view, Calendar selectedDate) {
MemberAddFragment.startTxt.setText(String.format("%tB %te, %tY", selectedDate, selectedDate, selectedDate));
}
};
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Calendar c = Calendar.getInstance(); // Get calendar instance to use in DateSlider
return new AlternativeDateSlider(getActivity(),mDateSetListener,c,null,null); // return DateSlider dialog to DialogFragment
}
}
Hope this helps clear up your confusion.

I know this question is mine and already has an answer. But I thought I'd add my own answer to others looking for help on this same issue. I have simplified the example code that I provided above into only 2 classes. Kbeal, who was gracious enough to provide the tutorial did a great job, but it was too bloated with information that was not even necessary. So using what I knew about dialogs and what I learned from what I could understand from the Kbeal's tutorial as well as the research I did on fragments, I have provided my implementation from a DatePickerDialog on my GitHub.
https://github.com/Zeroe31890/DateDialogFragment
All you honestly have to do is run it straight after only copy and pasting the 2 classes, the single layout, and modifying the Android Manifest file. Much easier than any of the other tutorials. Oh and if anybody sees anything that can be done better on it please do. Enjoy!

Author of the post here: Sounds like you have it all figured out. I replied to your questions on my post before finding this though Barak does a fine job of answering them. If you haven't done so already download the complete source for the example app. I think it will help fill any missing blanks you still have.

Related

what is DATE_DIALOG_ID in DatePickerDialog examples

I'm an Android beginner. Several examples on DatePickerDialog have a line of code such as DATE_DIALOG_ID = 999. (https://www.mkyong.com/android/android-date-picker-example/). This number is different in each example. I Googled this but couldn't find anything helpful.
I'm having trouble in general, understanding how to use DatePickerDialog. Is there a simplified tutorial available anywhere? All the examples I've seen so far have little description or code comments.
That is a unique identifier for that dialog, so you can match against it in onCreateDialog. This method of showing Dialogs in deprecated in Android, and it is recommended instead to use DialogFragment, as seen here:
https://developer.android.com/guide/topics/ui/controls/pickers.html

Confused on MainActivity.java again

So I'm still fairly new to Android Studio and programming in general and I'm trying to follow this course by Udacity. The problem is that in MainActivity.java all it says in mine is
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// ...
}
while they have more Override Methods such as OnOptionsItemSelected, etc. I believe I was told by them to set it to "Blank Activity", which I believe is "Empty Activity" now, and I think the problem is the updates that have been made since there video had been made and now, but I'm just looking for a solution for this. Any help would be appreciated. I hope I am making sense here Thanks.
What you're looking for is "Basic Activity" instead of "Empty Activity" and you should be able to continue following the tutorial.
If they have the xml files and java files available, once you copy and paste it should look the same as well.
Sounds like you didn't really know about Android's coding framework, and need to read some example or tutorial.
I'm not have any relation about the following site, but as my point of view, it's a good Android coding tutorial.
Android - Hello World Example
OnOptionItemSelected is a callback method which handles menus.
You only need the onCreate callback method in order to display a sample Hello world! and then follow the Udacity course.
You can erase the others if you have created a blank activity instead of an empty one.

Android: getIntent() is deprecated

My program consists of a MainActivity and two fragment activities. I need one fragment to take a String value from the user and pass it to the second fragment.
I am trying to wrap my head around how to do this. Since I am familiar with intents, I found this answer on another post and decided to try it out. Everything looks fine until I get to step 4, when I try to use Intent i = getIntent(); in my second fragment, Studio won't let me use it and says "getIntent(java.lang.String) is deprecated".
This doesn't make sense to me since I have used getIntent() in other programs without issue, and it is letting me use it in my MainActivity (step 2 from the other post) without screaming at me.
I know this can be done without using intents, but I can't figure it out and can't find any really thorough tutorials in order to do so. So I guess my questions are:
Can I make intents work for this purpose still? What should I do to get around this deprecation issue?
Any other advice, explanations, or links to "explain it like I'm 5" tutorials would be very helpful and welcome. I have Googled and read a few, but I am still not understanding this and am becoming increasingly frustrated. It seems like this should be a relatively simple concept.
It is too late for answer but still I am providing my answer for other persons. It is happen because Intent is basically work with an activity. And fragments are not activity, but attached to activity. So simply you need to do this:
Intent intent=getActivity().getIntent();
Having the same problem while passing Object from an Activity to a Java Class.
Here is what I did
This Activity sends data
Salary newSalary = new Salary();
Intent intent = new Intent(ViewData.this,Data.class);
intent.putExtra("SalaryObj", newSalary);
It recieves data(In Data.class)
Here I tried this but Android Studio says getIntent is deprecatedIntent intent = Intent.getIntent();
So What can I use in place of getIntent(), because all the solutions I find on Internet to this problem, uses getIntent().
EDIT:
I was playing around with this and found that I was trying to receive data in Java Class(Not an Activity). But when I used it in an Activity then it works fine. But it takes me to another question that how to send data from an Activity to Java Class(which is not an Activity).
On your onCreate
val bundle = intent.extras
if (bundle != null) {
idEmployee = bundle?.getString("idEmployee", "")
idClient = bundle?.getString("idClient", "")
listAvailable = bundle?.getStringArrayList("listAvailable") as ArrayList<String>
Log.i("list:", "$listAvailable" )
}
It means, that this method could not be supported in further releases. The method is still in the API for backward compability for an unspecified amount of time. Mostly it is dangerous to use deprecated methods or there is a better way to achieve this.
Like it is described here
In this case you should rather use: parseUri(String, int) to achieve this ( according to the android developer api).

WindowManager.LayoutParams why is there a setTitle method?

So yesterday i was programming and suddenly i came across the setTitle method in the WindowManager.LayoutParams class, why would this class like this have a setTitle method? Where is is used for? The documentation does not say anything about the function.
My guess is that it could be used for debugging or something a like, but other than that i don't have a clue.
And why would it be declared as final method? Because it's not ready yet for us to override it?
Just wondering...
Note: Both answers below are some how "correct" and offer good information, but can only accept one.
getTitle
setTitle
Going through the source code for WindowManager, it seems that the title isn't actually used anywhere, except in the debug and parcel writing methods.
Furthermore, searching google for +"windowmanager.layoutparams" +".setTitle()" site:grepcode.com doesn't seem to return any results where the setTitle() and getTitle() methods of WindowManager.LayoutParams are actually used in production code.
I'd say that the Android engineers felt that maybe at some point of time in the future they might need a title property, and put it in as a stub.
Well There doen't seem to be any usage for this layout parameter value anywhere.
It seems like a place holder for now.
I only managed to find these tow cases where it has been used:
SoftInputWindow
and
StatusBar Service
And visually both has no effect (at least on my device and emulator)

NullPointerException's on StartActivityForResult, Struggling with SQLite in android i think?

I have one class called Budget.java, within that i start Keypad.java. This was all working fine up until recently when i added a bunch of code to Keypad.java (The added code was to update a row in my SQLite database on the press of a button, all the unrelated methods were working until i tried to implement this). Now using breakpoints i think i've figured out that i get the error message as soon as i try to open the Keypad activity and i don't have a clue what could be the problem.
Maybe it's my misunderstanding of the sqlite open helper? Or perhaps its because i'm using StartActivityForResult?
Any suggestions would be very appreciated! I can upload the logcat if you think that would help.
I uploaded the two little classes to pastebin, you might find it easier to read?
Budget.java ( look for ListItemCommonIntent )
keypad.java
In your Keypad.java, you have the following outside the onCreate:
EditText userAmount=(EditText)this.findViewById(R.id.cost_input);
This wont work because you have to use the setContentView to reference the layout where you want to find the view. And when it initializes userAmount, the object is not available yet (so this is null) .
Try this:
private EditText userAmount;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.keypad);
userAmount=(EditText)findViewById(R.id.cost_input);
MySpinner();
Main();
}

Categories

Resources