activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<SearchView
android:id="@+id/searchView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"
android:layout_marginTop="30dp" >
</SearchView>
</RelativeLayout>
MainActivity.java
package com.androidian.search;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
SearchView search;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
search=(SearchView) findViewById(R.id.searchView1);
search.setQueryHint("SearchView");
//*** setOnQueryTextFocusChangeListener ***
search.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(), String.valueOf(hasFocus),
Toast.LENGTH_SHORT).show();
}
});
//*** setOnQueryTextListener ***
search.setOnQueryTextListener(new OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(), query,
Toast.LENGTH_SHORT).show();
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
// TODO Auto-generated method stub
// Toast.makeText(getBaseContext(), newText,
Toast.LENGTH_SHORT).show();
return false;
}
});
}
}