TimePicker not updating from user input - android

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.

Related

Android DatePicker dialog not showing on Android 6

I have a DatePicker in my app that has always worked on both phones and tablets until Android 6. I have a Samsung Galaxy 7 Edge t test on and the dialog is not showing.
I have set the min and target sdk version to 11 and 21 respectively in the manifest.
I understand that i should be using DialogFragment but is there a reason why the code below is not showing on Android 6? The code works fine on 2.3.6, 4.x and 5.x.
The following log is being executed so i know that the .show() method has been called.
Log.e(TAG, "Just executed dialog.show() and at the end of showDateTimeDialog method");
[EDIT]
Things i have tried are :
Adding a style to the style.xml and the calling a different Dialog constuctor that references that style. This does show a dialog on Android 6, but unfortunately it is not my custom one that uses the my layout with buttons on.
<style name="MyDialogStyle" parent="Theme.AppCompat.Light">
.
dialog = new Dialog(MenuActivity2.this, R.style.MyDialogStyle);
dialog.setContentView(R.layout.datetimepickerunalloc);
Thanks
Button Set;
Button ReSet;
Button Cancel;
DatePicker DPic;
TextView Date;
private ViewSwitcher switcher;
static final int DATE_TIME_DIALOG_ID = 999;
Dialog dialog;
TextView dialogMessage;
DateTime timeSetOnSpinner;
public void showDateTimeDialog() {
// final Calendar c = Calendar.getInstance();
final SimpleDateFormat dfDate = new SimpleDateFormat("dd-MMM-yyyy HH:mm");
dialog = new Dialog(MenuActivity2.this);
dialog.setContentView(R.layout.datetimepickerunalloc);
DPic = (DatePicker) dialog.findViewById(R.id.DatePicker);
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());
timeSetOnSpinner = new DateTime(c);
DateTimeFormatter fmt = DateTimeFormat.forPattern("d-MMM-Y");
String formattedDate = fmt.print(timeSetOnSpinner);
formattedDate = formattedDate.trim();
//Toast.makeText(MenuActivity2.this, "date = " + formattedDate, Toast.LENGTH_LONG).show();
final Intent intent = new Intent(MenuActivity2.this, ShowUnallocatedCallsActivity.class);
intent.putExtra("unallocdate", formattedDate);
startActivity(intent);
}// end of onClick
});
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));
}
});
dialog.setTitle("Unallocated calls date");
try {
ReSet.performClick();
dialog.show();
Log.e(TAG, "Just executed dialog.show() and at the end of showDateTimeDialog method");
} catch (Exception e) {
// ignore
}
}// showDateTimeDialog()
.
This is my layout for the custom dialog
<?xml version="1.0" encoding="UTF-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="700dp"
android:layout_height="wrap_content"
android:fillViewport="true">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/textviewdatetimepickermessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textAppearance="?android:attr/textAppearanceLarge"
/>
<LinearLayout
android:id="#+id/DateLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true" >
<DatePicker
android:id="#+id/DatePicker"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dip"
android:layout_marginRight="5dip" />
</LinearLayout>
<LinearLayout
android:id="#+id/ControlButtons"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="10dip" >
<Button
android:id="#+id/SetDateTime"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#android:string/ok" />
<Button
android:id="#+id/ResetDateTime"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Reset" />
<!--
<Button
android:layout_height="wrap_content"
android:layout_width="0dip"
android:id="#+id/CancelDialog"
android:text="#android:string/cancel"
android:layout_weight="1"/>
-->
</LinearLayout>
</LinearLayout>
</ScrollView>
.
I eventually got the dialog showing by using a different constructor that passed in a theme arg.
dialog = new Dialog(MenuActivity2.this, R.style.AppBaseTheme );

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

Android doesn't detect click on EditText

I am working on an Android tablet application with a little bit complex forms (layout has a ListView and the layout with the form included so I have to inflate EditText) . My idea is that if I click on a TextView where the date is shown the DataPicker would be shown in dialog.
The problem is that click is not detected. There is no Logs.
Java:
public static EditText datep;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.admissions_all);
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
View inf = LayoutInflater.from(getApplicationContext()).inflate(R.layout.admissions, null);
datep = (EditText) inf.findViewById(R.id.date_value);
datep.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.i("Date", "You clicked me mate!");
showDatePickerDialog(v);
}
});
}
public void showDatePickerDialog(View v) {
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getFragmentManager(), "datePicker");
Log.i("Date", "I want to show datePicker");
}
public static class DatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day= c.get(Calendar.DAY_OF_MONTH);
// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);
}
public void onDateSet(DatePicker view, int year, int month, int day) {
datep.setText(day + "." + month+ "." + year);
}
}
XML:
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#id/general"
android:layout_alignRight="#id/general"
android:layout_below="#+id/general"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp" >
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Data"
android:textAppearance="?android:attr/textAppearanceLarge" />
<View
android:id="#+id/line"
android:layout_width="wrap_content"
android:layout_height="1dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/title"
android:background="#333333" />
<EditText
android:id="#+id/date_value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/line"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_toRightOf="#+id/date_desc"
android:ems="9"
android:inputType="date" >
</EditText>
<TextView
android:id="#+id/date_desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/date_value"
android:layout_alignBottom="#+id/date_value"
android:layout_alignParentLeft="true"
android:text="Date:"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
I followed this: http://developer.android.com/guide/topics/ui/controls/pickers.html
You need to set date_value's focusable in touch mode to false. You can do so either by adding android:focusableInTouchMode="false" to your xml or in your code with datep.setFocusableInTouchMode(false);. This should then have your on click event fire. (tested on emulator)
See also: How to do something after user clicks on my EditText
Reference: http://android-developers.blogspot.com/2008/12/touch-mode.html
I didn't see any dateup.setclickable(true);, try to add this. Maybe that's why it's not work , You can also debug it and see , if it call the onlciklistner or not
I saw that you wrote something like this.
datep = (EditText) inf.findViewById(R.id.date_value);
when you have this
setContentView(R.layout.admissions_all);
It means that you editText is in another Activity , but you are setting his onclickListner in another one , so it will not work. Initialize his onlcikListner in the activity, that content view is inf.xml. If all these will not help please add more part of your Code
Regards Hayk Nahapetyan
Could you change the DatePickerFragment to DatePickerDialog. Then do a DatePickerDialog.show();
Something like :
DatePickerDialog datePickerDialog = null;
datep.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.i("Date", "You clicked me mate!");
showDatePickerDialog();
}
});
private void showDatePickerDialog() {
datePickerDialog = new DatePickerDialog(context, new DateSetListener(), year, month, day);
datePickerDialog.show();
}

Programmatically adding EditText box

I have the following text with me.
"I drink tea and coffee". Now the requirement is to change this text to "I drink EditText AND EditText"....Here the EditText is a edit box where in the user can enter answers once it is clicked. I need to make this change pro grammatically.
Any suggestions on this, as to how this can be achieved????
You could use the following code in button's click event handler:
String str = "I drink tea and coffee";
String editTextString = editText.getText().ToString();
str.replace("coffee", editTextString);
str.replace("tea", editTextString);
You can ctreate activity structure in your layout xml file:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="I drink " />
<Button
android:id="#+id/firstAnswer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="EditText" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" and " />
<Button
android:id="#+id/secondAnswer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="EditText" />
</LinearLayout>
And set listners to your buttons
private String[] answers = { "tea", "coffee", "juice", "compote" };
...
Button firstAnswer = (Button) findViewById(R.id.firstAnswer);
firstAnswer.setOnClickListener(new OnClickListener() {
private int position = 0;
#Override
public void onClick(View view) {
((Button) view).setText(answers[position]);
position++;
if (position == answers.length)
position = 0;
}
});
By using getText()
Example
Button mButton;
EditText mEdit;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mButton = (Button)findViewById(R.id.button);
mEdit = (EditText)findViewById(R.id.edittext);
mButton.setOnClickListener(
new View.OnClickListener()
{
public void onClick(View view)
{
Log.v("EditText", mEdit.getText().toString());
}
});
}
after that
mEdit.setText("Tea");
EditText et=(EditText)findViewByID(R.id.yourId);
Button bt=(Button)findViewById(R.id.yourBtId);
bt.setOnClickListener(
new View.setOnClickListener()
{
public void onClick(View v)
{
et.setText("You Drink EditText");
}
});
Put These code in onCreate() method
Use ViewSwitcher. This widget displays one of two views it contains:
<ViewSwitcher
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</ViewSwitcher>
And then, when button is pressed, switch it and load text from EditText to TextView or vice versa:
editText.setText(textView.getText());
viewswitcher.showNext();
or
textView.setText(editText.getText());
viewswitcher.showPrevious();
Refer to this for details.
You can use 4 textboxes with texts "I drink" ,"tea" , " and ", "coffee" respectively.
On the ontouch event of the 2nd and 4th textbox, you can display a textbox or edittext and edit the text. On a button click you can get the texts from the textboxes and display it again.
Just Use this : yourTextField.setText("..."+yourEditText.getText().toString());

How to use xml timepicker?

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);

Categories

Resources