I have a form in which I have TextInputEditText, I want that it should show a timepicker on clicking it but user should not be able to enter any text directly into that box. I am not replacing this with a TextView or a Button because the form has outlined boxes similar to the screenshot. I want to maintain the uniformity of fields in the form.
This is the code for the EditText --
<com.google.android.material.textfield.TextInputLayout
android:id="@ id/edittext_add_name"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="16dp"
android:fontFamily="sans-serif-light"
android:inputType="textAutoComplete"
android:minHeight="@dimen/min_height"
android:paddingLeft="6dp"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@ id/button_import"
app:startIconDrawable="@drawable/ic_baseline_child_care_24">
<com.google.android.material.textfield.TextInputEditText
android:id="@ id/edittext_add_name_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Name"
android:inputType="textEmailAddress" />
</com.google.android.material.textfield.TextInputLayout>
I have tried enabled=false
but then the field is not clickable also. Any recommendation how can I achieve the desired result?
CodePudding user response:
I hope this code will help you out in the desired output i have done same thing many times.
1st thing do the changes in layout
<com.google.android.material.textfield.TextInputLayout
android:id="@ id/edittext_add_name"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginHorizontal="16dp"
android:layout_marginTop="32dp"
android:fontFamily="sans-serif-light"
android:inputType="textAutoComplete"
android:paddingStart="6dp"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="RtlSymmetry">
<com.google.android.material.textfield.TextInputEditText
android:id="@ id/edt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/name"
android:clickable="false"
android:cursorVisible="false"
android:focusable="false"
android:focusableInTouchMode="false"
/>
</com.google.android.material.textfield.TextInputLayout>
In Java side do this changes to achieve the output
public class TestActivity extends AppCompatActivity {
Calendar myCalendar;
TextInputEditText edt ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
edt = findViewById(R.id.edt);
myCalendar = Calendar.getInstance();
DatePickerDialog.OnDateSetListener dateListener = (datePicker, year, monthOfYear, dayOfMonth) -> {
myCalendar.set(Calendar.YEAR, year);
myCalendar.set(Calendar.MONTH, monthOfYear);
myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
updateEdtTextview();
};
edt.setOnClickListener(view -> new DatePickerDialog(
TestActivity.this,
dateListener,
myCalendar.get(Calendar.YEAR),
myCalendar.get(Calendar.MONTH),
myCalendar.get(Calendar.DAY_OF_MONTH)).show());
}
private void updateEdtTextview() {
String myFormat = "dd/MM/yyyy"; //In which you need put here
SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
edt.setText(sdf.format(myCalendar.getTime()));
}
}