Skip to content

Commit acc114b

Browse files
author
mancj
committed
Add navigation menu icon, change getters and setters, bug fix
Added Navigation menu icon, Changed OnSearchActionListener, fixed ArrayIndexOutOfBoundsException error, changed getters and setters ...
1 parent 48a38a9 commit acc114b

30 files changed

+510
-143
lines changed

README.md

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ then add SearchBar to your activity:
5353
| hint | Set custom prompt in the search box |
5454
| speechMode | If set to true, microphone icon will display instead of the search icon. |
5555
| maxSuggestionsCount | Specifies the maximum number of search queries stored until the activity is destroyed |
56-
| iconLeft | Change left icon |
57-
| iconRight | Change right icon |
56+
| searchIconDrawable | Set search icon drawable resource |
57+
| navIconDrawable | Set navigation icon drawable resource |
5858
| textColor | Change text color |
5959
| hintColor | Change text hint color |
6060

@@ -71,8 +71,8 @@ then add SearchBar to your activity:
7171
- `setLastSuggestions(List<String> suggestions)`
7272
- `getLastSuggestions()`
7373
- `setMaxSuggestionCount(int maxQuery)`
74-
- `setIconLeft(int iconLefttResId)`
75-
- `setIconRight(int iconRightResId)`
74+
- `setSearchIcon(int searchIconResId)`
75+
- `setNavigationIcon(int navigationIconResId)`
7676
- `setTextColor(int textColor)`
7777
- `setTextHintColor(int hintColor)`
7878
- `inflateMenu(int menuResource)`
@@ -116,22 +116,25 @@ protected void onDestroy() {
116116
saveSearchSuggestionToDisk(searchBar.getLastSuggestions());
117117
}
118118

119-
//called when searchbar enabled or disabled
120119
@Override
121120
public void onSearchStateChanged(boolean enabled) {
122121
String s = enabled ? "enabled" : "disabled";
123122
Toast.makeText(MainActivity.this, "Search " + s, Toast.LENGTH_SHORT).show();
124123
}
125124

126-
//called when user confirms request
127125
@Override
128126
public void onSearchConfirmed(CharSequence text) {
129127
startSearch(text.toString(), true, null, true);
130128
}
131129

132-
//called when microphone icon clicked
133130
@Override
134-
public void onSpeechIconSelected() {
135-
openVoiceRecognizer();
131+
public void onButtonClicked(int buttonCode) {
132+
switch (buttonCode){
133+
case MaterialSearchBar.BUTTON_NAVIGATION:
134+
drawer.openDrawer(Gravity.LEFT);
135+
break;
136+
case MaterialSearchBar.BUTTON_SPEECH:
137+
openVoiceRecognizer();
138+
}
136139
}
137140
```

app/build.gradle

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ android {
2121

2222
dependencies {
2323
compile fileTree(include: ['*.jar'], dir: 'libs')
24-
testCompile 'junit:junit:4.12'
25-
compile 'com.android.support:appcompat-v7:23.4.0'
2624
compile project(':library')
25+
compile 'com.android.support:appcompat-v7:23.4.0'
26+
compile 'com.android.support:design:23.4.0'
27+
testCompile 'junit:junit:4.12'
2728
}

app/src/main/AndroidManifest.xml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
88
android:label="@string/app_name"
99
android:supportsRtl="true"
1010
android:theme="@style/AppTheme">
11-
<activity android:name=".MainActivity">
11+
<activity
12+
android:name=".MainActivity"
13+
android:label="@string/title_activity_main"
14+
android:theme="@style/AppTheme">
1215
<intent-filter>
1316
<action android:name="android.intent.action.MAIN" />
14-
1517
<category android:name="android.intent.category.LAUNCHER" />
1618
</intent-filter>
1719
</activity>
Lines changed: 79 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,113 @@
11
package com.mancj.example;
22

3-
import android.graphics.Color;
4-
import android.support.v7.app.AppCompatActivity;
53
import android.os.Bundle;
6-
import android.support.v7.widget.PopupMenu;
7-
import android.util.Log;
8-
import android.view.MenuItem;
4+
import android.support.design.widget.FloatingActionButton;
5+
import android.support.design.widget.Snackbar;
6+
import android.view.Gravity;
97
import android.view.View;
10-
import android.widget.FrameLayout;
11-
import android.widget.RelativeLayout;
12-
import android.widget.Toast;
8+
import android.support.design.widget.NavigationView;
9+
import android.support.v4.view.GravityCompat;
10+
import android.support.v4.widget.DrawerLayout;
11+
import android.support.v7.app.ActionBarDrawerToggle;
12+
import android.support.v7.app.AppCompatActivity;
13+
import android.support.v7.widget.Toolbar;
14+
import android.view.Menu;
15+
import android.view.MenuItem;
1316

1417
import com.mancj.materialsearchbar.MaterialSearchBar;
1518

16-
import java.util.List;
17-
18-
public class MainActivity extends AppCompatActivity implements MaterialSearchBar.OnSearchActionListener, PopupMenu.OnMenuItemClickListener {
19-
private List<String> lastSearches;
20-
private MaterialSearchBar searchBar;
21-
private FrameLayout dim;
19+
public class MainActivity extends AppCompatActivity
20+
implements NavigationView.OnNavigationItemSelectedListener, MaterialSearchBar.OnSearchActionListener {
21+
MaterialSearchBar searchBar;
22+
private DrawerLayout drawer;
2223

2324
@Override
2425
protected void onCreate(Bundle savedInstanceState) {
2526
super.onCreate(savedInstanceState);
2627
setContentView(R.layout.activity_main);
2728

28-
dim = (FrameLayout) findViewById(R.id.dim);
29-
29+
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
30+
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
31+
navigationView.setNavigationItemSelectedListener(this);
3032
searchBar = (MaterialSearchBar) findViewById(R.id.searchBar);
31-
//enable searchbar callbacks
3233
searchBar.setOnSearchActionListener(this);
3334
searchBar.inflateMenu(R.menu.main);
34-
searchBar.getMenu().setOnMenuItemClickListener(this);
35-
//restore last queries from disk
36-
// lastSearches = loadSearchSuggestionFromDiks();
37-
// searchBar.setLastSuggestions(list);
3835
}
3936

4037
@Override
41-
protected void onDestroy() {
42-
super.onDestroy();
43-
//save last queries to disk
44-
// saveSearchSuggestionToDisk(searchBar.getLastSuggestions());
38+
public void onBackPressed() {
39+
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
40+
if (drawer.isDrawerOpen(GravityCompat.START)) {
41+
drawer.closeDrawer(GravityCompat.START);
42+
} else {
43+
super.onBackPressed();
44+
}
4545
}
4646

47-
//called when searchbar enabled or disabled
4847
@Override
49-
public void onSearchStateChanged(boolean enabled) {
50-
String s = enabled ? "enabled" : "disabled";
51-
Toast.makeText(MainActivity.this, "Search " + s, Toast.LENGTH_SHORT).show();
52-
if (enabled){
53-
dim.setVisibility(View.VISIBLE);
54-
}else {
55-
dim.setVisibility(View.GONE);
48+
public boolean onCreateOptionsMenu(Menu menu) {
49+
// Inflate the menu; this adds items to the action bar if it is present.
50+
getMenuInflater().inflate(R.menu.main, menu);
51+
return true;
52+
}
53+
54+
@Override
55+
public boolean onOptionsItemSelected(MenuItem item) {
56+
// Handle action bar item clicks here. The action bar will
57+
// automatically handle clicks on the Home/Up button, so long
58+
// as you specify a parent activity in AndroidManifest.xml.
59+
int id = item.getItemId();
60+
61+
//noinspection SimplifiableIfStatement
62+
if (id == R.id.action_settings) {
63+
return true;
5664
}
65+
66+
return super.onOptionsItemSelected(item);
5767
}
5868

59-
//called when user confirms request
69+
@SuppressWarnings("StatementWithEmptyBody")
6070
@Override
61-
public void onSearchConfirmed(CharSequence text) {
62-
// startSearch(text.toString(), true, null, true);
71+
public boolean onNavigationItemSelected(MenuItem item) {
72+
// Handle navigation view item clicks here.
73+
int id = item.getItemId();
74+
75+
if (id == R.id.nav_camera) {
76+
// Handle the camera action
77+
} else if (id == R.id.nav_gallery) {
78+
79+
} else if (id == R.id.nav_slideshow) {
80+
81+
} else if (id == R.id.nav_manage) {
82+
83+
} else if (id == R.id.nav_share) {
84+
85+
} else if (id == R.id.nav_send) {
86+
87+
}
88+
89+
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
90+
drawer.closeDrawer(GravityCompat.START);
91+
return true;
6392
}
6493

65-
//called when microphone icon clicked
6694
@Override
67-
public void onSpeechIconSelected() {
68-
// openVoiceRecognizer();
95+
public void onSearchStateChanged(boolean enabled) {
96+
6997
}
7098

7199
@Override
72-
public boolean onMenuItemClick(MenuItem item) {
73-
Log.d("LOG_TAG", getClass().getSimpleName() + ": item clicked " + item.getTitle());
74-
return false;
100+
public void onSearchConfirmed(CharSequence text) {
101+
102+
}
103+
104+
@Override
105+
public void onButtonClicked(int buttonCode) {
106+
switch (buttonCode){
107+
case MaterialSearchBar.BUTTON_NAVIGATION:
108+
drawer.openDrawer(Gravity.LEFT);
109+
break;
110+
case MaterialSearchBar.BUTTON_SPEECH:
111+
}
75112
}
76113
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
2+
android:width="24dp"
3+
android:height="24dp"
4+
android:viewportHeight="24.0"
5+
android:viewportWidth="24.0">
6+
<path
7+
android:fillColor="#FF000000"
8+
android:pathData="M12,12m-3.2,0a3.2,3.2 0,1 1,6.4 0a3.2,3.2 0,1 1,-6.4 0" />
9+
<path
10+
android:fillColor="#FF000000"
11+
android:pathData="M9,2L7.17,4H4c-1.1,0 -2,0.9 -2,2v12c0,1.1 0.9,2 2,2h16c1.1,0 2,-0.9 2,-2V6c0,-1.1 -0.9,-2 -2,-2h-3.17L15,2H9zm3,15c-2.76,0 -5,-2.24 -5,-5s2.24,-5 5,-5 5,2.24 5,5 -2.24,5 -5,5z" />
12+
</vector>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
2+
android:width="24dp"
3+
android:height="24dp"
4+
android:viewportHeight="24.0"
5+
android:viewportWidth="24.0">
6+
<path
7+
android:fillColor="#FF000000"
8+
android:pathData="M22,16V4c0,-1.1 -0.9,-2 -2,-2H8c-1.1,0 -2,0.9 -2,2v12c0,1.1 0.9,2 2,2h12c1.1,0 2,-0.9 2,-2zm-11,-4l2.03,2.71L16,11l4,5H8l3,-4zM2,6v14c0,1.1 0.9,2 2,2h14v-2H4V6H2z" />
9+
</vector>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
2+
android:width="24dp"
3+
android:height="24dp"
4+
android:viewportHeight="24.0"
5+
android:viewportWidth="24.0">
6+
<path
7+
android:fillColor="#FF000000"
8+
android:pathData="M22.7,19l-9.1,-9.1c0.9,-2.3 0.4,-5 -1.5,-6.9 -2,-2 -5,-2.4 -7.4,-1.3L9,6 6,9 1.6,4.7C0.4,7.1 0.9,10.1 2.9,12.1c1.9,1.9 4.6,2.4 6.9,1.5l9.1,9.1c0.4,0.4 1,0.4 1.4,0l2.3,-2.3c0.5,-0.4 0.5,-1.1 0.1,-1.4z" />
9+
</vector>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
2+
android:width="24dp"
3+
android:height="24dp"
4+
android:viewportHeight="24.0"
5+
android:viewportWidth="24.0">
6+
<path
7+
android:fillColor="#FF000000"
8+
android:pathData="M2.01,21L23,12 2.01,3 2,10l15,2 -15,2z" />
9+
</vector>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
2+
android:width="24dp"
3+
android:height="24dp"
4+
android:viewportHeight="24.0"
5+
android:viewportWidth="24.0">
6+
<path
7+
android:fillColor="#FF000000"
8+
android:pathData="M18,16.08c-0.76,0 -1.44,0.3 -1.96,0.77L8.91,12.7c0.05,-0.23 0.09,-0.46 0.09,-0.7s-0.04,-0.47 -0.09,-0.7l7.05,-4.11c0.54,0.5 1.25,0.81 2.04,0.81 1.66,0 3,-1.34 3,-3s-1.34,-3 -3,-3 -3,1.34 -3,3c0,0.24 0.04,0.47 0.09,0.7L8.04,9.81C7.5,9.31 6.79,9 6,9c-1.66,0 -3,1.34 -3,3s1.34,3 3,3c0.79,0 1.5,-0.31 2.04,-0.81l7.12,4.16c-0.05,0.21 -0.08,0.43 -0.08,0.65 0,1.61 1.31,2.92 2.92,2.92 1.61,0 2.92,-1.31 2.92,-2.92s-1.31,-2.92 -2.92,-2.92z" />
9+
</vector>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
2+
android:width="24dp"
3+
android:height="24dp"
4+
android:viewportHeight="24.0"
5+
android:viewportWidth="24.0">
6+
<path
7+
android:fillColor="#FF000000"
8+
android:pathData="M4,6H2v14c0,1.1 0.9,2 2,2h14v-2H4V6zm16,-4H8c-1.1,0 -2,0.9 -2,2v12c0,1.1 0.9,2 2,2h12c1.1,0 2,-0.9 2,-2V4c0,-1.1 -0.9,-2 -2,-2zm-8,12.5v-9l6,4.5 -6,4.5z" />
9+
</vector>

0 commit comments

Comments
 (0)