1

When I open activity, directly time picker is opening but I don't want this. So I have added this line in my layout android:focusable="false". Now time picker is not opening while opening activity.

My requirement is, In the activity when the user submits the details, I have done some validations and if the user did not select time from the time picker, I have to open the time picker immediately. But It was not opened because I made that edittext focusable property as false.

How can I achieve my requirement?

Any help would be appreciated. Help me.

Mostafa Arian Nejad
  • 1,278
  • 1
  • 19
  • 32
vishnu
  • 337
  • 4
  • 14

2 Answers2

0

You maybe are lookig for TextWatcher

Steps to follow

Your detail stuff

detail.addTextChangedListener(detailWatcher);

Then create a class named detailWatcher

private final TextWatcher detailWatcher= new TextWatcher() {
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        public void onTextChanged(CharSequence s, int start, int before, int count) {
            //Comprobations
        }

        public void afterTextChanged(Editable s) {
            //Comprobations
        }
    };

To understand better with a .gif see this answer

Then when your detail stuff is ok you can show the timePicker

Skizo-ozᴉʞS ツ
  • 19,464
  • 18
  • 81
  • 148
0

This code is work for me. Set Onclicklistner to the button and check edit text is empty or not using if statement. if edit text empty, call BirthDayPick() method. I use this for date pick. In your case replace BirthDayPick() method in to your time picker method.

public class RegisterActivity extends AppCompatActivity {
    int defaultBirthYear=1990, defaultBirthMonth=0, defaultBirthDay=01;
    int BirthYear, BirthMonth, BirthDay;
    EditText etDOB;

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

        etDOB = (EditText) findViewById(R.id.etDOB);
        etDOB.setFocusable(false);

        etDOB.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BirthDayPick();
            }
        });

        final Button btnRegister = (Button) findViewById(R.id.bRegister);
        btnRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (etDOB.getText().toString().length() < 1) {
                    Toast.makeText(this, "Date of Birth cannot be empty", Toast.LENGTH_LONG).show();
                    BirthDayPick();
        }
        else{ Register(); // your registration method. 
        }   
            }
        });
    }

    public void BirthDayPick(){
        DatePickerDialog dpd = new DatePickerDialog(RegisterActivity.this, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker datePicker, int year, int monthOfYear, int dayOfMonth) {
                BirthYear = defaultBirthYear = year;
                BirthMonth = defaultBirthMonth = monthOfYear;
                BirthDay = defaultBirthDay = dayOfMonth;

                DecimalFormat df = new DecimalFormat("00");
                final EditText etDOB = (EditText) findViewById(R.id.etDOB);
                etDOB.setText(year+"-"+df.format(monthOfYear+1)+"-"+df.format(dayOfMonth));
            }
        },defaultBirthYear,defaultBirthMonth,defaultBirthDay);
        dpd.show();
    }
}
Dinith Rukshan Kumara
  • 638
  • 2
  • 10
  • 19