Search code examples
javaandroidonclicklistenerandroid-datepicker

OnClickListener only one function is fired up


This is my view where I have Monday-Sunday tables with textviews where I have dates

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background"
    android:orientation="vertical">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="50dp">
        <HorizontalScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent">

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

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

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:gravity="center"
                        android:paddingBottom="10dp"
                        android:paddingLeft="10dp"
                        android:paddingTop="10dp">

                        <TextView
                            android:id="@+id/monday"
                            android:layout_width="wrap_content"
                            android:layout_height="match_parent"
                            android:layout_marginRight="15dp"
                            android:layout_weight="1"
                            android:gravity="center|top"
                            android:text="@string/monday"
                            android:textAlignment="center"
                            android:textColor="@color/textColor"
                            android:textSize="24sp"
                            android:textStyle="bold" />
                    </LinearLayout>

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

                        <LinearLayout
                            android:id="@+id/mondayTimesList"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:orientation="vertical">

                            <LinearLayout
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:orientation="vertical"
                                android:padding="10dp">

                                <TextView
                                    android:layout_width="match_parent"
                                    android:layout_height="wrap_content"
                                    android:background="@color/orangeFlat"
                                    android:padding="5dp"
                                    android:text="Period 1"
                                    android:textColor="@color/primary" />

                                <LinearLayout
                                    android:layout_width="match_parent"
                                    android:layout_height="wrap_content"
                                    android:gravity="center">

                                    <TextView
                                        android:layout_width="100dp"
                                        android:layout_height="wrap_content"
                                        android:hint="From"
                                        android:padding="10dp" />

                                    <TextView
                                        android:layout_width="wrap_content"
                                        android:layout_height="wrap_content"
                                        android:layout_marginLeft="10dp"
                                        android:layout_marginRight="10dp"
                                        android:text=":" />

                                    <TextView
                                        android:layout_width="100dp"
                                        android:layout_height="wrap_content"
                                        android:padding="10dp"
                                        android:hint="To" />
                                </LinearLayout>
                            </LinearLayout>

                            <LinearLayout
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:orientation="vertical"
                                android:padding="10dp">

                                <TextView
                                    android:layout_width="match_parent"
                                    android:layout_height="wrap_content"
                                    android:background="@color/orangeFlat"
                                    android:padding="5dp"
                                    android:text="Period 2"
                                    android:textColor="@color/primary" />

                                <LinearLayout
                                    android:layout_width="match_parent"
                                    android:layout_height="wrap_content"
                                    android:gravity="center">

                                    <TextView
                                        android:layout_width="100dp"
                                        android:layout_height="wrap_content"
                                        android:padding="10dp"
                                        android:hint="From" />

                                    <TextView
                                        android:layout_width="wrap_content"
                                        android:layout_height="wrap_content"
                                        android:layout_marginLeft="10dp"
                                        android:layout_marginRight="10dp"
                                        android:text=":" />

                                    <TextView
                                        android:layout_width="100dp"
                                        android:layout_height="wrap_content"
                                        android:padding="10dp"
                                        android:hint="To" />
                                </LinearLayout>
                            </LinearLayout>

                            <LinearLayout
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:orientation="vertical"
                                android:padding="10dp">

                                <TextView
                                    android:layout_width="match_parent"
                                    android:layout_height="wrap_content"
                                    android:background="@color/orangeFlat"
                                    android:padding="5dp"
                                    android:text="Period 3"
                                    android:textColor="@color/primary" />

                                <LinearLayout
                                    android:layout_width="match_parent"
                                    android:layout_height="wrap_content"
                                    android:gravity="center">

                                    <TextView
                                        android:layout_width="100dp"
                                        android:layout_height="wrap_content"
                                        android:padding="10dp"
                                        android:hint="From" />

                                    <TextView
                                        android:layout_width="wrap_content"
                                        android:layout_height="wrap_content"
                                        android:layout_marginLeft="10dp"
                                        android:layout_marginRight="10dp"
                                        android:text=":" />

                                    <TextView
                                      android:layout_width="100dp"
                                      android:layout_height="wrap_content"
                                      android:padding="10dp"
                                      android:hint="To" />
                                </LinearLayout>
                            </LinearLayout>

                            <LinearLayout
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:orientation="vertical"
                                android:padding="10dp">

                                <TextView
                                    android:layout_width="match_parent"
                                    android:layout_height="wrap_content"
                                    android:background="@color/orangeFlat"
                                    android:padding="5dp"
                                    android:text="Period 4"
                                    android:textColor="@color/primary" />

                                <LinearLayout
                                    android:layout_width="match_parent"
                                    android:layout_height="wrap_content"
                                    android:gravity="center">

                                    <TextView
                                      android:layout_width="100dp"
                                      android:layout_height="wrap_content"
                                      android:padding="10dp"
                                      android:hint="From" />

                                    <TextView
                                      android:layout_width="wrap_content"
                                      android:layout_height="wrap_content"
                                      android:layout_marginLeft="10dp"
                                      android:layout_marginRight="10dp"
                                      android:text=":" />

                                    <TextView
                                        android:layout_width="100dp"
                                        android:layout_height="wrap_content"
                                        android:padding="10dp"
                                        android:hint="To" />
                                </LinearLayout>
                            </LinearLayout>

                            <LinearLayout
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:orientation="vertical"
                                android:padding="10dp">

                                <TextView
                                    android:layout_width="match_parent"
                                    android:layout_height="wrap_content"
                                    android:background="@color/orangeFlat"
                                    android:padding="5dp"
                                    android:text="Period 5"
                                    android:textColor="@color/primary" />

                                <LinearLayout
                                    android:layout_width="match_parent"
                                    android:layout_height="wrap_content"
                                    android:gravity="center">

                                    <TextView
                                        android:layout_width="100dp"
                                        android:layout_height="wrap_content"
                                        android:padding="10dp"
                                        android:hint="From" />

                                    <TextView
                                       android:layout_width="wrap_content"


                                      android:layout_height="wrap_content"
                                        android:layout_marginLeft="10dp"
                                        android:layout_marginRight="10dp"
                                        android:text=":" />

                                    <TextView
                                        android:layout_width="100dp"
                                        android:layout_height="wrap_content"
                                        android:padding="10dp"
                                        android:hint="To" />
                                </LinearLayout>
                            </LinearLayout>
                        </LinearLayout>
                    </ScrollView>
                </LinearLayout>
            </LinearLayout>
        </HorizontalScrollView>
    </RelativeLayout>
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="@color/primary"
        >
        <LinearLayout
            android:id="@+id/back"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            >
            <ImageButton
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:src="@drawable/arrow"
                android:rotation="180"
                android:layout_weight="1"
                android:onClick="backToHomeScreen"
                android:background="@color/orangeFlat"
                android:paddingVertical="10dp"/>
            <ImageButton
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:src="@drawable/save"
                android:layout_weight="1"
                android:onClick="saveAsTemplate"
                android:background="@color/orangeFlat"
                android:paddingVertical="10dp"/>
            <ImageButton
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:src="@drawable/update"
                android:layout_weight="1"
                android:onClick="updateTimming"
                android:background="@color/orangeFlat"
                android:paddingVertical="10dp"/>
        </LinearLayout>
    </RelativeLayout>
</RelativeLayout>

Here is my activity for that view

    public class TimesheetActivity extends AppCompatActivity {
    TextView selectedTextView;

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

        Intent data = getIntent();
        String times = data.getStringExtra("Times");

        int counter = 1;
        counter = fillFields(R.id.sundayTimesList, times, counter);
        counter = fillFields(R.id.mondayTimesList, times, counter);
        counter = fillFields(R.id.tuesdayTimesList, times, counter);
        counter = fillFields(R.id.wednesdayTimesList, times, counter);
        counter = fillFields(R.id.thursdayTimesList, times, counter);
        counter = fillFields(R.id.fridayTimesList, times, counter);
        fillFields(R.id.saturdayTimesList, times, counter);

    }

    private int fillFields(int id, String data, int counter){
        ViewGroup timeList = findViewById(id);
        for(int i = 0; i < 5; i++) {
            ViewGroup firstModayTimeListChild = (ViewGroup) timeList.getChildAt(i);
            ViewGroup period = (ViewGroup) firstModayTimeListChild.getChildAt(1);
            TextView periodFromText = (TextView) period.getChildAt(0);
            TextView periodToText = (TextView) period.getChildAt(2);

            periodFromText.setClickable(true);
            periodFromText.setFocusableInTouchMode(false);

            periodFromText.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
            periodToText.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);

            periodFromText.setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View view){
                    Toast.makeText(getApplicationContext(), "WORKS!", Toast.LENGTH_SHORT).show();
                    setDate(view);
                }
            });

            periodToText.setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View view){
                    setDate(view);
                }
            });

    //            Log.i("FOCUSABLE ", "" + periodFromText.isFocusable());
    //            Log.i("FOCUSABLE TOUCH MODE ", "" + periodFromText.isFocusableInTouchMode());
    //            Log.i("IS CLICKABLE ", "" + periodFromText.isClickable());

            periodFromText.setText(
                    String.format(
                            data.charAt(++counter) + "" +
                                    data.charAt(++counter) + ":" +
                                    data.charAt(++counter) + "" +
                                    data.charAt(++counter)
                    ));

            periodToText.setText(
                    String.format(
                            data.charAt(++counter) + "" +
                                    data.charAt(++counter) + ":" +
                                    data.charAt(++counter) + "" +
                                    data.charAt(++counter)
                    ));
        }
        return counter;
    }

    public void setDate(View view){
        selectedTextView = (TextView) view;
        view.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
                showDialog(0);
            }
        });
    }

    protected Dialog onCreateDialog(int id){
        Calendar now = Calendar.getInstance();

        TimePickerDialog timePickerDialog = new TimePickerDialog(this, TimePickerDialog.THEME_HOLO_DARK, timePickerListener, now.get(Calendar.HOUR_OF_DAY), now.get(Calendar.MINUTE), true);
        return timePickerDialog;
    }

    private TimePickerDialog.OnTimeSetListener timePickerListener = new TimePickerDialog.OnTimeSetListener() {

        @Override
        public void onTimeSet(TimePicker timePicker, int hour, int minute) {
            selectedTextView.setText(String.format("%02d:%02d", hour, minute));
        }
    };
    }

So my problem is when I touch/click textview first time "toast" fired up datepicker is not showing up, but after that every time when I touch/click I can see datepicker? Why datepicker is not showing up when I press textview first time? How to make it to works?

vikas kumar thanks that help


Solution

  • The reason is for the first time when you click it sets up click listener as you have set in setDate(View view); after that, it listens for subsequent calls. so you need to change the method a bit.

    public void setDate(View view){
     selectedTextView = (TextView) view;
     showDialog(0);
    }
    

    or you can just do this at the site of onClick also for date pickers.

     periodFromText.setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View view){
                    Toast.makeText(getApplicationContext(), "WORKS!", Toast.LENGTH_SHORT).show();
                    selectedTextView = (TextView) view;
                    showDialog(0);
                }
            });
    

    this doesn't require you to set two onClickListeners for the same TextView.