Home » Android » android – Custom Dialog with 2 TextViews – focus problem-Exceptionshub

android – Custom Dialog with 2 TextViews – focus problem-Exceptionshub

Posted by: admin February 26, 2020 Leave a comment

Questions:

In my app I need to fix problem with focus of TextViews. Looks like with custom DatePickerFragment and TimePickerFragment user must click 2 times in order to open custom dialog.

How to fix it ? I need to open custom DatePickerFragment and TimePickerFragment after 1st click.

code : https://github.com/fanysoft/AndroidCustomDateTimePIcker

screen video : download at https://github.com/fanysoft/AndroidCustomDateTimePIcker/blob/master/focusProblem.webm

XML :
I have 2 EditTexts

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="32dp"
        android:ems="10"
        android:inputType="textPersonName"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:ems="10"
        android:inputType="textPersonName"
        app:layout_constraintEnd_toEndOf="@+id/editText1"
        app:layout_constraintStart_toStartOf="@+id/editText1"
        app:layout_constraintTop_toBottomOf="@+id/editText1" />

</androidx.constraintlayout.widget.ConstraintLayout>

Java :

EditText mEditText1, mEditText2;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Log.d(TAG, "app started");

    mEditText1 = findViewById(R.id.editText1);
    mEditText2 = findViewById(R.id.editText2);

    // hide keyboard
    mEditText1.setInputType(InputType.TYPE_NULL);
    mEditText2.setInputType(InputType.TYPE_NULL);

    // onCLick
    mEditText1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d(TAG, "editText 1 click..");

            DialogFragment newFragment = new DatePickerFragment();
            newFragment.show(getSupportFragmentManager(), "datePicker");
        }
    });

    mEditText2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d(TAG, "editText 2 click..");

            DialogFragment newFragment = new TimePickerFragment();
            newFragment.show(getSupportFragmentManager(), "timePicker");
        }
    });
}

@Override
public void onDateReceive(int dd, int mm, int yy) {
    String recievedDate = yy + "." + mm + "." + dd;
    Log.d(TAG, "onDateReceive .. " + recievedDate);
    mEditText1.setText(recievedDate);
}

@Override
public void onTimeReceive(int hourOfDay, int minute) {
    String recievedTime = hourOfDay + ":" + minute;
    Log.d(TAG, "onTimeReceive .." + recievedTime);
    mEditText2.setText(recievedTime);
}
How to&Answers: