How to update dialogue in android - android

I have a popup dialog where the user inputs data, including a date. To select the date, I have a button to open another window with a date picker. When I select the date and it returns to the first dialog, the text field with the date is not changed unless I open the date picker a second time. How would I refresh or update the first dialog immediately after I return to it from the date picker window?
Here is the code for the first dialog:
public void addEntry(View view) {
final Dialog d = new Dialog(this);
d.setContentView(R.layout.dialog);
d.setTitle("Add Entry");
d.setCancelable(true);
d.show();
...
chooseDate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String str = selectDate();
date.setText(str);
}
});
}
Here is the code for the second window where you would choose the date:
public String selectDate(){
final Dialog datePicker = new Dialog(this);
datePicker.setContentView(R.layout.choose_date);
datePicker.setTitle("Choose Date...");
datePicker.setCancelable(true);
datePicker.show();
Button selectFinalDate = (Button) datePicker.findViewById(R.id.selectDate);
final DatePicker dp = (DatePicker) datePicker.findViewById(R.id.datePicker1);
selectFinalDate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
strDateTime = (dp.getMonth() + 1) + "/" + dp.getDayOfMonth() + "/" + dp.getYear();
datePicker.dismiss();
}
});
return strDateTime;
};
Thanks!!

Put date.setText(datechoosen) in the second dialog on click and it will set the textView or button or whatever view that shows the date on the first dialog. Make it static in case it is not accessible in the second dialog but check if it is not null before accessing it.

not tested, but should work.
...
date.setText(str);
view.invalidate(); // the view that you are showing in the dialog
...
so in your code, you update this :
/**
* global variable for your dialog view
*/
View view =null;
// in your addEntry(View view)
...
Dialog d = new Dialog(this);
view = LayoutInflater.from(this).inflate(R.layout.dialog, null);
d.setContentView(view);
...
// selectDate()
...
date.setText(str);
view.invalidate(); // the view that you are showing in the dialog
...
see this answer

Related

How to reset input to DialogFramgment after initial build?

EDIT: Attached the code for my onCickListener which sends Rating Value to and then Show my Dialog.
I have a TextView which shows me a rating in numbers format (4.5). And when I press this TextView a dialog pops up to let me change the rating trough a RatingBar. The Ratingbar`s rating is set to equal the TextView Rating when it pops up. This functions as expected and the TextView is updated to the new rating when I press OK. BUT when I press the TextView again, the initial first value is shown and not the value which I just updated it to. I have figured out as much as this is because I have all my code within the onCreateDialog (). I have tried to get this to work by using OnStart() and onResume() but then my app crashes. How do I write this code correctly?
Attached is my functional code with all code set within the onCreadeDialog()
public class RatingDialog extends DialogFragment{
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Get the layout inflater
LayoutInflater inflater = getActivity().getLayoutInflater();
// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
final View DialogView = inflater.inflate(R.layout.dialog_rating, null);
/**
* Retrieve the argument "num" (Previously rating) and set ratingbar´s rating equal to this.
*/
getArguments().getFloat("num");
RatingBar ValueView = (RatingBar) DialogView.findViewById(R.id.Ratingbar);
ValueView.setRating(getArguments().getFloat("num"));
builder.setView(DialogView)
// Add action buttons
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
/**
* Get the new value from the Ratingbar and send this back to the AddRating TextView
*/
RatingBar ValueView = (RatingBar) DialogView.findViewById(R.id.Ratingbar);
float Value = ValueView.getRating();
TextView Text = (TextView) getActivity().findViewById(R.id.AddRating);
Text.setText(String.valueOf(Value));
RatingDialog.this.getDialog().cancel();
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
RatingDialog.this.getDialog().cancel();
}
});
return builder.create();
}
}
Below is the code for onCickListener which sends Rating Value to and then Show my Dialog:
/**
* Set the On Click Listener and send the Rating value to the Dialog
*/
final TextView Rating = (TextView) findViewById(R.id.AddRating);
String S = (String) Rating.getText();
final Float F;
if (S==""){
F=0.0f;}
else
F = Float.valueOf(S);
Rating.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
RatingDialog newFragment = new RatingDialog();
newFragment.show(getSupportFragmentManager(), "Rating");
/**
* Send Verdien av rating til dialogvinduet
*/
Bundle args = new Bundle();
args.putFloat("num", F);
newFragment.setArguments(args);
}
});
You are passing same F value all the time.
It should be:
final TextView rating = (TextView) findViewById(R.id.AddRating);
rating.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
RatingDialog newFragment = new RatingDialog();
newFragment.show(getSupportFragmentManager(), "Rating");
/**
* Send Verdien av rating til dialogvinduet
*/
Bundle args = new Bundle();
args.putFloat("num", TextUtils.isEmpty(rating.getText()) ?
0.0f : Float.valueOf(rating.getText().toString()));
newFragment.setArguments(args);
}
});
In that case you will pass actual value of rating TextView.
And yes, please follow java code convention. Because it's hard to read your code.

Android: How to set time from time picker in two different EditText fields (start/end)?

I am trying to get the time when the user clicks on the EditText StartTime and EndTime. The problem is that I don't know how to distinguish the EditTexts at the TimePickerFragment. Any help, please?
public void initializeTime () {
startTimeEditText = (EditText) findViewById(R.id.startTimeEditText);
startTimeEditText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Initialize a new time picker dialog fragment
DialogFragment dFragment = new TimePickerFragment();
// Show the time picker dialog fragment
dFragment.show(getSupportFragmentManager(),"TimePicker");
}
});
endTimeEditText = (EditText) findViewById(R.id.endTimeEditText);
endTimeEditText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Initialize a new time picker dialog fragment
DialogFragment dFragment = new TimePickerFragment();
// Show the time picker dialog fragment
dFragment.show(getSupportFragmentManager(),"TimePicker");
}
});
}
TimePickerFragment.java:
#Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// Do something with the time chosen by the user
EditText startTimeEditText = (EditText) getActivity().findViewById(R.id.startTimeEditText);
EditText endTimeEditText = (EditText) getActivity().findViewById(R.id.endTimeEditText);
flag = getArguments().getString("Flag");
Log.v(TAG,flag);
Toast.makeText(getActivity(), "Toast"+flag, Toast.LENGTH_LONG).show();
if (startTimeEditText.isActivated()){
startTimeEditText.setText(String.valueOf(hourOfDay) + ":" + String.valueOf(minute));
}
else if (endTimeEditText.isActivated()){
endTimeEditText.setText(String.valueOf(hourOfDay) + ":" + String.valueOf(minute));
}
}
TimePickerFragment should not mess with the layout of the rest of the Activity: the best way here is to pass a listener to the TimePickerFragment. Take a look at TimePicker.setOnTimeChangedListener(...), I know it's not a Fragment, but the idea is the same.
Another solution is to use an event bus (for example including the EventBus library) and posting a new event when the user select a date: in this way any component of your app can subscribe to this event and act accordingly.

how to create a custom dialog and receive results in android?

i have an activity that when user click on button , a dialog open. in this dialog there is a spinner that have 3 choices: Blue,Red,Green. and there is a submit button. i want that when user select a color and click on submit, in caller activity, its String color set to selected color in dialog. i try this: but not worked. please help me....
String color;
String dialogColor;
showDialog.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.custom_dialog);
dialog.setTitle("my dialog");
Spinner spinner = (Spinner) dialog.findViewById(R.id.spinner);
final TextView status = (TextView) dialog.findViewById(R.id.status);
Button submit = (Button) dialog.findViewById(R.id.submit);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
dialogColor = parent.getItemAtPosition(position).toString();
status.setText("Color is: "+dialogColor);
color = dialogColor;
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
submit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent();
intent.putExtra("Color",dialogColor);
dialog.dismiss();
}
});
dialog.show();
}
});
i use both of direct and with intent ways to assign my color String to selected value. but not worked. where i have mistake?
I think the best way to create custom dialogs now is the Dialog Fragment, because the simple dialog it's limited. For example it's the way to create a dialogs with material design. And you have a differents ways to take info from dialog fragment, the first and the second for example.
This is basic code to create a dialog fragment:
//Method to call and start dialog fragment class
public void ShowPhotoFilesDialog(Activity context,File photo){
//Declaration of classes
Custom_DialogFragment custom_dialogFragment = new Custom_DialogFragment ();
FragmentManager fragmentManager = context.getFragmentManager();
// The device is using a large layout, so show the fragment as a dialog
custom_dialogFragment.show(fragmentManager, "dialog");
}
And this is the basic dialog fragment class:
public class Custom_DialogFragment extends DialogFragment {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
try {
// The only reason you might override this method when using onCreateView() is
// to modify any dialog characteristics. For example, the dialog includes a
// title by default, but your custom layout might not need it. So here you can
// remove the dialog title, but you must call the superclass to get the Dialog.
Dialog dialog = super.onCreateDialog(savedInstanceState);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
//To hide action bar from layout
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
//Declaration of controls
View v = getActivity().getLayoutInflater().inflate(R.layout.my_custom_layout);
builder.setView(v);
//My code
return builder.create();
}
catch (Exception ex){
Log.e("-- Custom_DialogFragment.onCreateDialog --","",ex);
return null;
}
}
}
Tell me if I helped you, good programming!

Android - Add new buttons in listview dynamically

I've a Listview with two linear layouts and there is one button add new row . When i click add new row button i want to create new row of buttons dynamically. After that click on that created button i want to show an time picker dialog . When user click set time button i want to set that time in that button. My problem is All the buttons(with different id) are added fine and when click that button time picker dialog was pop up. But after click set time button the time will not set . How can i add this time to the button. How can i handle this button ?
It is my piece of code inside of listview onscroll listener
final LinearLayout l1 = LinearLayout) List_Layout.getChildAt(0);
LinearLayout l12 = (LinearLayout) List_Layout.getChildAt(1);
final Button button = new Button(getApplicationContext());
final Button button1 = new Button(getApplicationContext());
add_button.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
button.setLayoutParams(lparams);
button1.setLayoutParams(lparams);
button.setId(0);
button1.setId(1);
l1.addView(button, lparams);
l12.addView(button1, lparams);
}
}
button.setOnClickListener(new OnClickListener()
{
#SuppressWarnings("deprecation")
public void onClick(View v)
{
showDialog(TIME_DIALOG_ID);
}
}
button1.setOnClickListener(new OnClickListener()
{
#SuppressWarnings("deprecation")
public void onClick(View v)
{
// TODO Auto-generated method stub
Log.e("tag 1", button1.getTag()+"");
showDialog(TIME_DIALOG_ID);
}
});
Rest of the codes for timepicker dialog is works fine
It's just sample code
Please anyone help me get out from this riddle.
Edit:
static final int TIME_DIALOG_ID = 998;
private int hour;
private int minute;
#Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case TIME_DIALOG_ID:
return new TimePickerDialog(this,
timePickerListener, hour, minute,false);
}
return null;
}
private TimePickerDialog.OnTimeSetListener timePickerListener =
new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int selectedHour,
int selectedMinute) {
hour = selectedHour;
minute = selectedMinute;
}
}
You need to set text of button or button1 to the time that was actually picked by the user. To do so, code some button.setText(<formatted time>) in the timePickerListener.
Here's some line of code, I use to format date and time from a java.util.Date object:
Date date = new Date();
java.text.DateFormat dateFormat = DateFormat.getMediumDateFormat(context);
java.text.DateFormat timeFormat = DateFormat.getTimeFormat(context);
button.setText(String.format("%s %s", dateFormat.format(date), timeFormat.format(date)));
In your case of the time picker you can just format the selected values into a string:
button.setText(String.format("%02d:%02d", selectedHour, selectedMinute));

Custom Dialog not displaying - Android

I'm trying to pop up a custom dialog when I click on a button but it won't pop up at all. my app is basically a calendar and I'm going to use sqlite to add/hold appointments and stuff to a date in the calendar using the dialog, which is where the appointment details will be specified.
The code I'm using for this is the following:
public void onClick(View v) {
// TODO Auto-generated method stub
//long a = calendar.getDate();
switch(v.getId()){
case R.id.createButton:
openCreateAppointmentDialog();
break;
}
}
private void openCreateAppointmentDialog(){
Context mContext = getApplicationContext();
Dialog createAppmntDialog = new Dialog(mContext);
createAppmntDialog.setContentView(R.layout.create);
createAppmntDialog.setTitle(R.string.createTitle);
appointmentTitle = (EditText) createAppmntDialog.findViewById(R.id.titleTextBox);
appointmentTitle.setText("hello");
appointmentTime = (EditText) createAppmntDialog.findViewById(R.id.timeTextBox);
appointmentDetails = (EditText) createAppmntDialog.findViewById(R.id.detailsTextBox);
saveAppointment = (Button) createAppmntDialog.findViewById(R.id.saveButton);
saveAppointment.setOnClickListener(this);
}
What am I doing wrong?
Call the show() method for your dialog.
createAppmntDialog.show(); //when you want the dialog to appear on the screen

Categories

Resources