Skip to content

Commit

Permalink
Add navigation menu icon, change getters and setters, bug fix
Browse files Browse the repository at this point in the history
Added Navigation menu icon, Changed OnSearchActionListener, fixed
ArrayIndexOutOfBoundsException error, changed getters and setters ...
  • Loading branch information
mancj committed Aug 20, 2016
1 parent 48a38a9 commit acc114b
Show file tree
Hide file tree
Showing 30 changed files with 510 additions and 143 deletions.
21 changes: 12 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ then add SearchBar to your activity:
| hint | Set custom prompt in the search box |
| speechMode | If set to true, microphone icon will display instead of the search icon. |
| maxSuggestionsCount | Specifies the maximum number of search queries stored until the activity is destroyed |
| iconLeft | Change left icon |
| iconRight | Change right icon |
| searchIconDrawable | Set search icon drawable resource |
| navIconDrawable | Set navigation icon drawable resource |
| textColor | Change text color |
| hintColor | Change text hint color |

Expand All @@ -71,8 +71,8 @@ then add SearchBar to your activity:
- `setLastSuggestions(List<String> suggestions)`
- `getLastSuggestions()`
- `setMaxSuggestionCount(int maxQuery)`
- `setIconLeft(int iconLefttResId)`
- `setIconRight(int iconRightResId)`
- `setSearchIcon(int searchIconResId)`
- `setNavigationIcon(int navigationIconResId)`
- `setTextColor(int textColor)`
- `setTextHintColor(int hintColor)`
- `inflateMenu(int menuResource)`
Expand Down Expand Up @@ -116,22 +116,25 @@ protected void onDestroy() {
saveSearchSuggestionToDisk(searchBar.getLastSuggestions());
}

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

//called when user confirms request
@Override
public void onSearchConfirmed(CharSequence text) {
startSearch(text.toString(), true, null, true);
}

//called when microphone icon clicked
@Override
public void onSpeechIconSelected() {
openVoiceRecognizer();
public void onButtonClicked(int buttonCode) {
switch (buttonCode){
case MaterialSearchBar.BUTTON_NAVIGATION:
drawer.openDrawer(Gravity.LEFT);
break;
case MaterialSearchBar.BUTTON_SPEECH:
openVoiceRecognizer();
}
}
```
5 changes: 3 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ android {

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.4.0'
compile project(':library')
compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.android.support:design:23.4.0'
testCompile 'junit:junit:4.12'
}
6 changes: 4 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main"
android:theme="@style/AppTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Expand Down
121 changes: 79 additions & 42 deletions app/src/main/java/com/mancj/example/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,76 +1,113 @@
package com.mancj.example;

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.PopupMenu;
import android.util.Log;
import android.view.MenuItem;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.view.Gravity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;

import com.mancj.materialsearchbar.MaterialSearchBar;

import java.util.List;

public class MainActivity extends AppCompatActivity implements MaterialSearchBar.OnSearchActionListener, PopupMenu.OnMenuItemClickListener {
private List<String> lastSearches;
private MaterialSearchBar searchBar;
private FrameLayout dim;
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener, MaterialSearchBar.OnSearchActionListener {
MaterialSearchBar searchBar;
private DrawerLayout drawer;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

dim = (FrameLayout) findViewById(R.id.dim);

drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
searchBar = (MaterialSearchBar) findViewById(R.id.searchBar);
//enable searchbar callbacks
searchBar.setOnSearchActionListener(this);
searchBar.inflateMenu(R.menu.main);
searchBar.getMenu().setOnMenuItemClickListener(this);
//restore last queries from disk
// lastSearches = loadSearchSuggestionFromDiks();
// searchBar.setLastSuggestions(list);
}

@Override
protected void onDestroy() {
super.onDestroy();
//save last queries to disk
// saveSearchSuggestionToDisk(searchBar.getLastSuggestions());
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}

//called when searchbar enabled or disabled
@Override
public void onSearchStateChanged(boolean enabled) {
String s = enabled ? "enabled" : "disabled";
Toast.makeText(MainActivity.this, "Search " + s, Toast.LENGTH_SHORT).show();
if (enabled){
dim.setVisibility(View.VISIBLE);
}else {
dim.setVisibility(View.GONE);
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}

//called when user confirms request
@SuppressWarnings("StatementWithEmptyBody")
@Override
public void onSearchConfirmed(CharSequence text) {
// startSearch(text.toString(), true, null, true);
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();

if (id == R.id.nav_camera) {
// Handle the camera action
} else if (id == R.id.nav_gallery) {

} else if (id == R.id.nav_slideshow) {

} else if (id == R.id.nav_manage) {

} else if (id == R.id.nav_share) {

} else if (id == R.id.nav_send) {

}

DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}

//called when microphone icon clicked
@Override
public void onSpeechIconSelected() {
// openVoiceRecognizer();
public void onSearchStateChanged(boolean enabled) {

}

@Override
public boolean onMenuItemClick(MenuItem item) {
Log.d("LOG_TAG", getClass().getSimpleName() + ": item clicked " + item.getTitle());
return false;
public void onSearchConfirmed(CharSequence text) {

}

@Override
public void onButtonClicked(int buttonCode) {
switch (buttonCode){
case MaterialSearchBar.BUTTON_NAVIGATION:
drawer.openDrawer(Gravity.LEFT);
break;
case MaterialSearchBar.BUTTON_SPEECH:
}
}
}
12 changes: 12 additions & 0 deletions app/src/main/res/drawable-v21/ic_menu_camera.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportHeight="24.0"
android:viewportWidth="24.0">
<path
android:fillColor="#FF000000"
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" />
<path
android:fillColor="#FF000000"
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" />
</vector>
9 changes: 9 additions & 0 deletions app/src/main/res/drawable-v21/ic_menu_gallery.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportHeight="24.0"
android:viewportWidth="24.0">
<path
android:fillColor="#FF000000"
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" />
</vector>
9 changes: 9 additions & 0 deletions app/src/main/res/drawable-v21/ic_menu_manage.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportHeight="24.0"
android:viewportWidth="24.0">
<path
android:fillColor="#FF000000"
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" />
</vector>
9 changes: 9 additions & 0 deletions app/src/main/res/drawable-v21/ic_menu_send.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportHeight="24.0"
android:viewportWidth="24.0">
<path
android:fillColor="#FF000000"
android:pathData="M2.01,21L23,12 2.01,3 2,10l15,2 -15,2z" />
</vector>
9 changes: 9 additions & 0 deletions app/src/main/res/drawable-v21/ic_menu_share.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportHeight="24.0"
android:viewportWidth="24.0">
<path
android:fillColor="#FF000000"
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" />
</vector>
9 changes: 9 additions & 0 deletions app/src/main/res/drawable-v21/ic_menu_slideshow.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportHeight="24.0"
android:viewportWidth="24.0">
<path
android:fillColor="#FF000000"
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" />
</vector>
9 changes: 9 additions & 0 deletions app/src/main/res/drawable/side_nav_bar.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:angle="135"
android:centerColor="#4CAF50"
android:endColor="#2E7D32"
android:startColor="#81C784"
android:type="linear" />
</shape>
41 changes: 16 additions & 25 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,34 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
<android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimaryDark"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="com.mancj.example.MainActivity">
android:fitsSystemWindows="true"
tools:openDrawer="start">

<RelativeLayout
<include
layout="@layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:background="@drawable/round_corners"
android:layout_marginTop="65dp"
android:visibility="visible"></RelativeLayout>
android:layout_height="match_parent" />

<FrameLayout
android:layout_width="match_parent"
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@color/transparentBlack"
android:visibility="gone"
android:id="@+id/dim"></FrameLayout>

<com.mancj.materialsearchbar.MaterialSearchBar
app:hint="Custom hint"
app:maxSuggestionsCount="10"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/searchBar" />
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer" />

</RelativeLayout>
</android.support.v4.widget.DrawerLayout>
33 changes: 33 additions & 0 deletions app/src/main/res/layout/app_bar_main.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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"
android:fitsSystemWindows="true"
tools:context="com.mancj.example.MainActivity">


<include layout="@layout/content_main" />

<RelativeLayout
android:background="@color/colorPrimary"
android:layout_width="match_parent"
android:layout_height="70dp">

</RelativeLayout>

<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/transparentBlack">

</FrameLayout>

<com.mancj.materialsearchbar.MaterialSearchBar
android:id="@+id/searchBar"
app:navIconEnabled="true"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

</android.support.design.widget.CoordinatorLayout>
Loading

0 comments on commit acc114b

Please sign in to comment.