How to get default edit line color after setting a drawable?

372 views Asked by At

I am using a TextInputEditText. For this I am setting a drawable programatically on click of same edit text.

By default the edit text shows blue bottom line and hint color as blue i.e accent color which I have set in theme.

Now I after setting the drawable programatically, I want to show edit text as default.

I tried to set drawable to null or background resource as 0 but this totally removes the drawable from edit text and bottom line disappears.

What should I do to get it back to default?

Looks like this after setting a drawable.

enter image description here

And later drawable continues but if I click on edt_unit I tried edt_Item_Name.setBackgroundResource(0); : so bottom line disappears.

enter image description here

Code:

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

            edt_Item_Name.setFocusable(false);
            edt_Item_Unit.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.setText("");
            edt_Item_quantity.setText("");
            edt_Item_Unit.setText("");

        }
    });

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

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

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



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

xml :

  <?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="match_parent"
            android:fillViewport="true">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                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:focusable="false"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginBottom="07dp"
                android:textSize="14sp"
                android:textStyle="bold">

</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.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.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:textColorHint="@color/grey"
                    android:textStyle="bold" />
            </android.support.design.widget.TextInputLayout>
        </LinearLayout>
        </LinearLayout>
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true">

        <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>

Can anyone help please thank you..

2

There are 2 answers

0
Apoorv Mehrotra On BEST ANSWER

you can try this also:

<EditText
    android:id="@+id/et"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:hint="Input your country"
    android:background="@drawable/edittext_bg"
    android:padding="10dp"
    />

now code for "edittext_bg" you can customize also as per your choice:

res/drawable/edittext_bg.xml

    <?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <!-- Draw a 2dp width border around shape -->
            <stroke
                android:color="#ff1e0c"
                android:width="2dp"
                />
        </shape>
    </item>
    <!-- Overlap the left, top and right border using background color  -->
    <item
        android:bottom="2dp"
        >
        <shape android:shape="rectangle">
            <solid android:color="#fffbce"/>
        </shape>
    </item>
</layer-list> 
11
pankaj khedekar On

But why did you applied a drawable to it? Anyways,you can add view below the textInput layout which has wraped your edit text in it. and set the edt_Item_Name.setBackgroundResource(0);

your xml will look like that.

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

<EditText
    ....

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



<View
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:background="@color/your_color"
        ></View>

UPDATED

    <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:focusable="false"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginBottom="07dp"
            android:textSize="14sp"
            android:textStyle="bold">

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

add view here

                   <View
                        android:layout_width="match_parent"
                        android:layout_height="2dp"
                        android:background="@color/your_color">
                   </View>
        </LinearLayout>

ANS 2

TextInputLayout textInputLayout =(TextInputLayout)findViewById(R.id.input_layout_item_name);

textInputLayout.setBackgroundResource(R.drawable.your_bg);

and after click on another edit text set

textInputLayout.setBackgroundResource(0);