MainActivity.java
================
package com.sam.web.listviewsearch;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.Toast;
import java.util.List;
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {
private String[] countryNames;
private ListView listView;
ArrayAdapter
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.setTitle("Country Names");
listView=(ListView) findViewById(R.id.listViewId);
countryNames=getResources().getStringArray(R.array.country_names);
adapter=new ArrayAdapter
listView.setAdapter(adapter);
listView.setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView> parent, View view, int position, long id) {
String value=adapter.getItem(position);
Toast.makeText(getApplicationContext(),value,Toast.LENGTH_SHORT).show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater=getMenuInflater();
menuInflater.inflate(R.menu.menu_layout,menu);
MenuItem menuItem=menu.findItem(R.id.searchViewID);
SearchView searchView= (SearchView) menuItem.getActionView();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
adapter.getFilter().filter(newText);
return false;
}
});
return super.onCreateOptionsMenu(menu);
}
}
===================================================================================
activity_main.xml
==================
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="#ab223cff" tools:context="com.sam.web.listviewsearch.MainActivity"> android:layout_width="wrap_content" android:layout_height="wrap_content"> android:listSelector="#27e223" android:dividerHeight="2dp" android:divider="#22cbff" android:id="@+id/listViewId" android:layout_width="match_parent" android:layout_height="match_parent">
===============================================================================
string.xml
============
============================================================
sample_view.xml
==================
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> android:padding="5dp" android:gravity="center_horizontal" android:textStyle="bold" android:textSize="20sp" android:text="Hellow there" android:id="@+id/textViewId" android:layout_width="match_parent" android:layout_height="50dp" />
===================================================================
menu_layout.xml
==================
thistimebd Bangladesh Live online newsportal, education, Lifestyle, Health, Photography, gif image etc.
Make your own name or company name website | contact: thistimebd24@gmail.com
Copyright © 2020-2024 News Portal in Bangladesh - THISTIMEBD.COM. ALL Rights Reserved.