noname
noname

Reputation: 565

Android TextView opening datepicker after second click

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
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>
           </LinearLayout>
       </LinearLayout>
   </HorizontalScrollView>

And I have:

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.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
        periodToText.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);


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


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

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

What I try achieve is that after click on TextView want open DatePicker and almost did, I mean my code is working but I need to touch TextView two time, because after one nothing happen.

I was looking solving problem in google but what I found was related to "focusable" but it does not help.

periodFromText.focusable(false); periodFromText.focusableInTouchMode(false);

Doesn't help, any idea? Some hints would be very nice.

Ok, thanks to Jyoti JK I found solution, I was misunderstand the "focusable" I was setting it to "false" instead of "true", now when I actually set "focusableInTouchMode" on "true" everything is working properly. Thanks.

Upvotes: 0

Views: 469

Answers (2)

Gautam Surani
Gautam Surani

Reputation: 1185

try to set textview

    android:inputType="none"

Upvotes: 0

Jyoti JK
Jyoti JK

Reputation: 2171

You can try adding this in textview

  android:focusableInTouchMode="false"
  android:clickable="true"

or use Button instead.

Upvotes: 1

Related Questions