0

I have two edit text views on which I have added two drawables. One is edit_text_bottom_line and another is edit_drawable.

Now I want to show edit_text_bottom_line drawable onClick of an edit text input.So I can see the layout like this :

enter image description here

This worked. Now Again I want to show another drawable when the we again click on edit text input and floating hint appears that time it should look like this:

enter image description here

This also works. But now if I again click on edit text view then it changes the layout edit_text_bottom_line drawable appears like this:

enter image description here

I want to show blue drawable when user starts typing and till he is on that edit text view and after if edit text is focused.

How can I find out if edit text is focused? I tried from onFocusChangeListener if the edit text is focused show the blue drawable but it gets dispappear on next click.

Layout:

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bg"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.kiranacustomerapp.Activities.SearchActivity"
    tools:showIn="@layout/activity_search">


        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fillViewport="true">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="380dp"
                android:background="@android:color/white"
                android:orientation="vertical"
                android:id="@+id/linearLayoutContainer">

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

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:layout_height="wrap_content"
            android:id="@+id/linear1"
            android:layout_marginTop="10dp">


            <android.support.design.widget.TextInputLayout
                android:id="@+id/input_layout_item_name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:layout_marginLeft="20dp"
                android:layout_marginRight="20dp">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="150dp"
                    android:background="@drawable/bg"
                    android:id="@+id/linearLayoutSpinner"
                    android:visibility="gone">

                    <android.support.v7.widget.RecyclerView
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:background="#ffffff"
                        android:id="@+id/recyclerview"
                        android:layout_margin="08dp"></android.support.v7.widget.RecyclerView>

                </LinearLayout>

            <android.support.design.widget.TextInputEditText
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:id="@+id/editTextItemName"
                android:layout_gravity="center_horizontal|center_vertical"
                android:hint="@string/item_name"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginBottom="07dp"
                android:textSize="14sp"
                android:imeOptions="actionDone"
                android:background="@drawable/edit_text_bottom_line"
                android:textStyle="bold"
                android:inputType="text"
                android:textColor="@color/lighttextcolor">

</android.support.design.widget.TextInputEditText>
            </android.support.design.widget.TextInputLayout>
        </LinearLayout>

        <LinearLayout
            android:orientation="vertical"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/linear2"
            android:layout_below="@+id/linear1"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true">

            <android.support.design.widget.TextInputLayout
                android:id="@+id/input_layout_item_unit"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="20dp"
                android:layout_marginRight="20dp">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="150dp"
                    android:background="@drawable/bg"
                    android:id="@+id/linearLayoutUnits"
                    android:visibility="gone">

                    <android.support.v7.widget.RecyclerView
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:background="#ffffff"
                        android:id="@+id/recyclerviewUnits"
                        android:layout_margin="08dp"></android.support.v7.widget.RecyclerView>

                </LinearLayout>
                <android.support.design.widget.TextInputEditText
                    android:layout_width="match_parent"
                    android:layout_height="40dp"
                    android:focusable="false"
                    android:layout_marginLeft="10dp"
                    android:layout_marginRight="10dp"
                    android:layout_marginBottom="07dp"
                    android:id="@+id/editTextItemUnit"
                    android:layout_gravity="center_horizontal"
                    android:hint="@string/unit"
                    android:textSize="14sp"
                    android:textStyle="bold"
                    android:imeOptions="actionDone"
                    android:background="@drawable/edit_text_bottom_line"
                    android:inputType="text"
                    android:textColor="@color/lighttextcolor" />
            </android.support.design.widget.TextInputLayout>
        </LinearLayout>

        <LinearLayout
            android:orientation="vertical"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/linear3"
            android:layout_below="@+id/linear2">

            <android.support.design.widget.TextInputLayout
                android:id="@+id/input_layout_item_quantity"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="20dp"
                android:layout_marginRight="20dp"
                android:layout_marginTop="02dp">

                <android.support.design.widget.TextInputEditText
                    android:layout_width="match_parent"
                    android:layout_height="40dp"
                    android:layout_marginLeft="10dp"
                    android:layout_marginRight="10dp"
                    android:layout_marginBottom="05dp"
                    android:id="@+id/editTextItemQuantity"
                    android:layout_gravity="center_horizontal"
                    android:hint="@string/quantity"
                    android:textSize="14sp"
                    android:imeOptions="actionDone"
                    android:textColorHint="@color/grey"
                    android:textStyle="bold"
                    android:background="@drawable/edit_text_bottom_line"
                    android:inputType="number"
                    android:textColor="@color/lighttextcolor" />
            </android.support.design.widget.TextInputLayout>
        </LinearLayout>
        </LinearLayout>
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:layout_alignParentBottom="false"
            android:layout_centerHorizontal="true"
            android:layout_below="@+id/linearp"
            android:layout_marginTop="130dp">

        <Button
            android:layout_width="100dp"
            android:layout_height="30dp"
            android:text="Select"
            style="?android:attr/borderlessButtonStyle"
            android:id="@+id/buttonSelect"
            android:background="@drawable/btn_hlf_blue"
            android:textColor="@android:color/white"
            android:textSize="12sp"
            android:layout_alignParentBottom="false"
            android:layout_centerHorizontal="true" />
        </RelativeLayout>
            </RelativeLayout>
        </ScrollView>

</RelativeLayout>

code:

   containerLayout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            edt_Item_Name.setFocusable(false);
            edt_Item_Unit.setFocusable(false);
            edt_Item_quantity.setFocusable(false);

            linearLayoutRecycleView.setVisibility(View.GONE);
            linearLayoutUnits.setVisibility(View.GONE);
            textInput_Item_quantity.setVisibility(View.VISIBLE);
            textInput_Item_Unit.setVisibility(View.VISIBLE);
            textInput_Item_Unit.setBackgroundResource(0);
            textInput_Item_name.setBackgroundResource(0);

            edt_Item_Name.setBackgroundDrawable(ContextCompat.getDrawable(SearchActivity.this,R.drawable.edit_text_bottom_line));
            edt_Item_Unit.setBackgroundDrawable(ContextCompat.getDrawable(SearchActivity.this,R.drawable.edit_text_bottom_line));
            edt_Item_quantity.setBackgroundDrawable(ContextCompat.getDrawable(SearchActivity.this,R.drawable.edit_text_bottom_line));


            edt_Item_Name.setText("");
            edt_Item_quantity.setText("");
            edt_Item_Unit.setText("");

        }
    });

    edt_Item_Name.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View view, boolean b) {

            if(b) {

                edt_Item_Name.setBackgroundDrawable(ContextCompat.getDrawable(SearchActivity.this, R.drawable.edit_drawable));
                edt_Item_Unit.setBackgroundDrawable(ContextCompat.getDrawable(SearchActivity.this, R.drawable.edit_text_bottom_line));
                edt_Item_quantity.setBackgroundDrawable(ContextCompat.getDrawable(SearchActivity.this, R.drawable.edit_text_bottom_line));
            }

        }
    });

    edt_Item_Unit.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View view, boolean b) {

            if(b) {
                edt_Item_Unit.setBackgroundDrawable(ContextCompat.getDrawable(SearchActivity.this, R.drawable.edit_drawable));
                edt_Item_Name.setBackgroundDrawable(ContextCompat.getDrawable(SearchActivity.this, R.drawable.edit_text_bottom_line));
                edt_Item_quantity.setBackgroundDrawable(ContextCompat.getDrawable(SearchActivity.this, R.drawable.edit_text_bottom_line));
            }

        }
    });

    edt_Item_quantity.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View view, boolean b) {

            edt_Item_quantity.setBackgroundDrawable(ContextCompat.getDrawable(SearchActivity.this,R.drawable.edit_drawable));
            edt_Item_Name.setBackgroundDrawable(ContextCompat.getDrawable(SearchActivity.this,R.drawable.edit_text_bottom_line));
            edt_Item_Unit.setBackgroundDrawable(ContextCompat.getDrawable(SearchActivity.this,R.drawable.edit_text_bottom_line));


        }
    });

    edt_Item_Name.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            edt_Item_Name.setFocusableInTouchMode(true);

        mRecyclerView.setLayoutManager(new LinearLayoutManager(SearchActivity.this));
        mRecyclerView.setAdapter(mAdapter);

        edt_Item_Name.setText("");

        edt_Item_Name.setBackgroundResource(R.drawable.edit_text_bottom_line);
            edt_Item_quantity.setBackgroundDrawable(ContextCompat.getDrawable(SearchActivity.this,R.drawable.edit_text_bottom_line));
            edt_Item_Unit.setBackgroundDrawable(ContextCompat.getDrawable(SearchActivity.this,R.drawable.edit_text_bottom_line));


            edt_Item_Name.setHintTextColor(ContextCompat.getColor(SearchActivity.this,R.color.grey));
        linearLayoutRecycleView.setVisibility(View.VISIBLE);

            linearLayoutUnits.setVisibility(View.GONE);
        textInput_Item_quantity.setVisibility(View.GONE);
        textInput_Item_Unit.setVisibility(View.GONE);

        textInput_Item_name.setBackgroundResource(R.drawable.purple_bg);
            textInput_Item_Unit.setBackgroundResource(0);

        }
    });


    edt_Item_Name.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
            // When user changed the Text
            SearchActivity.this.mAdapter.getFilter().filter(cs);
        }

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                                      int arg3) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable arg0) {
            // TODO Auto-generated method stub
        }
    });


    edt_Item_Unit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            edt_Item_Unit.setFocusable(true);
            edt_Item_Name.setFocusable(false);
            edt_Item_Unit.setFocusableInTouchMode(true);

            edt_Item_Name.setBackgroundResource(0);

            mUnitsRecyclerView.setLayoutManager(new LinearLayoutManager(SearchActivity.this));
            mUnitsRecyclerView.setAdapter(mUnitsAdapter);

            edt_Item_Unit.setText("");

            edt_Item_Unit.setBackgroundResource(R.drawable.edit_text_bottom_line);
            edt_Item_Unit.setHintTextColor(ContextCompat.getColor(SearchActivity.this,R.color.grey));
            edt_Item_Name.setBackgroundDrawable(ContextCompat.getDrawable(SearchActivity.this,R.drawable.edit_text_bottom_line));
            edt_Item_quantity.setBackgroundDrawable(ContextCompat.getDrawable(SearchActivity.this,R.drawable.edit_text_bottom_line));


            linearLayoutUnits.setVisibility(View.VISIBLE);
            linearLayoutRecycleView.setVisibility(View.GONE);
            textInput_Item_quantity.setVisibility(View.GONE);

            textInput_Item_Unit.setBackgroundResource(R.drawable.purple_bg);
        }
    });

    edt_Item_Unit.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
            // When user changed the Text

            SearchActivity.this.mUnitsAdapter.getFilter().filter(cs);
        }

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                                      int arg3) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable arg0) {
            // TODO Auto-generated method stub

        }
    });


    edt_Item_quantity.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            edt_Item_quantity.setFocusableInTouchMode(true);
            edt_Item_Name.setBackgroundDrawable(ContextCompat.getDrawable(SearchActivity.this,R.drawable.edit_text_bottom_line));
            edt_Item_Unit.setBackgroundDrawable(ContextCompat.getDrawable(SearchActivity.this,R.drawable.edit_text_bottom_line));

        }
    });

    edt_Item_quantity.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View view, boolean b) {

            if(b) {
                edt_Item_Name.setBackgroundDrawable(ContextCompat.getDrawable(SearchActivity.this, R.drawable.edit_text_bottom_line));
                edt_Item_Unit.setBackgroundDrawable(ContextCompat.getDrawable(SearchActivity.this, R.drawable.edit_text_bottom_line));
                edt_Item_quantity.setBackgroundDrawable(ContextCompat.getDrawable(SearchActivity.this, R.drawable.edit_drawable));
            }
            else {

            }
        }
    });

Or rather applying two layouts, I have set the accent color as blue in the theme so by default the edit text takes blue color if focused that's fine but when I change the drawable of grey line after that how can I go to default blue line of an edit text?

Please help, thank you..

Sid
  • 2,792
  • 9
  • 55
  • 111

2 Answers2

0

Just do one thing make your input text layout like this:

 <android.support.design.widget.TextInputLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    app:hintTextAppearance="@android:style/TextAppearance.Small">

                    <EditText
                        android:id="@+id/user_name"
                        android:layout_height="wrap_content"
                        android:layout_width="match_parent"
                        android:padding="@dimen/_10sdp"
                        android:hint="@string/full_name"
                        android:inputType="text"
                        android:background="@android:color/transparent"
                        />

                </android.support.design.widget.TextInputLayout>

And it will solve your problem.

Rishabh Mahatha
  • 1,251
  • 9
  • 19
  • can you please explain what have you done and how it will solve the problem? – Sid Dec 23 '16 at 18:28
  • wont work, if background kept as transparent I cant see edit text line by default. I want the edit text with blue line by default then change the background and again in blue line when clicked and floating hint appears – Sid Dec 24 '16 at 11:28
0

Change your xml TextInputLayout and TextInputEditText with this:

<android.support.design.widget.TextInputLayout
            android:id="@+id/input_layout_item_quantity"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:layout_marginTop="02dp"
            android:textColorHint="@color/grey">

            <android.support.design.widget.TextInputEditText
                android:id="@+id/editTextItemQuantity"
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:layout_gravity="center_horizontal"
                android:layout_marginBottom="05dp"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:hint="@string/quantity"
                android:imeOptions="actionDone"
                android:inputType="number"
                android:textColor="@color/lighttextcolor"
                android:textSize="14sp"
                android:textStyle="bold"
                android:theme="@style/style_edittext_color_control"/>
        </android.support.design.widget.TextInputLayout>

In your res/values/styles.xml add this:

<style name="style_edittext_color_control" parent="Theme.AppCompat.Light">
        <item name="colorControlNormal">@color/grey</item>
        <item name="colorControlActivated">@color/blue</item>
    </style>

Hope this will give you proper result.

Sneha Sarkar
  • 731
  • 6
  • 19
  • Thank you.. this worked, but onClickListener of an edit text as I am clicking i get the blue background and grey color but edit text line blinks with blue color for a second and then I can see the grey color line. – Sid Dec 31 '16 at 06:59
  • can you tell me solution for this blinking line? @Sneha Sarkar – Sid Dec 31 '16 at 08:22
  • I didn't understand your problem. – Sneha Sarkar Dec 31 '16 at 08:46
  • as I have set this style to an edit text, when I first click on edit text the focus gets changed cursor is not visible and edit text is not activated,this time the bottom line of an edit text blinks with blue line and then shows a grey line. and after when i again click on an edit text it gets activated floating label appears and it works fine with blue bottom line that time. – Sid Dec 31 '16 at 09:04
  • I think you have done some coding in java file to achieve this result before. If my assumption is right, then remove those lines. Only xml itself can manage your problem. – Sneha Sarkar Dec 31 '16 at 09:09
  • I have removed all the code, and implemented style xml. and only onClickListener is implemented. @Sneha Sarkar – Sid Jan 02 '17 at 05:17