Toolbar SearchView örneği

Android Yazılım konuları
Cevapla
abdulkadirlevent
Site Admin
Mesajlar: 18
Kayıt: Pzr Oca 19, 2020 4:27 pm

Toolbar SearchView örneği

Mesaj gönderen abdulkadirlevent »

AramaActivity

Kod: Tümünü seç

public class AramaActivity  extends AppCompatActivity implements SearchView.OnQueryTextListener {
    private Toolbar toolbar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_basit_arama);

        toolbar = (Toolbar) findViewById(R.id.mToolBar);
        toolbar.setTitle("ToolBar Menu");
        toolbar.setTitleTextColor(getResources().getColor(R.color.colorAccent));
        setSupportActionBar(toolbar);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_arama, menu);
        final MenuItem item = menu.findItem(R.id.action_search);
        /* eski versiyon */
       // final SearchView searchView = (SearchView) MenuItemCompat.getActionView(item);
         final SearchView searchView = (SearchView) item.getActionView();
         searchView.setOnQueryTextListener(this);

        return super.onCreateOptionsMenu(menu);
    }
 
    @Override
    public boolean onQueryTextChange(String aramaSonucu) {
        Log.e("Harf girdikçe Sonucu",aramaSonucu);
        return true;
    }

    @Override
    public boolean onQueryTextSubmit(String query) {
        Log.e("Gönderilen Arama Sonucu",query);
        return false;
    }
}


XML menu_arama.xml

Kod: Tümünü seç

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <item
        android:id="@+id/action_search"
        android:icon="@android:drawable/ic_menu_search"
        android:title="Ara"
        app:actionViewClass="android.support.v7.widget.SearchView"
        app:showAsAction="always|collapseActionView" />

</menu>


Cevapla