How to use xml timepicker? - android

I have dialog button and timepicker created in the same xml code below:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TimePicker
android:id="#+id/timePicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout android:id="#+id/LinearLayout02" android:layout_height="wrap_content" android:layout_width="fill_parent">
<Button android:layout_height="wrap_content" android:id="#+id/dialog_ok" android:text="OK" android:layout_width="fill_parent" android:layout_weight="1"></Button>
<Button android:layout_height="wrap_content" android:text="Cancel" android:id="#+id/dialog_cancel" android:layout_width="fill_parent" android:layout_weight="1"></Button>
</LinearLayout>
</LinearLayout>
And then i created dialog in java code and also bring in the dialog and timepicker from xml code as showen below:
public void TimePickerDialog(){
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.sign_in_dialog);
/////////////////////updated code////////////////////
TimePicker timePicker1 = (TimePicker) findViewById(R.id.timePicker1);
timePicker1.setIs24HourView(true);
/////////////////////////////////
dialog.setTitle("Quick Mute");
//Allow the dialog to be cancelable
dialog.setCancelable(true);
// Here we add functionality to our dialog box's content. In this example it's the two buttons
//reference the button
Button okButton = (Button) dialog.findViewById(R.id.dialog_ok);
//Create a click listener
okButton.setOnClickListener(new OnClickListener() {
//override the onClick function to do something
public void onClick(View v) {
}
});
//reference the button
Button cancelButton = (Button) dialog.findViewById(R.id.dialog_cancel);
//Create a click listener
cancelButton.setOnClickListener(new OnClickListener() {
//override the onClick function to do something
public void onClick(View v) {
//Close the dialog</span>
dialog.dismiss();
}
});
//finally show the dialog
dialog.show();
}
The problem i have is how do i get access to the timepicker that was created in xml code so that i can make the timepicker 24 hour timepicker? ... and also how do i get the time from the timepicker that is in xml code?

Just like its usually done:
TimePicker timePicker = (TimePicker)dialog.findViewById(R.id.timePicker1);
and as usual this only works after dialog.setContentView is called. Then, to get the time picked, call:
timePicker.getCurrentHour();
timePicker.getCurrentMinute();

You can't set the 24 hours mode in the XML, use MyTimePicker.setIs24HourView(boolean) instead.
See this example on how to get the time.

These are the steps to get the instance of a TimePicker:
final Dialog mTimeDialog = new Dialog(this);
final RelativeLayout mTimeDialogView = (RelativeLayout)getLayoutInflater().inflate(R.layout.time_dialog, null);
final TimePicker mTimePicker = (TimePicker) mTimeDialogView.findViewById(R.id.TimePicker);
mTimePicker.setIs24HourView(true);

Related

how to get the value of edittext

i have a feedback activity and it has an EditText and a button :
<?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="230dp"
android:background="#color/colorPrimary">
<TextView
android:layout_width="wrap_content"
android:gravity="center"
android:layout_height="wrap_content"
android:text="#string/bugtitle"
android:textSize="35sp"
android:layout_centerInParent="true"
android:layout_alignParentTop="true"
android:layout_marginTop="20dp"
android:textColor="#000000"
android:id="#+id/BUG_title"
android:fontFamily="sans-serif-thin"/>
<EditText
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_below="#+id/BUG_title"
android:layout_marginTop="15dp"
android:layout_marginRight="30dp"
android:id="#+id/et_bug"
android:layout_marginLeft="30dp"
android:fontFamily="sans-serif-light"
android:hint="#string/bug_et"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:text="SEND"
android:fontFamily="sans-serif-light"
android:id="#+id/send_bug"/>
</RelativeLayout>
so user write his feedback and when he clicked on SEND button it should send to Parse host with this code:
et1 = (EditText)findViewById(R.id.et_bug);
ParseObject bugsend = new ParseObject("BUGS");
bugsend.put("BUG_REPORT", et1.getText().toString());
bugsend.put("OS", OS_VERSION);
bugsend.put("MODEL", PHONE_MODEL);
bugsend.saveInBackground();
and problem is here when users sends his feedback a null text will be send to Parse host
also i have tried this one:
et1 = (EditText)findViewById(R.id.et_bug);
String BUG_SEND = et1.getText().toString();
ParseObject bugsend = new ParseObject("BUGS");
bugsend.put("BUG_REPORT", BUG_SEND);
bugsend.put("OS", OS_VERSION);
bugsend.put("MODEL", PHONE_MODEL);
bugsend.saveInBackground();
but same as the first one
and then i have tried SharedPreferences and saving the value to SP and send SP value to Parse but its null :|
this is the feedback dialog:
public void onBug(View view) {
final Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCancelable(true);
dialog.setContentView(R.layout.bug_dialog);
Button dialogButton = (Button) dialog.findViewById(R.id.send_bug);
dialogButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final String PHONE_MODEL = android.os.Build.MODEL;
final String OS_VERSION = Build.VERSION.RELEASE;
et1 = (EditText)findViewById(R.id.et_bug);
String BUG_SEND = et1.getText().toString();
ParseObject bugsend = new ParseObject("BUGS");
bugsend.put("BUG_REPORT", BUG_SEND);
bugsend.put("OS", OS_VERSION);
bugsend.put("MODEL", PHONE_MODEL);
bugsend.saveInBackground();
Toast.makeText(getApplication(),"Thank You We Will Check This Report",Toast.LENGTH_LONG).show();
dialog.dismiss();
}
});
dialog.show();
}
anyone knows how i should fix this?
EDIT: I've already changed a bit your code:
public void onBug(View view) {
final Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCancelable(true);
dialog.setContentView(R.layout.bug_dialog);
Button dialogButton = (Button) dialog.findViewById(R.id.send_bug);
dialogButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ParseObject bugsend = new ParseObject("BUGS");
bugsend.put("BUG_REPORT", et1.getText().toString()).commit();
bugsend.put("OS", Build.VERSION.RELEASE;).commit();
bugsend.put("MODEL", android.os.Build.MODEL).commit();
bugsend.saveInBackground(); //THIS GUY
Toast.makeText(getApplication(),"Thank You We Will Check This Report",Toast.LENGTH_LONG).show();
dialog.dismiss();
}
});
dialog.show();
}
If you're using SharedPreferences you need after putting variable, also commit it like in code above. I don't know what is doing your saveInBackground() method, but i think that adding .commit() after every argument should also work.
Please check it and let me know if it won't work

Writing onClick() event for an android custom dialog button

I have a button with id "ok" in my dialog.
Partial - Dialog Layout:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_margin="8dp"
android:layout_below="#+id/licenseTypeLayout">
<Button
android:id="#+id/ok"
style="#style/agreement_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Ok"/>
</LinearLayout>
In activity:
Dialog dialog;
#OptionsItem(R.id.action_about)
boolean displayPopup() {
dialog = new Dialog(this);
dialog.setContentView(R.layout.about_dialog);
dialog.setTitle(R.string.app_name);
dialog.show();
Button btn=(Button)findViewById(R.id.ok);
//btn remains empty
return true;
}
How would I write onClick() event for this button?
To initialize the Button, you need to use the Dialog object
Button btn = (Button) dialog.findViewById(R.id.ok);
Then set OnClickListener to the Button using setOnClickListener() as follows...
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
//write your code here
}
});
I would guess you make the method:
public void closeDialog(View v){
// Do what you want to do here (event handler)
}
?

TimePicker not updating from user input

I've an app that uses a custom dateTime Picker. when the user selects a time and clicks the SET button, it gets the time set on the spinner. My app checks the time and if it's not suitable based on some criteria i show the spinner again.
The problem is when the user changes the time on the spinner a second or subsequent time, the initial time is still set. How can i get the time from the spinner reflected in the dateTime object?
I've tried calling TPic.getCurrentHour etc and also refreshDrawableState on the TPic object but it still doesn't work. Any ideas?
Button ShowDTPicker;
Button ShowDatePicker;
Button ShowTimePicker;
Button Set;
Button ReSet;
Button Cancel;
DatePicker DPic;
TimePicker TPic;
TextView Date;
private ViewSwitcher switcher;
static final int DATE_TIME_DIALOG_ID = 999;
Dialog dialog;
public void showDateTimeDialog(){
//final Calendar c = Calendar.getInstance();
final SimpleDateFormat dfDate = new SimpleDateFormat("dd-MMM-yyyy HH:mm");
dialog = new Dialog(NfcscannerActivity.this);
dialog.setContentView(R.layout.datetimepicker);
switcher = (ViewSwitcher) dialog.findViewById(R.id.DateTimePickerVS);
TPic = (TimePicker) dialog.findViewById(R.id.TimePicker);
DPic = (DatePicker) dialog.findViewById(R.id.DatePicker);
TPic.setIs24HourView(true);
ShowDatePicker = ((Button) dialog.findViewById(R.id.SwitchToDate));
ShowDatePicker.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
switcher.showPrevious();
ShowDatePicker.setEnabled(false);
ShowTimePicker.setEnabled(true);
}
});
ShowTimePicker = ((Button) dialog.findViewById(R.id.SwitchToTime));
ShowTimePicker.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
switcher.showNext();
ShowDatePicker.setEnabled(true);
ShowTimePicker.setEnabled(false);
}
});
Set = ((Button) dialog.findViewById(R.id.SetDateTime));
Set.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Calendar c = Calendar.getInstance();
c.set(DPic.getYear(), DPic.getMonth(), DPic.getDayOfMonth(), TPic.getCurrentHour(), TPic.getCurrentMinute());
Log.e(TAG, "TPic hour and minute = " + TPic.getCurrentHour() + " " + TPic.getCurrentMinute());
timeSetOnSpinner = new DateTime(c);
................
................
...............
//check if time is suitable, if not call showDateTimeDialog() again
}
});
ReSet = ((Button) dialog.findViewById(R.id.ResetDateTime));
ReSet.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Calendar c = Calendar.getInstance();
DPic.updateDate(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH));
TPic.setCurrentHour(c.get(Calendar.HOUR_OF_DAY));
TPic.setCurrentMinute(c.get(Calendar.MINUTE));
}
});
Cancel = ((Button) dialog.findViewById(R.id.CancelDialog));
Cancel.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dialog.cancel();
}
});
dialog.setTitle("Please enter time of last logout");
try{
ReSet.performClick();
dialog.show();
}catch(Exception e){
//ignore
}
Log.e(TAG,"Just executed dialog.show() and at the end of showDateTimeDialog method");
}//showDateTimeDialog()
#Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_TIME_DIALOG_ID:
return dialog;
}
return null;
}
.
<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout
android:padding="5dip"
android:fitsSystemWindows="true"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:id="#+id/DateTimePicker"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:id="#+id/ViewSwitchButtons"
android:layout_marginBottom="5dip">
<Button
android:layout_height="wrap_content"
android:layout_width="0dip"
android:id="#+id/SwitchToDate"
android:text="Set date"
android:enabled="false"
android:layout_weight="1"/>
<Button
android:layout_height="wrap_content"
android:layout_width="0dip"
android:id="#+id/SwitchToTime"
android:text="Set time"
android:layout_weight="1"/>
</LinearLayout>
<ViewSwitcher
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:id="#+id/DateTimePickerVS"
android:layout_below="#+id/ViewSwitchButtons"
android:outAnimation="#android:anim/fade_out"
android:inAnimation="#android:anim/fade_in">
<LinearLayout
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:id="#+id/TimeLayout"
android:fillViewport="true">
<TimePicker
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:id="#+id/TimePicker"
android:layout_marginRight="5dip"
android:layout_marginLeft="5dip"/>
</LinearLayout>
<LinearLayout android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:id="#+id/DateLayout"
android:fillViewport="true">
<DatePicker
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:id="#+id/DatePicker"
android:layout_marginRight="5dip"
android:layout_marginLeft="5dip"/>
</LinearLayout>
</ViewSwitcher>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:id="#+id/ControlButtons"
android:layout_below="#+id/DateTimePicker"
android:paddingTop="185dip">
<Button
android:layout_height="wrap_content"
android:layout_width="0dip"
android:id="#+id/SetDateTime"
android:text="#android:string/ok"
android:layout_weight="1"/>
<Button
android:layout_height="wrap_content"
android:layout_width="0dip"
android:id="#+id/ResetDateTime"
android:text="Reset"
android:layout_weight="1"/>
<Button
android:layout_height="wrap_content"
android:layout_width="0dip"
android:id="#+id/CancelDialog"
android:text="#android:string/cancel"
android:layout_weight="1"/>
</LinearLayout>
</RelativeLayout>
Android: How to get the time from a TimePicker when it is typed in
It seems like that DatePicker or TimePicker updates own values when its focus is gone. Use clearFocus() method in OnClickListener of set button.
I think the code would be like this:
Set = ((Button) dialog.findViewById(R.id.SetDateTime));
Set.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
DPic.clearFocus();
TPic.clearFocus();
Calendar c = Calendar.getInstance();
c.set(DPic.getYear(), DPic.getMonth(), DPic.getDayOfMonth(),
TPic.getCurrentHour(), TPic.getCurrentMinute());
Log.e(TAG, "TPic hour and minute = " + TPic.getCurrentHour()
+ " " + TPic.getCurrentMinute());
timeSetOnSpinner = new DateTime(c);
}
});
Please note that ShowDTPicker, ShowDatePicker, ShowTimePicker, Set, ReSet, Cancel, DPic, TPic, Date are not following Java naming conventions. Names with a capitalized first letter are for classes or interfaces (and Date class is also exists!). If they are public class variables, each of their first letter should be lowercase. Also use mSwitcher instead of switcher since it is private class variable.
for kotlin you can test this code.
var timePicker : TimePicker = findViewById(R.id.timePicker)
timePicker.setOnTimeChangedListener(object:TimePicker.OnTimeChangedListener {
override fun onTimeChanged(p0: TimePicker?, hour: Int,minute: Int) {
// get hour and minute
hour_set =hour
minute_set =minute
} })
I'm having the same issue (with asp.net though)... i know that it's due to the fact that the new selected time is not been considered during the postback so the updates doesn't even occure ... i also know that i had the same issue with the date picker and solve it saving the values in hiddenfields taht i'm retrieving in the code behind .
so, basically it's the same here, we need javascript function to store th values in hiddenfield to be retrieved later
this function could be triggered with OnClientClick action on your submit button.
But i'm having issues finding how to get the values form the timePicker in javascript i did not found no accurate documentation.
i hope this helped,keep us tuned please if you find any solution.

Trying to Read Edit box inside Custom Dialog... Getting FC

I am trying to read a EditText box and send its contents to another method from within a Custom dialog box. My current code causes a Force Close. The logcat is very vague... however I know the uncaught exception takes place in this method:
public void getName(){
Dialog dialog = new Dialog(main.this);
dialog.setContentView(R.layout.customdialog);
dialog.setTitle("New Game");
dialog.setCancelable(true);
//there are a lot of settings, for dialog, check them all out!
final EditText inputBox = new EditText(this);
//set up text
final TextView text = (TextView) dialog.findViewById(R.id.TextView01);
text.setText("Enter Your Name...");
//set up button
final Button button = (Button) dialog.findViewById(R.id.namebutton);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
String str = inputBox.getText().toString();
setName(str);
}
});
//now that the dialog is set up, it's time to show it
dialog.show();
}
Here is the custom XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="wrap_content">
<LinearLayout android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="#+id/linearLayout1">
<TextView android:text="#+id/TextView01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="#+id/nameprompt"></TextView>
<EditText android:layout_width="fill_parent" android:id="#+id/editText1" android:layout_height="wrap_content" android:inputType="textPersonName" android:text="Player">
<requestFocus></requestFocus>
</EditText>
<Button android:layout_height="wrap_content" android:id="#+id/namebutton" android:text="Ok" android:layout_width="fill_parent"></Button>
</LinearLayout>
</RelativeLayout>
Any ideas???
You have the EditText in your XML for the Dialog's layout... and you're properly using findViewById() to instantiate your TextView...
You need to do the same for the EditText, also use findViewById() to instantiate it.
final EditText inputBox = (EditText) dialog.findViewById(R.id.editText1);
Something is up with the inputBox object. You create it in this method, but I don't see you actually adding to the layout anywhere. When this method completes, you'll be displaying the dialog box, but the input box won't be displayed anywhere. In fact, I think that inputBox might be garbage collected since there are not references to if around after the getName() method completes. Therefore when you call get input on it, it might be null.
I think what you meant to do was this:
final EditText inputBox = (EditText)dialog.findViewById(R.id.editText1)

How to configure custom button in dialog?

here I have a custom dialog with background 2 ImageButton inside it.
the problem is, when I try to set onclick listener to that buttons, the program will return NullPointerException. I don't know why is this happen. how to assign operation to button inside dialog anyway??
pause menu xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="wrap_content" android:id="#+id/linearLayout1" android:layout_width="wrap_content" android:background="#drawable/pause_menu_cropped" android:layout_gravity="center" android:gravity="center|center_horizontal">
<TableLayout android:layout_width="wrap_content" android:id="#+id/tableLayout1" android:layout_height="wrap_content">
<ImageButton android:src="#drawable/pause_button_option" android:layout_width="wrap_content" android:background="#drawable/pause_button_option" android:layout_height="wrap_content" android:id="#+id/btn_pause_option"></ImageButton>
<ImageButton android:src="#drawable/pause_button_quit" android:layout_width="wrap_content" android:background="#drawable/pause_button_quit" android:layout_height="wrap_content" android:id="#+id/btn_pause_quit"></ImageButton>
</TableLayout>
</LinearLayout>
dialog code
Dialog pauseMenu = new Dialog(this, R.style.NewDialog);
pauseMenu.setContentView(R.layout.pause_menu);
ImageButton quit = (ImageButton)findViewById(R.id.btn_pause_quit);
quit.setOnClickListener(
new OnClickListener() {
#Override
public void onClick(View v) {
TestActivity.this.finish();
}
});
return pauseMenu;
the code is returning error in line
quit.setOnClickListener();
ImageButton quit =
(ImageButton)findViewById(R.id.btn_pause_quit);
should be
ImageButton quit = (ImageButton)pauseMenu.findViewById(R.id.btn_pause_quit);
This happens because findViewById is invoked for the activity, and it doesn't have btn_pause_quit button in it's layout. But your dialog has.
U can use this custom dialog and onclicklistener..
public class CustomizeDialog extends Dialog implements OnClickListener {
Button okButton;
public CustomizeDialog(Context context) {
super(context);
/** 'Window.FEATURE_NO_TITLE' - Used to hide the title */
requestWindowFeature(Window.FEATURE_NO_TITLE);
/** Design the dialog in main.xml file */
setContentView(R.layout.main);
okButton = (Button) findViewById(R.id.OkButton);
okButton.setOnClickListener(this);
}
#Override
public void onClick(View v) {
/** When OK Button is clicked, dismiss the dialog */
if (v == okButton)
dismiss();
}
}
I think your onClickListener should be DialogInterface.OnClickListener

Categories

Resources