Timepicker Dialog from Clicking Edittext

TimePicker Dialog when pressing EditText

Add these properties to your EditText tag in xml to disable keyboard popping up

android:editable="false" 

and

android:focusable="false"

UPDATE:

android:editable="false" is deprecated now.
use android:inputType="none" instead or in the code:

editText.setEnabled(false);

TimePicker Dialog from clicking EditText

eReminderTime.setText( "" + selectedHour + ":" + selectedMinute);

Your missing a + between "" and selected hour, setText methods only take a single string, so you need to concatenate all the parts (the first quotes are likely unnecessary).

eReminderTime.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Calendar mcurrentTime = Calendar.getInstance();
int hour = mcurrentTime.get(Calendar.HOUR_OF_DAY);
int minute = mcurrentTime.get(Calendar.MINUTE);
TimePickerDialog mTimePicker;
mTimePicker = new TimePickerDialog(AddReminder.this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
eReminderTime.setText( selectedHour + ":" + selectedMinute);
}
}, hour, minute, true);//Yes 24 hour time
mTimePicker.setTitle("Select Time");
mTimePicker.show();

}
});

That should fix your second error, you weren't providing the last parameter.
TimePickerDialog Constructors

TimePicker on EditText click

I got the answer for the same:-

Include the following methods in your Fragment.

public void selectTimeToDisplay(EditText edText) {
DialogFragment dialogFragment = new DisplayTimeFragment(edText);
dialogFragment.show(getActivity().getSupportFragmentManager(), "DatePicker");
}

public void setSelectedTime(String hourOfDay,String minute, EditText edText) {
edText.setText(hourOfDay + ":" + minute);
}

Define DisplayTimeFragment as a Inner class in your Fragment:-

public class DisplayTimeFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener {
EditText edText;
public DisplayTimeFragment(EditText edText){
this.edText = edText;
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current time as the default values for the picker
final Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
return new TimePickerDialog(getActivity(), this, hour, minute,
DateFormat.is24HourFormat(getActivity()));
}
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
String sHrs, sMins;
if(minute<10){
sMins = "0"+minute;
}else{
sMins = String.valueOf(minute);
}
if(hourOfDay<10){
sHrs = "0"+hourOfDay;
}else{
sHrs = String.valueOf(hourOfDay);
}
setSelectedTime(sHrs,sMins, edText);

}
}

Now, Write the following code in the onClickListener of your Edittext.

as,

edStartTime.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
selectTimeToDisplay(edStartTime);
}
});

Note:- For Smooth functioning, Set focusable to false in the xml of the Edittext.

Bingo.. It will work as charms for any no of EditText now...

Show DatePickerDialog and TimePickerDialog after one time EditText click

import android.app.Application
import androidx.lifecycle.AndroidViewModel

class TestViewModel(application: Application) : AndroidViewModel(application) {

private val context = getApplication<Application>().applicationContext
........
}

Then pass context

 fun showDateTimeDialog() {
val currentDateTime = Calendar.getInstance()
val startYear = currentDateTime.get(Calendar.YEAR)
val startMonth = currentDateTime.get(Calendar.MONTH)
val startDay = currentDateTime.get(Calendar.DAY_OF_MONTH)
val startHour = currentDateTime.get(Calendar.HOUR_OF_DAY)
val startMinute = currentDateTime.get(Calendar.MINUTE)
DatePickerDialog(context, { _, year, month, day ->
TimePickerDialog(context, { _, hour, minute ->
val pickedDateTime = Calendar.getInstance()
pickedDateTime.set(year, month, day, hour, minute)
}, startHour, startMinute, false).show()
}, startYear, startMonth, startDay).show()
}
}

EditText TimePicker shows KeyBoard

Add this into EditText

android:cursorVisible="false"
android:focusable="false"
android:inputType="none"

Like that:


<com.google.android.material.textfield.TextInputEditText
android:id="@+id/fIni"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:cursorVisible="false"
android:focusable="false"
android:inputType="none"
android:maxLines="1"
android:textColor="@color/colorAccent" />

How to get TimePickerDialog on edittext appears at first click

add android:focusableInTouchMode="false" in layout declaration

<EditText
android:id="@+id/txtTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusableInTouchMode="false"
android:layout_weight="1">

</EditText>

How to popup a TimePicker on click of text box in Andriod 2.3.3?

Do the following and you will get a nice time picker for your text box:

final EditText my_Time = (EditText) findViewById(R.id.my_time);

my_Time.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Calendar mcurrentTime = Calendar.getInstance();
int hour = mcurrentTime.get(Calendar.HOUR_OF_DAY);
int minute = mcurrentTime.get(Calendar.MINUTE);

TimePickerDialog mTimePicker;
mTimePicker = new TimePickerDialog(TimeActivity.this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
my_Time.setText( selectedHour + ":" + selectedMinute);
}
}, hour, minute, true);
mTimePicker.setTitle("Select Time");
mTimePicker.show();

}
});

In your xml create an edit text

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/my_time"
android:text="20:00"
android:editable="false"
android:focusable="false"
/>

Datepicker: How to popup datepicker when click on edittext

Try this in the XML file:

<EditText
android:id="@+id/Birthday"
custom:font="@string/font_avenir_book"
android:clickable="false"
android:cursorVisible="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:hint="@string/birthday"/>

And this in the Java File:

public class MainActivity extends AppCompatActivity {
final Calendar myCalendar= Calendar.getInstance();
EditText editText;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText=(EditText) findViewById(R.id.BirthDate);
DatePickerDialog.OnDateSetListener date =new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int day) {
myCalendar.set(Calendar.YEAR, year);
myCalendar.set(Calendar.MONTH,month);
myCalendar.set(Calendar.DAY_OF_MONTH,day);
updateLabel();
}
};
editText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new DatePickerDialog(MainActivity.this,date,myCalendar.get(Calendar.YEAR),myCalendar.get(Calendar.MONTH),myCalendar.get(Calendar.DAY_OF_MONTH)).show();
}
});
}

private void updateLabel(){
String myFormat="MM/dd/yy";
SimpleDateFormat dateFormat=new SimpleDateFormat(myFormat, Locale.US);
editText.setText(dateFormat.format(myCalendar.getTime()));
}
}

Add android:focusable="false" within the xml file of the EditText to allow for a single touch.

how i can open time picker dialog box in dynamic layout

The problem is that you setting View.ClickListener only to a single view EditText time that is a direct child of R.layout.activity_services_and_rate_list.

Later you add more child views to inflated R.layout.activity_services_and_rate_list layout but not setting click listeners.

One of the options would be to create a method that will set click listener for each passed in time EditText.

Note: Consider using RecyclerView with Adapter. It has much better performance when the number of views grows in your layout.

public class ServicesAndRateList extends AppCompatActivity {

//your variables here ...

@Override
protected void onCreate(Bundle savedInstanceState) {
//your other onCreate code here ...

setTimeEditTextClickListener(time);
}

private void setTimeEditTextClickListener(EditText time) {
time.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
timePickerDialog = new TimePickerDialog(ServicesAndRateList.this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker timePicker, int hourOfDay, int minutes) {
time.setText(hourOfDay + ":" + minutes);
}
}, 0, 0, true);

timePickerDialog.show();
}
});
}

public void onAddField(View view) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View rowView = inflater.inflate(R.layout.childview, null);
// Add the new row before the add field button.
parentLinearLayout.addView(rowView, parentLinearLayout.getChildCount() - 1);
setTimeEditTextClickListener((EditText) rowView.findViewById(R.id.time));
}
}


Related Topics



Leave a reply



Submit