Keegan Cruickshank
Keegan Cruickshank

Reputation: 51

Datepicker and TimePicker dialogs take two clickes of button

So for the life of me I can not find the reason behind needing to click twice on the start date and start time for the picker dialog to open. I have searched these forums many times and they have all been mostly related to edit text fields whereas mine is a simple button but the onClickListener takes two hits. Thanks in advance.

This is my Class:

package com.shotsevolved.app

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.Toast;
import com.parse.FindCallback;
import com.parse.Parse;
import com.parse.ParseException;
import com.parse.ParseGeoPoint;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import com.parse.SaveCallback;
import java.util.List;


public class DealCreator extends FragmentActivity {


    String mUsername;
    String companyName;
    ParseGeoPoint location;
    String title;
    double mOldPrice;
    double mNewPrice;
    boolean isFree;
    boolean isUnlimited;
    String mDescription;
    int mUses;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Parse.initialize(this, "Ztgl9DAaj4XPrDnS2Ro8jNHiaNnTPFCeF6V1Gm71", "26QMHWwfHmxKfwMvKemaEXH2XsFxpO5sR8Csuo9v");
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_deal_creator);

        final Button create = (Button)findViewById(R.id.createButton);
        final ProgressBar progress = (ProgressBar)findViewById(R.id.progressIcon);
        final LinearLayout view = (LinearLayout)findViewById(R.id.linView);
        final LinearLayout view1 = (LinearLayout)findViewById(R.id.linView1);
        final LinearLayout main = (LinearLayout)findViewById(R.id.mainLinear);
        final CheckBox freeBox = (CheckBox)findViewById(R.id.freeBox);
        final EditText oldPrice = (EditText)findViewById(R.id.oldPrice);
        final EditText newPrice = (EditText)findViewById(R.id.newPrice);
        final CheckBox unlimited = (CheckBox)findViewById(R.id.unlimitedBox);
        final EditText uses = (EditText)findViewById(R.id.uses);
        final Button date = (Button)findViewById(R.id.startDate);
        final Button time = (Button)findViewById(R.id.startTime);


        create.setVisibility(View.INVISIBLE);

        Intent intent = getIntent();
        mUsername = intent.getStringExtra("key");

        ParseQuery<ParseObject> query = ParseQuery.getQuery("appUsers");
        query.whereEqualTo("username", mUsername);
        query.findInBackground(new FindCallback<ParseObject>() {
            public void done(List<ParseObject> user, ParseException e) {
                if(user.size() == 1 && e == null){
                    int admin = user.get(0).getInt("admin");
                    if(admin == 2){

                        ParseQuery<ParseObject> query = ParseQuery.getQuery("AdminNames");
                        query.whereEqualTo("username", mUsername);
                        query.findInBackground(new FindCallback<ParseObject>() {
                            @Override
                            public void done(final List<ParseObject> user, ParseException e) {
                                if(user.size() == 1 && e == null){

                                    unlimited.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                                        @Override
                                        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                                            if(isChecked == true){
                                                uses.setVisibility(View.INVISIBLE);
                                                view1.removeView(uses);
                                            }else{
                                                uses.setVisibility(View.VISIBLE);
                                                view1.addView(uses);
                                            }
                                        }
                                    });

                                    freeBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                                        @Override
                                        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                                            if(isChecked == true){
                                                oldPrice.setVisibility(View.INVISIBLE);
                                                newPrice.setVisibility(View.INVISIBLE);
                                                view.removeView(oldPrice);
                                                view.removeView(newPrice);
                                            }else{
                                                oldPrice.setVisibility(View.VISIBLE);
                                                newPrice.setVisibility(View.VISIBLE);
                                                view.addView(oldPrice);
                                                view.addView(newPrice);
                                            }
                                        }
                                    });

                                    date.setOnClickListener(new View.OnClickListener() {
                                        @Override
                                        public void onClick(View v) {
                                            showDatePickerDialog(main);
                                        }
                                    });

                                    time.setOnClickListener(new View.OnClickListener() {
                                        @Override
                                        public void onClick(View v) {
                                            showTimePickerDialog(main);
                                        }
                                    });

                                    progress.setVisibility(View.GONE);
                                    view.removeView(progress);
                                    create.setVisibility(View.VISIBLE);

                                    create.setOnClickListener(new View.OnClickListener() {
                                        @Override
                                        public void onClick(View v) {
                                            if(freeBox.isChecked()){
                                                isFree = true;
                                                mOldPrice = 0;
                                                mNewPrice = 0;
                                            }else{

                                                mOldPrice = Double.parseDouble(oldPrice.getText().toString());
                                                mNewPrice = Double.parseDouble(newPrice.getText().toString());
                                                isFree = false;
                                            }
                                            if(unlimited.isChecked()){
                                                isUnlimited = true;
                                                mUses = 0;
                                            }else{

                                                mUses = Integer.parseInt(uses.getText().toString());
                                                isUnlimited = false;
                                            }

                                            //Call create deal class
                                            deal();
                                        }
                                    });
                                }else{
                                    Context context = getApplicationContext();
                                    CharSequence text = "Error!!! Database Hacked!";
                                    int duration = Toast.LENGTH_LONG;
                                    Toast toast = Toast.makeText(context, text, duration);
                                    toast.show();
                                }
                            }
                        });

                    }else{
                        Context context = getApplicationContext();
                        CharSequence text = "Error!!! You are not an Admin!";
                        int duration = Toast.LENGTH_LONG;
                        Toast toast = Toast.makeText(context, text, duration);
                        toast.show();
                    }

                }else{
                    Context context = getApplicationContext();
                    CharSequence text = "Error!!! Database Hacked!";
                    int duration = Toast.LENGTH_LONG;
                    Toast toast = Toast.makeText(context, text, duration);
                    toast.show();
                }
            }
        });
    }

    private void deal() {

        ParseObject newDeal = new ParseObject("Deals");
        newDeal.put("uses", mUses);
        newDeal.put("unlimitedUses", isUnlimited);
        newDeal.put("description", mDescription);
        newDeal.put("free", isFree);
        newDeal.put("title", title);
        newDeal.put("oldPrice", mOldPrice);
        newDeal.put("newPrice", mNewPrice);
        newDeal.put("location", location);
        newDeal.put("username", mUsername);
        newDeal.put("companyName", companyName);
        newDeal.saveInBackground(new SaveCallback() {
            @Override
            public void done(ParseException e) {
                Context context = getApplicationContext();
                CharSequence text = "Deal Saved";
                int duration = Toast.LENGTH_SHORT;
                Toast toast = Toast.makeText(context, text, duration);
                toast.show();
            }
        });
    }

    public void showTimePickerDialog(View v) {
        DialogFragment newFragment = new TimePickerFragment();
        newFragment.show(getSupportFragmentManager(), "timePicker");
    }

    public void showDatePickerDialog(View v) {
        DialogFragment newFragment = new DatePickerFragment();
        newFragment.show(getSupportFragmentManager(), "datePicker");
    }

}

And these are my Fragments:

Date:

package com.shotsevolved.app;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.widget.DatePicker;

import java.util.Calendar;


public 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) {
        // Do something with the date chosen by the user
    }
}

Time:

package com.shotsevolved.app;

import android.app.Dialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.text.format.DateFormat;
import android.widget.TimePicker;

import java.util.Calendar;


public class TimePickerFragment extends DialogFragment
        implements TimePickerDialog.OnTimeSetListener {

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

        // Create a new instance of TimePickerDialog and return it
        return new TimePickerDialog(getActivity(), this, hour, minute,
                DateFormat.is24HourFormat(getActivity()));
    }

    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        // Do something with the time chosen by the user
    }
}

And finally my XML:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="#1e1c1c"
    android:id="@+id/root"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="@dimen/height"
        android:layout_alignParentTop="true"
        android:background="@color/purple"
        xmlns:android="http://schemas.android.com/apk/res/android">

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">

            <ImageButton
                android:id="@+id/btn_backFromSettings"
                android:layout_width="@dimen/width"
                android:layout_height="fill_parent"
                android:background="@drawable/ui_button_purple"
                android:contentDescription="@string/desc"
                android:src="@drawable/ico_left" />

            <LinearLayout
                android:layout_width="@dimen/divider_size"
                android:layout_height="fill_parent"
                android:background="@color/dark_purple" >
            </LinearLayout>

            <TextView
                android:id="@+id/mainLogin"
                android:layout_width="0dip"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:gravity="center"
                android:tag="bold"
                android:text="@string/dealCreator"
                android:textColor="@color/white"
                android:textSize="@dimen/tex_size_xxlarge" />

            <LinearLayout
                android:layout_width="@dimen/divider_size"
                android:layout_height="fill_parent"
                android:background="@color/dark_purple" >
            </LinearLayout>

        </LinearLayout>
    </LinearLayout>



    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical"
            android:id="@+id/mainLinear">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:padding="@dimen/dim_20"
                android:id="@+id/linView">

                <ProgressBar
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/progressIcon"
                    android:layout_gravity="center_horizontal" />

                <EditText
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/titleOfDeal"
                    style="@style/EditText_Purple"
                    android:hint="Title of Deal"
                    android:layout_gravity="center_horizontal" />

                <EditText
                    android:layout_width="match_parent"
                    android:layout_height="200dp"
                    android:id="@+id/dealDescription"
                    android:gravity="top"
                    android:layout_marginTop="@dimen/dim_10"
                    style="@style/EditText_Purple"
                    android:hint="Describe company and deal"
                    android:layout_gravity="center_horizontal" />

                <CheckBox
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Free"
                    android:layout_marginTop="@dimen/dim_10"
                    style="@style/CheckBox_Purple"
                    android:textColor="@color/offwhite"
                    android:id="@+id/freeBox" />

                <EditText
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/oldPrice"
                    android:layout_marginTop="@dimen/dim_10"
                    style="@style/EditText_Purple"
                    android:hint="Old cost of product"
                    android:inputType="numberDecimal"
                    android:layout_gravity="center_horizontal" />

                <EditText
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/newPrice"
                    android:layout_marginTop="@dimen/dim_10"
                    android:inputType="numberDecimal"
                    style="@style/EditText_Purple"
                    android:hint="New cost of product"
                    android:layout_gravity="center_horizontal" />
                </LinearLayout>



            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:id="@+id/linView1"
                android:paddingRight="@dimen/dim_20"
                android:paddingLeft="@dimen/dim_20">

                <CheckBox
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Unlimited uses"
                    style="@style/CheckBox_Purple"
                    android:textColor="@color/offwhite"
                    android:id="@+id/unlimitedBox" />

                <EditText
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/uses"
                    android:layout_marginTop="@dimen/dim_10"
                    style="@style/EditText_Purple"
                    android:hint="Number of uses per customer"
                    android:inputType="number"
                    android:layout_gravity="center_horizontal" />

            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:id="@+id/linView2"
                android:gravity="center_horizontal"
                android:layout_marginTop="@dimen/dim_10"
                android:paddingRight="@dimen/dim_20"
                android:paddingLeft="@dimen/dim_20">

                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/startDate"
                    android:layout_marginTop="@dimen/dim_10"
                    style="@style/EditText_Purple"
                    android:hint="Start Date"
                    android:layout_marginRight="@dimen/dim_10"
                    android:layout_gravity="center_horizontal" />

                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/startTime"
                    android:layout_marginTop="@dimen/dim_10"
                    style="@style/EditText_Purple"
                    android:hint="Start Time"
                    android:layout_gravity="center_horizontal" />

            </LinearLayout>

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:orientation="vertical">

                <LinearLayout
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal"
                    android:layout_marginTop="@dimen/dim_10"
                    android:gravity="center_horizontal">

                    <Button
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Set expiry date"
                        android:layout_marginRight="@dimen/dim_10"
                        android:padding="@dimen/dim_10"
                        style="@style/Button_Purple"
                        android:id="@+id/dateButtonEnd"
                        android:layout_gravity="center_horizontal" />

                    <Button
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Set expiry time"
                        android:padding="@dimen/dim_10"
                        style="@style/Button_Purple"
                        android:id="@+id/timeButtonEnd"
                        android:layout_gravity="center_horizontal" />
                </LinearLayout>


            </LinearLayout>

                <Button
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:text="Create"
                    android:padding="@dimen/dim_10"
                    android:layout_marginTop="@dimen/dim_10"
                    android:layout_marginLeft="@dimen/dim_20"
                    android:layout_marginRight="@dimen/dim_20"
                    style="@style/Button_Purple"
                    android:id="@+id/createButton"
                    android:layout_gravity="center_horizontal" />

            </LinearLayout>
    </ScrollView>

</LinearLayout>

Upvotes: 0

Views: 1187

Answers (2)

Vesko
Vesko

Reputation: 3760

Hmm, your code looks ok. Can you try adding android:focusable="false" to your buttons. I'm curious if the problem is that you're just requesting focus the first click and the second actually initiates the click.

Also, if this doesn't help, can you put some logs in your click listener and also in the public void showTimePickerDialog(View v) { method as well ... to see if it's triggered the first click at all.

Upvotes: 1

Kamalanathan
Kamalanathan

Reputation: 1747

Try to add delay on button click it will avoid multi click

date.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            date.setEnabled(false);
            showDatePickerDialog(main);
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    date.setEnabled(true);
                }
            }, 100);
        }
    });

Upvotes: 0

Related Questions