0
# Navigation Components
1
2
Components for app navigation including bottom navigation, navigation drawer, tabs, navigation rail, and app bars.
3
4
## Core Imports
5
6
```java
7
import com.google.android.material.navigation.NavigationView;
8
import com.google.android.material.bottomnavigation.BottomNavigationView;
9
import com.google.android.material.navigationrail.NavigationRailView;
10
import com.google.android.material.tabs.TabLayout;
11
import com.google.android.material.tabs.TabLayoutMediator;
12
import com.google.android.material.appbar.AppBarLayout;
13
import com.google.android.material.appbar.CollapsingToolbarLayout;
14
import com.google.android.material.appbar.MaterialToolbar;
15
import com.google.android.material.bottomappbar.BottomAppBar;
16
import com.google.android.material.search.SearchBar;
17
import com.google.android.material.search.SearchView;
18
```
19
20
## Bottom Navigation View
21
22
Bottom navigation bar for switching between top-level views.
23
24
```java { .api }
25
class BottomNavigationView extends NavigationBarView {
26
BottomNavigationView(Context context);
27
BottomNavigationView(Context context, AttributeSet attrs);
28
29
// Selection listeners (deprecated - use NavigationBarView methods)
30
void setOnNavigationItemSelectedListener(OnNavigationItemSelectedListener listener);
31
void setOnNavigationItemReselectedListener(OnNavigationItemReselectedListener listener);
32
33
// Menu and selection
34
Menu getMenu();
35
void setSelectedItemId(@IdRes int itemId);
36
int getSelectedItemId();
37
38
// Badges
39
void setBadgeFor(@IdRes int menuItemId, BadgeDrawable badgeDrawable);
40
BadgeDrawable getBadgeFor(@IdRes int menuItemId);
41
void removeBadge(@IdRes int menuItemId);
42
43
// Styling
44
void setItemIconTintList(ColorStateList tint);
45
ColorStateList getItemIconTintList();
46
void setItemTextColor(ColorStateList textColor);
47
ColorStateList getItemTextColor();
48
void setItemBackgroundResource(@DrawableRes int resId);
49
void setItemBackground(Drawable background);
50
Drawable getItemBackground();
51
52
// Label visibility
53
void setLabelVisibilityMode(int mode);
54
int getLabelVisibilityMode();
55
}
56
57
interface BottomNavigationView.OnNavigationItemSelectedListener {
58
boolean onNavigationItemSelected(@NonNull MenuItem item);
59
}
60
61
interface BottomNavigationView.OnNavigationItemReselectedListener {
62
void onNavigationItemReselected(@NonNull MenuItem item);
63
}
64
```
65
66
### Usage Example
67
68
```java
69
BottomNavigationView bottomNav = findViewById(R.id.bottom_navigation);
70
71
// Handle navigation selection
72
bottomNav.setOnItemSelectedListener(item -> {
73
switch (item.getItemId()) {
74
case R.id.nav_home:
75
// Navigate to home
76
return true;
77
case R.id.nav_search:
78
// Navigate to search
79
return true;
80
case R.id.nav_profile:
81
// Navigate to profile
82
return true;
83
default:
84
return false;
85
}
86
});
87
88
// Configure badges
89
BadgeDrawable badge = bottomNav.getOrCreateBadge(R.id.nav_notifications);
90
badge.setNumber(5);
91
badge.setVisible(true);
92
93
// Set selected item
94
bottomNav.setSelectedItemId(R.id.nav_home);
95
```
96
97
## Navigation View
98
99
Navigation drawer for hierarchical navigation and app-level options.
100
101
```java { .api }
102
class NavigationView extends ScrimInsetsFrameLayout {
103
NavigationView(Context context);
104
NavigationView(Context context, AttributeSet attrs);
105
106
// Selection handling
107
void setNavigationItemSelectedListener(OnNavigationItemSelectedListener listener);
108
109
// Menu management
110
Menu getMenu();
111
void inflateMenu(@MenuRes int resId);
112
113
// Header views
114
View getHeaderView(int index);
115
int getHeaderCount();
116
View inflateHeaderView(@LayoutRes int res);
117
void addHeaderView(View view);
118
void removeHeaderView(View view);
119
120
// Selection state
121
void setCheckedItem(@IdRes int id);
122
void setCheckedItem(MenuItem menuItem);
123
MenuItem getCheckedItem();
124
125
// Item styling
126
void setItemIconTintList(ColorStateList tint);
127
ColorStateList getItemIconTintList();
128
void setItemTextColor(ColorStateList textColor);
129
ColorStateList getItemTextColor();
130
void setItemBackground(Drawable itemBackground);
131
Drawable getItemBackground();
132
void setItemBackgroundResource(@DrawableRes int resId);
133
134
// Item layout
135
void setItemHorizontalPadding(@Dimension int padding);
136
int getItemHorizontalPadding();
137
void setItemHorizontalPaddingResource(@DimenRes int paddingResource);
138
void setItemIconPadding(@Dimension int padding);
139
int getItemIconPadding();
140
void setItemIconPaddingResource(@DimenRes int paddingResource);
141
void setItemMaxLines(int itemMaxLines);
142
int getItemMaxLines();
143
}
144
145
interface NavigationView.OnNavigationItemSelectedListener {
146
boolean onNavigationItemSelected(@NonNull MenuItem menuItem);
147
}
148
```
149
150
### Usage Example
151
152
```java
153
NavigationView navigationView = findViewById(R.id.nav_view);
154
DrawerLayout drawer = findViewById(R.id.drawer_layout);
155
156
// Handle navigation selection
157
navigationView.setNavigationItemSelectedListener(menuItem -> {
158
switch (menuItem.getItemId()) {
159
case R.id.nav_dashboard:
160
// Navigate to dashboard
161
break;
162
case R.id.nav_settings:
163
// Navigate to settings
164
break;
165
case R.id.nav_logout:
166
// Handle logout
167
break;
168
}
169
170
// Close drawer
171
drawer.closeDrawer(GravityCompat.START);
172
return true;
173
});
174
175
// Setup header
176
View headerView = navigationView.getHeaderView(0);
177
TextView userName = headerView.findViewById(R.id.user_name);
178
userName.setText("John Doe");
179
180
// Set selected item
181
navigationView.setCheckedItem(R.id.nav_dashboard);
182
```
183
184
## Navigation Rail View
185
186
Vertical navigation rail for tablet and desktop layouts.
187
188
```java { .api }
189
class NavigationRailView extends NavigationBarView {
190
NavigationRailView(Context context);
191
NavigationRailView(Context context, AttributeSet attrs);
192
193
// Header view
194
void setHeaderView(View headerView);
195
View getHeaderView();
196
void addHeaderView(View headerView);
197
void removeHeaderView();
198
199
// Menu positioning
200
int getMenuGravity();
201
void setMenuGravity(int gravity);
202
203
// Item size
204
int getItemMinimumHeight();
205
void setItemMinimumHeight(@Dimension int minHeight);
206
}
207
```
208
209
### Navigation Rail Gravity Constants
210
211
```java { .api }
212
public static final int GRAVITY_TOP = 48;
213
public static final int GRAVITY_CENTER = 17;
214
public static final int GRAVITY_BOTTOM = 80;
215
```
216
217
### Usage Example
218
219
```java
220
NavigationRailView navigationRail = findViewById(R.id.navigation_rail);
221
222
// Add header (like FAB)
223
FloatingActionButton fab = new FloatingActionButton(this);
224
fab.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.ic_add));
225
navigationRail.setHeaderView(fab);
226
227
// Configure menu positioning
228
navigationRail.setMenuGravity(NavigationRailView.GRAVITY_CENTER);
229
230
// Handle selection
231
navigationRail.setOnItemSelectedListener(item -> {
232
// Handle navigation selection
233
return true;
234
});
235
```
236
237
## Tab Layout
238
239
Horizontal tabs for organizing content into logical groups.
240
241
```java { .api }
242
class TabLayout extends HorizontalScrollView {
243
TabLayout(Context context);
244
TabLayout(Context context, AttributeSet attrs);
245
246
// Tab management
247
Tab newTab();
248
void addTab(Tab tab);
249
void addTab(Tab tab, boolean setSelected);
250
void addTab(Tab tab, int position);
251
void addTab(Tab tab, int position, boolean setSelected);
252
void removeTab(Tab tab);
253
void removeTabAt(int position);
254
void removeAllTabs();
255
256
// Tab access
257
int getTabCount();
258
Tab getTabAt(int index);
259
int getSelectedTabPosition();
260
261
// Selection
262
void selectTab(Tab tab);
263
void selectTab(Tab tab, boolean updateIndicator);
264
265
// Selection listeners
266
void addOnTabSelectedListener(OnTabSelectedListener listener);
267
void removeOnTabSelectedListener(OnTabSelectedListener listener);
268
void clearOnTabSelectedListeners();
269
270
// Layout behavior
271
void setTabMode(int mode);
272
int getTabMode();
273
void setTabGravity(int gravity);
274
int getTabGravity();
275
276
// Styling
277
void setSelectedTabIndicatorColor(@ColorInt int color);
278
void setSelectedTabIndicatorHeight(int height);
279
void setTabTextColors(@ColorInt int normalColor, @ColorInt int selectedColor);
280
void setTabTextColors(ColorStateList textColor);
281
ColorStateList getTabTextColors();
282
void setTabIconTint(ColorStateList iconTint);
283
void setTabIconTintResource(@ColorRes int iconTintResourceId);
284
ColorStateList getTabIconTint();
285
void setTabRippleColor(ColorStateList color);
286
void setTabRippleColorResource(@ColorRes int tabRippleColorResourceId);
287
ColorStateList getTabRippleColor();
288
void setUnboundedRipple(boolean unboundedRipple);
289
boolean getUnboundedRipple();
290
}
291
292
class TabLayout.Tab {
293
CharSequence getText();
294
Tab setText(CharSequence text);
295
Tab setText(@StringRes int resId);
296
Drawable getIcon();
297
Tab setIcon(Drawable icon);
298
Tab setIcon(@DrawableRes int resId);
299
int getPosition();
300
void select();
301
boolean isSelected();
302
Tab setTag(Object tag);
303
Object getTag();
304
Tab setCustomView(View view);
305
Tab setCustomView(@LayoutRes int layoutResId);
306
View getCustomView();
307
BadgeDrawable getOrCreateBadge();
308
BadgeDrawable getBadge();
309
void removeBadge();
310
}
311
312
interface TabLayout.OnTabSelectedListener {
313
void onTabSelected(TabLayout.Tab tab);
314
void onTabUnselected(TabLayout.Tab tab);
315
void onTabReselected(TabLayout.Tab tab);
316
}
317
```
318
319
### Tab Layout Constants
320
321
```java { .api }
322
public static final int MODE_SCROLLABLE = 0;
323
public static final int MODE_FIXED = 1;
324
public static final int MODE_AUTO = 2;
325
326
public static final int GRAVITY_FILL = 0;
327
public static final int GRAVITY_CENTER = 1;
328
public static final int GRAVITY_START = 8388611;
329
```
330
331
### Usage Example
332
333
```java
334
TabLayout tabLayout = findViewById(R.id.tab_layout);
335
ViewPager2 viewPager = findViewById(R.id.view_pager);
336
337
// Setup tabs manually
338
String[] tabTitles = {"Home", "Search", "Profile"};
339
for (String title : tabTitles) {
340
TabLayout.Tab tab = tabLayout.newTab();
341
tab.setText(title);
342
tabLayout.addTab(tab);
343
}
344
345
// Configure appearance
346
tabLayout.setTabMode(TabLayout.MODE_FIXED);
347
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
348
349
// Handle tab selection
350
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
351
@Override
352
public void onTabSelected(TabLayout.Tab tab) {
353
// Handle tab selection
354
viewPager.setCurrentItem(tab.getPosition());
355
}
356
357
@Override
358
public void onTabUnselected(TabLayout.Tab tab) {}
359
360
@Override
361
public void onTabReselected(TabLayout.Tab tab) {}
362
});
363
```
364
365
## Tab Layout Mediator
366
367
Links TabLayout with ViewPager2 for synchronized tab and page navigation.
368
369
```java { .api }
370
class TabLayoutMediator {
371
TabLayoutMediator(TabLayout tabLayout, ViewPager2 viewPager, TabConfigurationStrategy tabConfigurationStrategy);
372
TabLayoutMediator(TabLayout tabLayout, ViewPager2 viewPager, boolean autoRefresh, TabConfigurationStrategy tabConfigurationStrategy);
373
374
// Lifecycle
375
void attach();
376
void detach();
377
boolean isAttached();
378
}
379
380
interface TabLayoutMediator.TabConfigurationStrategy {
381
void onConfigureTab(@NonNull TabLayout.Tab tab, int position);
382
}
383
```
384
385
### Usage Example
386
387
```java
388
TabLayout tabLayout = findViewById(R.id.tab_layout);
389
ViewPager2 viewPager = findViewById(R.id.view_pager);
390
391
// Setup ViewPager with adapter
392
MyPagerAdapter adapter = new MyPagerAdapter(this);
393
viewPager.setAdapter(adapter);
394
395
// Link TabLayout with ViewPager2
396
new TabLayoutMediator(tabLayout, viewPager, (tab, position) -> {
397
switch (position) {
398
case 0:
399
tab.setText("Home");
400
tab.setIcon(R.drawable.ic_home);
401
break;
402
case 1:
403
tab.setText("Search");
404
tab.setIcon(R.drawable.ic_search);
405
break;
406
case 2:
407
tab.setText("Profile");
408
tab.setIcon(R.drawable.ic_person);
409
break;
410
}
411
}).attach();
412
```
413
414
## App Bar Layout
415
416
Vertical LinearLayout implementing scrolling behaviors for Material app bars.
417
418
```java { .api }
419
class AppBarLayout extends LinearLayout {
420
AppBarLayout(Context context);
421
AppBarLayout(Context context, AttributeSet attrs);
422
423
// Offset listeners
424
void addOnOffsetChangedListener(OnOffsetChangedListener listener);
425
void removeOnOffsetChangedListener(OnOffsetChangedListener listener);
426
427
// Scroll behavior
428
int getTotalScrollRange();
429
void setExpanded(boolean expanded);
430
void setExpanded(boolean expanded, boolean animate);
431
432
// Lift state
433
boolean isLifted();
434
void setLifted(boolean lifted);
435
void setLiftOnScroll(boolean liftOnScroll);
436
boolean isLiftOnScroll();
437
}
438
439
interface AppBarLayout.OnOffsetChangedListener {
440
void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset);
441
}
442
443
class AppBarLayout.LayoutParams extends LinearLayout.LayoutParams {
444
AppBarLayout.LayoutParams(Context c, AttributeSet attrs);
445
AppBarLayout.LayoutParams(int width, int height);
446
AppBarLayout.LayoutParams(int width, int height, float weight);
447
AppBarLayout.LayoutParams(ViewGroup.LayoutParams p);
448
AppBarLayout.LayoutParams(MarginLayoutParams source);
449
AppBarLayout.LayoutParams(LinearLayout.LayoutParams source);
450
451
void setScrollFlags(int flags);
452
int getScrollFlags();
453
void setScrollInterpolator(Interpolator interpolator);
454
Interpolator getScrollInterpolator();
455
}
456
```
457
458
### Scroll Flags Constants
459
460
```java { .api }
461
public static final int SCROLL_FLAG_SCROLL = 0x1;
462
public static final int SCROLL_FLAG_EXIT_UNTIL_COLLAPSED = 0x2;
463
public static final int SCROLL_FLAG_ENTER_ALWAYS = 0x4;
464
public static final int SCROLL_FLAG_ENTER_ALWAYS_COLLAPSED = 0x8;
465
public static final int SCROLL_FLAG_SNAP = 0x10;
466
public static final int SCROLL_FLAG_SNAP_MARGINS = 0x20;
467
```
468
469
### Usage Example
470
471
```java
472
AppBarLayout appBarLayout = findViewById(R.id.app_bar_layout);
473
474
// Listen for scroll changes
475
appBarLayout.addOnOffsetChangedListener((appBarLayout1, verticalOffset) -> {
476
float alpha = 1.0f - Math.abs(verticalOffset) / (float) appBarLayout1.getTotalScrollRange();
477
// Update UI based on scroll offset
478
});
479
480
// Control expanded state
481
appBarLayout.setExpanded(true, true);
482
483
// Configure lift on scroll
484
appBarLayout.setLiftOnScroll(true);
485
```
486
487
## Collapsing Toolbar Layout
488
489
Wrapper for Toolbar implementing a collapsing app bar effect.
490
491
```java { .api }
492
class CollapsingToolbarLayout extends FrameLayout {
493
CollapsingToolbarLayout(Context context);
494
CollapsingToolbarLayout(Context context, AttributeSet attrs);
495
496
// Title
497
void setTitle(CharSequence title);
498
CharSequence getTitle();
499
500
// Scrim drawables
501
void setContentScrim(Drawable drawable);
502
Drawable getContentScrim();
503
void setStatusBarScrim(Drawable drawable);
504
Drawable getStatusBarScrim();
505
506
// Title text appearance
507
void setCollapsedTitleTextAppearance(@StyleRes int resId);
508
void setExpandedTitleTextAppearance(@StyleRes int resId);
509
510
// Title colors
511
void setCollapsedTitleTextColor(@ColorInt int color);
512
void setExpandedTitleTextColor(@ColorInt int color);
513
void setCollapsedTitleTextColor(ColorStateList colors);
514
void setExpandedTitleTextColor(ColorStateList colors);
515
516
// Title margins
517
void setExpandedTitleMargin(int start, int top, int end, int bottom);
518
void setExpandedTitleMarginStart(int margin);
519
int getExpandedTitleMarginStart();
520
void setExpandedTitleMarginTop(int margin);
521
int getExpandedTitleMarginTop();
522
void setExpandedTitleMarginEnd(int margin);
523
int getExpandedTitleMarginEnd();
524
void setExpandedTitleMarginBottom(int margin);
525
int getExpandedTitleMarginBottom();
526
527
// Parallax
528
void setTitleCollapseMode(int titleCollapseMode);
529
int getTitleCollapseMode();
530
}
531
```
532
533
### Usage Example
534
535
```java
536
CollapsingToolbarLayout collapsingToolbar = findViewById(R.id.collapsing_toolbar);
537
Toolbar toolbar = findViewById(R.id.toolbar);
538
539
// Setup with ActionBar
540
setSupportActionBar(toolbar);
541
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
542
543
// Configure collapsing toolbar
544
collapsingToolbar.setTitle("Article Title");
545
collapsingToolbar.setContentScrim(ContextCompat.getDrawable(this, R.color.primary));
546
collapsingToolbar.setStatusBarScrim(ContextCompat.getDrawable(this, R.color.primary_dark));
547
```
548
549
## Material Toolbar
550
551
Material Design themed toolbar with enhanced styling support.
552
553
```java { .api }
554
class MaterialToolbar extends Toolbar {
555
MaterialToolbar(Context context);
556
MaterialToolbar(Context context, AttributeSet attrs);
557
558
// Navigation icon
559
void setNavigationIcon(Drawable drawable);
560
void setNavigationIconTint(@ColorInt int color);
561
Integer getNavigationIconTint();
562
}
563
```
564
565
### Usage Example
566
567
```java
568
MaterialToolbar toolbar = findViewById(R.id.toolbar);
569
570
// Setup as ActionBar
571
setSupportActionBar(toolbar);
572
573
// Configure navigation
574
toolbar.setNavigationIcon(R.drawable.ic_menu);
575
toolbar.setNavigationIconTint(Color.WHITE);
576
toolbar.setNavigationOnClickListener(v -> {
577
// Handle navigation click (e.g., open drawer)
578
});
579
580
// Set title and subtitle
581
toolbar.setTitle("App Name");
582
toolbar.setSubtitle("Current Section");
583
```
584
585
## Bottom App Bar
586
587
Material Design bottom app bar with FAB integration.
588
589
```java { .api }
590
class BottomAppBar extends Toolbar {
591
BottomAppBar(Context context);
592
BottomAppBar(Context context, AttributeSet attrs);
593
594
// FAB alignment
595
void setFabAlignmentMode(int mode);
596
int getFabAlignmentMode();
597
598
// FAB animation
599
void setFabAnimationMode(int mode);
600
int getFabAnimationMode();
601
602
// Hide on scroll
603
void setHideOnScroll(boolean hideOnScroll);
604
boolean getHideOnScroll();
605
606
// FAB cradle configuration
607
void setCradleVerticalOffset(float verticalOffset);
608
float getCradleVerticalOffset();
609
void setFabCradleMargin(float margin);
610
float getFabCradleMargin();
611
void setFabCradleRoundedCornerRadius(float radius);
612
float getFabCradleRoundedCornerRadius();
613
}
614
```
615
616
### Bottom App Bar Constants
617
618
```java { .api }
619
public static final int FAB_ALIGNMENT_MODE_CENTER = 0;
620
public static final int FAB_ALIGNMENT_MODE_END = 1;
621
622
public static final int FAB_ANIMATION_MODE_SCALE = 0;
623
public static final int FAB_ANIMATION_MODE_SLIDE = 1;
624
```
625
626
### Usage Example
627
628
```java
629
BottomAppBar bottomAppBar = findViewById(R.id.bottom_app_bar);
630
FloatingActionButton fab = findViewById(R.id.fab);
631
632
// Configure FAB alignment
633
bottomAppBar.setFabAlignmentMode(BottomAppBar.FAB_ALIGNMENT_MODE_CENTER);
634
bottomAppBar.setFabAnimationMode(BottomAppBar.FAB_ANIMATION_MODE_SLIDE);
635
636
// Configure cradle
637
bottomAppBar.setFabCradleMargin(16f);
638
bottomAppBar.setFabCradleRoundedCornerRadius(8f);
639
640
// Handle menu clicks
641
bottomAppBar.setOnMenuItemClickListener(item -> {
642
switch (item.getItemId()) {
643
case R.id.menu_search:
644
// Handle search
645
return true;
646
case R.id.menu_more:
647
// Handle more options
648
return true;
649
default:
650
return false;
651
}
652
});
653
654
// Animate FAB alignment
655
bottomAppBar.setFabAlignmentMode(BottomAppBar.FAB_ALIGNMENT_MODE_END);
656
```
657
658
## Navigation Bar View (Base Class)
659
660
Base class for bottom navigation and navigation rail with common functionality.
661
662
```java { .api }
663
abstract class NavigationBarView extends FrameLayout {
664
// Item selection
665
void setOnItemSelectedListener(OnItemSelectedListener listener);
666
void setOnItemReselectedListener(OnItemReselectedListener listener);
667
668
// Menu and selection
669
Menu getMenu();
670
void setSelectedItemId(@IdRes int itemId);
671
int getSelectedItemId();
672
673
// Badges
674
void setBadgeFor(@IdRes int menuItemId, BadgeDrawable badgeDrawable);
675
BadgeDrawable getOrCreateBadge(@IdRes int menuItemId);
676
BadgeDrawable getBadge(@IdRes int menuItemId);
677
void removeBadge(@IdRes int menuItemId);
678
679
// Item styling
680
void setItemIconTintList(ColorStateList tint);
681
ColorStateList getItemIconTintList();
682
void setItemTextColor(ColorStateList textColor);
683
ColorStateList getItemTextColor();
684
void setItemIconSize(@Dimension int size);
685
int getItemIconSize();
686
void setItemIconSizeRes(@DimenRes int sizeRes);
687
void setItemTextAppearanceInactive(@StyleRes int textAppearanceRes);
688
int getItemTextAppearanceInactive();
689
void setItemTextAppearanceActive(@StyleRes int textAppearanceRes);
690
int getItemTextAppearanceActive();
691
void setItemBackgroundResource(@DrawableRes int backgroundRes);
692
void setItemBackground(Drawable background);
693
Drawable getItemBackground();
694
695
// Label visibility
696
void setLabelVisibilityMode(int mode);
697
int getLabelVisibilityMode();
698
}
699
700
interface NavigationBarView.OnItemSelectedListener {
701
boolean onNavigationItemSelected(@NonNull MenuItem item);
702
}
703
704
interface NavigationBarView.OnItemReselectedListener {
705
void onNavigationItemReselected(@NonNull MenuItem item);
706
}
707
```
708
709
### Label Visibility Constants
710
711
```java { .api }
712
public static final int LABEL_VISIBILITY_AUTO = -1;
713
public static final int LABEL_VISIBILITY_SELECTED = 0;
714
public static final int LABEL_VISIBILITY_LABELED = 1;
715
public static final int LABEL_VISIBILITY_UNLABELED = 2;
716
```
717
718
## Search Components
719
720
### Search Bar
721
722
A floating search field with affordances for search and navigation functionality.
723
724
```java { .api }
725
public class SearchBar extends Toolbar {
726
public SearchBar(Context context);
727
public SearchBar(Context context, AttributeSet attrs);
728
729
// Text content
730
public void setText(@Nullable CharSequence text);
731
public void setText(@StringRes int textResId);
732
public CharSequence getText();
733
public void clearText();
734
735
// Hint text
736
public void setHint(@Nullable CharSequence hint);
737
public void setHint(@StringRes int hintResId);
738
public CharSequence getHint();
739
740
// Text alignment
741
public void setTextCentered(boolean textCentered);
742
public boolean getTextCentered();
743
744
// Navigation
745
public void setNavigationOnClickListener(OnClickListener listener);
746
public void setNavigationIcon(@Nullable Drawable navigationIcon);
747
public void setNavigationIcon(@DrawableRes int navigationIconRes);
748
749
// Menu inflation
750
public void inflateMenu(@MenuRes int resId);
751
752
// Lift on scroll behavior
753
public void setLiftOnScroll(boolean liftOnScroll);
754
public boolean isLiftOnScroll();
755
756
// Center view
757
public void setCenterView(@Nullable View view);
758
public View getCenterView();
759
}
760
```
761
762
### Search View
763
764
A full-screen search interface that works together with SearchBar for rich search experiences.
765
766
```java { .api }
767
public class SearchView extends FrameLayout {
768
public SearchView(Context context);
769
public SearchView(Context context, AttributeSet attrs);
770
771
// Search bar setup
772
public void setupWithSearchBar(@Nullable SearchBar searchBar);
773
774
// Text content
775
public void setText(@Nullable CharSequence text);
776
public void setText(@StringRes int textResId);
777
public CharSequence getText();
778
public void clearText();
779
780
// Hint text
781
public void setHint(@Nullable CharSequence hint);
782
public void setHint(@StringRes int hintResId);
783
public CharSequence getHint();
784
785
// Visibility and animation
786
public void show();
787
public void hide();
788
public boolean isShowing();
789
790
// Status bar color
791
public void setStatusBarSpacerColor(@ColorInt int color);
792
public int getStatusBarSpacerColor();
793
public void setStatusBarSpacerEnabled(boolean enabled);
794
public boolean isStatusBarSpacerEnabled();
795
796
// Menu setup
797
public void inflateMenu(@MenuRes int menuResId);
798
public void setOnMenuItemClickListener(@Nullable OnMenuItemClickListener listener);
799
800
// Listeners
801
public void addTransitionListener(@NonNull TransitionListener transitionListener);
802
public void removeTransitionListener(@NonNull TransitionListener transitionListener);
803
804
public interface TransitionListener {
805
void onStateChanged(@NonNull SearchView searchView, @NonNull TransitionState previousState, @NonNull TransitionState newState);
806
}
807
808
public enum TransitionState {
809
HIDING, HIDDEN, SHOWING, SHOWN
810
}
811
}
812
```
813
814
**Usage Example:**
815
816
```java
817
// Setup SearchBar
818
SearchBar searchBar = findViewById(R.id.search_bar);
819
searchBar.setHint("Search products...");
820
searchBar.setNavigationOnClickListener(v -> {
821
// Handle navigation click
822
});
823
824
// Setup SearchView
825
SearchView searchView = findViewById(R.id.search_view);
826
searchView.setupWithSearchBar(searchBar);
827
searchView.inflateMenu(R.menu.search_menu);
828
829
// Add search functionality
830
EditText searchEditText = searchView.findViewById(R.id.search_edit_text);
831
searchEditText.addTextChangedListener(new TextWatcher() {
832
@Override
833
public void onTextChanged(CharSequence s, int start, int before, int count) {
834
performSearch(s.toString());
835
}
836
// ... other TextWatcher methods
837
});
838
```