Home » Android » android – How I can use EditText instead of TextView and open the datePicker in a single click?

android – How I can use EditText instead of TextView and open the datePicker in a single click?

Posted by: admin June 15, 2020 Leave a comment

Questions:

I am using materialdatetime picker to create a form. If I am using TextView, It is working good, but I want to use EditText for it, I am uploading the image you can see if I am using EditText how it looks and if I am using TextView how it looks.But when using EditText I need to click twice to open the DatePicker. For first click it behaves like an editable textbox .

  <!-- Adding Payment Method  progress -->
    <ProgressBar
        android:id="@+id/payment_method_progress"
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:visibility="gone" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/adding_payment_method_heading"
        android:textSize="@dimen/activity_heading"
        android:elevation="0dp"
        android:layout_marginBottom="@dimen/activity_vertical_margin"
        />


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/adding_payment_method_description"
        android:textSize="@dimen/activity_description"
        android:elevation="0dp"
        android:layout_marginBottom="@dimen/activity_vertical_margin"
        />

    <ScrollView
        android:id="@+id/adding_payment_method_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

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

            <android.support.design.widget.TextInputLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/layout_user_id"
                >

                <EditText
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="@string/enter_user_name"
                    android:inputType="text"
                    android:maxLines="1"
                    android:singleLine="true"
                    android:id="@+id/user_id" />
            </android.support.design.widget.TextInputLayout>
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="left"
                    android:layout_weight="1"
                    android:text="Select Payment Methods" />

                <Spinner
                    android:id="@+id/payment_methods"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="right"
                    android:layout_weight="1" />
            </LinearLayout>
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:id="@+id/check_chosen"
                >

            <android.support.design.widget.TextInputLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/layout_amount"
                >

                <EditText
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="@string/amount"
                    android:inputType="numberDecimal"
                    android:maxLines="1"
                    android:singleLine="true"
                    android:id="@+id/amount" />
            </android.support.design.widget.TextInputLayout>

            <android.support.design.widget.TextInputLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/layout_check_number"
                >

                <EditText
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="@string/check_number"
                    android:inputType="number"
                    android:maxLines="1"
                    android:singleLine="true"
                    android:id="@+id/check_number" />
            </android.support.design.widget.TextInputLayout>

            <android.support.design.widget.TextInputLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/layout_bank_name"
                >

                <EditText
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="@string/bank_name"
                    android:inputType="text"
                    android:maxLines="1"
                    android:singleLine="true"
                    android:id="@+id/bank_name" />
            </android.support.design.widget.TextInputLayout>


                <android.support.design.widget.TextInputLayout
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/date_of_check"
                    android:layout_weight="1"
                    >
                    <EditText
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:id="@+id/check_date"
                        android:hint="Date"
                        android:inputType="date" />
                </android.support.design.widget.TextInputLayout>
            </LinearLayout>

            <android.support.design.widget.TextInputLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/layout_payment_info"
                >

                <EditText
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="@string/payment_info"
                    android:inputType="text"
                    android:maxLines="1"
                    android:singleLine="true"
                    android:id="@+id/payment_info" />
            </android.support.design.widget.TextInputLayout>

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/payment_date"
                    android:hint="@string/payment_date"
                    android:inputType="date" />

            <Button
                android:id="@+id/add_payment_method_action"
                style="?android:textAppearanceSmall"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="16dp"
                android:text="@string/submit"
                android:onClick="testSubmit"
                android:textStyle="bold" />


     </LinearLayout>
    </ScrollView>
</LinearLayout>

enter image description here

Please let me know, if I need to post the java code here.

How to&Answers:

put

android:focusable="false"
android:focusableInTouchMode="false"

in your edit text

Answer:

You can use EditText to achieve your requirement. Firstly Do like this

 EditText edTxt;
 edTxt = (TextView) row.findViewById(R.id.acti_edtTxt_date);

Also you can do it from code to hide editable like this

edTxt.setClickable(false);

Now Apply OnClickListener on this edTxt and open DatePicker or what you want.

Please note that setClickable = “false ” in your xml file where you are
defining your EditText

<EditText ...
    android:clickable="false" 
    android:cursorVisible="false" 
    android:focusable="false" 
    android:focusableInTouchMode="false">

Answer:

You can achieve it by making it focusable false.

<EditText ...
    android:cursorVisible="false" 
    android:focusable="false" 
    android:focusableInTouchMode="false">

Answer:

In your edit text try this

android:editable="false"
android:enabled="true"

Answer:

Put the lines below in your edit text:

android:editable="false"
android:focusableInTouchMode="false"