BigT
BigT

Reputation: 1433

Change the width of EditText on text changed

I am trying to change the width of my Search Bar so that when a user starts typing the Search Bar covers the fav button but when they aren't typing the fav button shows up.

Here is what I have now

searchBar.addTextChangedListener(new TextWatcher(){         
        public void afterTextChanged(Editable arg0){
            menuItems.this.adapter.getFilter().filter(arg0);
            if(searchBar.getText().length() <= 0){

                searchBar.getLayoutParams().width = 280;
                favButton.getLayoutParams().width = 15;
            }
            else{                   
                searchBar.getLayoutParams().width = 295;
                favButton.getLayoutParams().width = 0;
            }
        }
        public void beforeTextChanged(CharSequence s, int start, int count,int after) {
        }
        public void onTextChanged(CharSequence s, int start, int before, int count) {               
        }
    }); 

Upvotes: 0

Views: 760

Answers (2)

Lucifer
Lucifer

Reputation: 29632

You need to set layout_width element in your AndroidManifest.xml as follows,

android:layout_width="fill_parent"

Just set this element in your EditText as follows,

<EditText android:id="@+id/txtURL" android:layout_width="fill_parent"
...
...>
</EditText>

Upvotes: 0

Buffalo
Buffalo

Reputation: 4042

I suggest using a different approach if your goal is to hide/show a View (Fav button in your case).

Try this:

if(searchBar.getText().length() <= 0) {
    searchBar.getLayoutParams().width = 280;
    favButton.setVisibility(View.Visible);
} else {                   
    searchBar.getLayoutParams().width = 295;
    favButton.setVisibility(View.GONE);
}

Hard to say how well it will work without knowing your layout, but it's generally not a very good practice to play with fix dimension sizes.

Upvotes: 1

Related Questions