Created
June 17, 2019 10:03
-
-
Save ahmedbr/4bd841b1c313d8c62bd513c32a66c222 to your computer and use it in GitHub Desktop.
Android - Toolbar with SearchView
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
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" | |
tools:context=".MainActivity"> | |
<androidx.appcompat.widget.Toolbar | |
android:id="@+id/toolbar" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:background="?attr/colorPrimary" | |
android:minHeight="?attr/actionBarSize" | |
app:theme="@style/ThemeOverlay.AppCompat.Dark" /> | |
<ListView | |
android:id="@+id/list_view" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_below="@+id/toolbar" /> | |
<TextView | |
android:id="@+id/empty_text_view" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_centerInParent="true" | |
android:text="No Results" | |
android:textSize="20sp" | |
android:visibility="gone" /> | |
</RelativeLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MainActivity extends AppCompatActivity { | |
ArrayAdapter arrayAdapter; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
Toolbar toolbar = findViewById(R.id.toolbar); | |
setSupportActionBar(toolbar); | |
ListView listView = findViewById(R.id.list_view); | |
TextView emptyTextView = findViewById(R.id.empty_text_view); | |
arrayAdapter = new ArrayAdapter<>(MainActivity.this, | |
android.R.layout.simple_list_item_1, getResources().getStringArray(R.array.months_array)); | |
listView.setAdapter(arrayAdapter); | |
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { | |
@Override | |
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { | |
Toast.makeText(MainActivity.this, arrayAdapter.getItem(i).toString(), | |
Toast.LENGTH_LONG).show(); | |
} | |
}); | |
listView.setEmptyView(emptyTextView); | |
} | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu) { | |
getMenuInflater().inflate(R.menu.menu_list, menu); | |
MenuItem searchItem = menu.findItem(R.id.action_search); | |
SearchView searchView = (SearchView) searchItem.getActionView(); | |
searchView.setQueryHint("Search"); | |
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() { | |
@Override | |
public boolean onQueryTextSubmit(String query) { | |
return false; | |
} | |
@Override | |
public boolean onQueryTextChange(String newText) { | |
arrayAdapter.getFilter().filter(newText); | |
return true; | |
} | |
}); | |
return super.onCreateOptionsMenu(menu); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<resources> | |
<string name="app_name">PreparingComponents</string> | |
<string-array name="months_array"> | |
<item>January</item> | |
<item>February</item> | |
<item>March</item> | |
<item>April</item> | |
<item>May</item> | |
<item>June</item> | |
<item>July</item> | |
<item>August</item> | |
<item>September</item> | |
<item>October</item> | |
<item>November</item> | |
<item>December</item> | |
</string-array> | |
</resources> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<resources> | |
<!-- Base application theme. --> | |
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> | |
<!-- Customize your theme here. --> | |
<item name="colorPrimary">@color/colorPrimary</item> | |
<item name="colorPrimaryDark">@color/colorPrimaryDark</item> | |
<item name="colorAccent">@color/colorAccent</item> | |
</style> | |
</resources> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment