I have tried other questions and answer but have not found a solution.
I am creating a social app that let's people search for others. When I click the search bar I want and type in a letter, for e.g. B, I see the list of users with the name B as expected. But when I removed the B and the search bar is empty, the list of users still shows up.
When nothing is in the search bar, I want the list of users to disappear. This is how I create the edit text in my activity_search:
<EditText
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_marginHorizontal="5dp"
android:hint="Search Username Here"
android:textSize="22dp"
android:id="@+id/input"
android:inputType="text"
android:layout_gravity="center"
android:background="@drawable/round_edit_text" />
This is where I set that edit text in the java activity:
input = findViewById(R.id.input);
And this is the whole java file:
EditText input;
RecyclerView FindFriendsRecyclerList;
DatabaseReference mRef;
Query query;
Context mContext;
TextWatcher textWacher;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
input = findViewById(R.id.input);
input.addTextChangedListener(textWacher);
TextWatcher textWacher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
String strText = s.toString();
if (strText .length() == 0) {
// removeList();
FindFriendsRecyclerList.setVisibility(View.INVISIBLE);
Toast.makeText(mContext, "Remove list", Toast.LENGTH_SHORT).show();
} else {
// showList();
}
}
@Override
public void afterTextChanged(Editable s) {
}
};
FindFriendsRecyclerList = (RecyclerView) findViewById(R.id.recycler);
FindFriendsRecyclerList.setLayoutManager(new LinearLayoutManager(this));
findViewById(R.id.search).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mRef = FirebaseDatabase.getInstance().getReference().child("Users");
query = mRef.orderByChild("fullname").orderByChild("username").startAt(input.getText().toString()).endAt(input.getText().toString()+"\uf8ff");
setdata();
}
});
checkIfEmpty();
if (input.getText().toString().length() == 0) {
FindFriendsRecyclerList.setVisibility(View.INVISIBLE);
}
}
private void setdata() {
FirebaseRecyclerOptions<User> options =
new FirebaseRecyclerOptions.Builder<User>()
.setQuery(query, User.class)
.build();
FirebaseRecyclerAdapter<User, FindFriendsViewHolder> adapter =
new FirebaseRecyclerAdapter<User, FindFriendsViewHolder>(options) {
@Override
protected void onBindViewHolder(@NonNull FindFriendsViewHolder holder, final int position, @NonNull User model) {
holder.userName.setText(model.getUsername());
holder.userFullName.setText(model.getFullname());
Picasso.get().load(model.getImageurl()).placeholder(R.drawable.profile_pic).into(holder.profileImage);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//String visit_user_id = getRef(position).getKey();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.search_users, new ProfileFragment()).commit();
}
});
}
@NonNull
@Override
public FindFriendsViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.user_item, viewGroup, false);
FindFriendsViewHolder viewHolder = new FindFriendsViewHolder(view);
return viewHolder;
}
};
FindFriendsRecyclerList.setAdapter(adapter);
adapter.startListening();
}
public static class FindFriendsViewHolder extends RecyclerView.ViewHolder {
public TextView userName, userFullName;
public CircleImageView profileImage;
public FindFriendsViewHolder(@NonNull View itemView) {
super(itemView);
userName = itemView.findViewById(R.id.username);
userFullName = itemView.findViewById(R.id.fullname);
profileImage = itemView.findViewById(R.id.image_profile);
}
}
public void checkIfEmpty() {
TextWatcher textWacher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
String strText = s.toString();
if (strText.length() == 0) {
FindFriendsRecyclerList.setVisibility(View.INVISIBLE);
} else {
// showList();
}
}
@Override
public void afterTextChanged(Editable s) {
FindFriendsRecyclerList.setVisibility(View.INVISIBLE);
}
};
}
As you can see above I have tried the following two ways to check if it's empty:
if (input.getText().toString().length() == 0) {
FindFriendsRecyclerList.setVisibility(View.INVISIBLE);
}
public void checkIfEmpty(View v){
if(input.getText().toString().equals("")) {
input.setVisibility(View.INVISIBLE);
}
}