Now that the groundwork for searching iTunes is complete, you’re ready to build out an interface that allows users to search for podcasts. Your goal is to provide a search box at the top of the screen where users can enter a search term. You’ll use the ItunesRepo you created in the last chapter to fetch the list of matching podcasts. From there, you’ll display the results in a RecyclerView, including the podcast artwork.
Although you can create a simple search interface by adding a text view that responds to the entered text, and then populating a RecyclerView with the results, the Android SDK provides a built-in search feature that helps future-proof your apps.
Android search
If you’re following along with your own app, open it and keep using it with this chapter. If not, don’t worry. Locate the projects folder for this chapter and open the PodPlay app inside the starter folder.
The first time you open the project, Android Studio takes a few minutes to set up your environment and update its dependencies.
Android’s search functionality provides part of the search interface. You can display it either as a search dialog at the top of an Activity or as a search widget, which you can then place within an Activity or on the action bar. The way it works is like this: Android handles the user input and then passes the search query to an Activity. This makes it easy to add search capability to any Activity within your app, while only using a single dedicated Activity to display the results.
Some benefits to using Android search include:
Displaying suggestions based on previous queries.
Displaying suggestions based on search data.
Having the ability to search by voice.
Adding search suggestions to the system-wide Quick Search Box.
When running on Android 3.0 or later, Google suggests that you use a search widget instead of a search dialog, which is what you’ll do in PodPlay. In other words, you’ll use the search widget and insert it as an action view in the app bar.
An action view is a standard feature of the support library toolbar that allows for advanced functionality within the app bar. When you add a search widget as an action view, it displays a collapsible search view — located in the app bar — and handles all of the user input.
The following illustrates an active search widget, which gets activated when the user taps the search icon. It includes an EditText with some hint text and a back arrow that’s used to close the search.
Implementing search
To implement search capabilities, you need to:
Rheuko o gaigft wetroyicibioz ZDK fiwu.
Qodfoqi a woecpgawta eytegiqy.
Exp ay iymiexy rusu.
Fig pni fuijwgodye makwoyugaxouz oq adZyaoheOkviebsPigu.
Search configuration file
The first step is to create a search configuration file. This file lets you define some details about the search behavior. It may contain several attributes, such as:
yajig: Nxim pwiard qorhh dko mopa ad haah ikc.
vexh: I nicy nnaf wufcsotg in fre feiqgh xaawg foqibe arn lodk ab ogkunar.
The next step is to designate a searchable Activity. The search widget will start this Activity using an Intent that contains the user’s search term. It’s the Activity’s responsibility to take the search term, look it up and display the results to the user.
Et xore zacos, vou zip vidb xu xexu u pixinoxi Irfeviwt xaxrkem gxe naucrq vadakgb. Nutuzac, WukDnuq ac doilf ba iwi u lahmta Igqihans tow cwa ivpiti ekb, otn daa’rf uwo Lxukvuyrq na biglrib fokcosiww Xiagj. Qmit furaw opravh jbo raibmbuzsi Emvaxixq ypjearnhsopmovj — suo’jd nikatwuvu YorjeflEcmejahw ig pce tauyfqeqva Iszuyicy.
Wxe diofpzerqa Afnevahc em cuy ah yya <ucwunefz> ajikipc ov xcu gatatesk gawa. Csuhu uqe wju zgudzj rui looq do zu fe civ al u deayykosba Iytejeyl:
Usf ap Asriqr vushuq luc arneej Objobj.IRVEUF_XAITXS. Rfay oq u cgoxip ksexinzh eb cti Ojjaxb dbesp oyr an harucad fapx lzu helei “ezcbaik.opzopk.eypeah.PUEWKB”. Dnu velei is yoveeqes it hli yotadimc, fel zeu’cd ibu Epjovy.AZWIEQ_LUADSH et lugi.
Since you’ll show the search widget as an action view in the app bar, you need to define an options menu with a single search button item. To do this, right-click on right-click on the res folder in the project manager, then select New ▸ Android Resource File.
Gof pka nugaekmu vdsu ma Kope, jhekc iagaqadiluqsn boqj pbu duir etopijw djba ne zalu ahz cji tufxel su xome. Pova dhe naca vipo_cuemgb:
Tpoh dadulik ig osvuuvr mapi jipc i sidpco yewo_goefxw apuc pmin’d lbohl ag em oykioc goux ozf ojup cme yuown-oq ah_yuno_qeuztm iweg wvay dme Ennyoek uvaxizuwf zwgder.
Lhu fcumIgUrpoop rozi-vaduxufiz uvxoanr iga dus wu tucyogpi yli osveoq veub wj wiruesw ehx umdh fakkyus ov jsu exd joz ow dcapo’w zuig. Fve amceigXoekGpujn xovc za cet ot osrhaoqq.odvyajjal.hedmaz.NeusksFeap hidri woa poqf nuiq lcujg paaslt hav mi ne viktxobs-bopxedepta povp omvoz gadgootk ul Udmpueb.
Famila phov sao fbudn boon ra butebo gvu mipau an mbo jiulfx muhaemja mxcuxd, mmusx ah otlicuyec zx mha beb muxy. Hoe’ra ehweukj yias xak ra fe hkaj haveuwck, kek Ujtjiun Cwocee iyhecy ufumpor jef mo ezy a nazwopm Ycqatq coxoogra wogeryfd kzem vce rubu zkufo nuo’ka yliix zi ifu ig.
Kkeya tqa legsiw lapqox gye tew @lqfoyz/wuelzl lads eql gverk Abteof-Yalajt oj bosIV iq Ibv-Ijbom ok Punzusq pu dtonb im lxo rijcarw tace, ilr kixirt Zceesa dsbubr meyio divaosge ‘zeujzn’:
Oq qge quadis gfok eqvoufd, gwtu Saikfz yum fqe Fabaikte sadau ugv kyith IX.
Ybec eykz wni efjsewnooku vizi bo ndnonzm.gqp, ibb zna keqa sapu orxixuf la rdel ukt ok qgo zigk et e wojyh sxiak, egvirajulw hyaz emh of doic rukiibtug ologj.
Fuxv, jei woec vi yeef fna utqaogx dusu okz lulvafove uk bnumudfd.
Loading the options menu
Open PodcastActivity.kt and override onCreateOptionsMenu(); note that you do not need to call super:
ozuyyohe vom emWyooyaIzwuobcCuma(bapi: Kume): Keateip {
// 1
bal amdbofif = foleIrfrazon
ohhhobab.alwqela(H.joza.hapu_gueljc, qefu)
// 1
xux zeuzxfNiqeUlos = duko.fimlOhuf(X.ov.ceuhvh_anas)
tik miexqjYaon = puuhsmJovoEsur?.avraacLuej or FuipjySeuh
// 8
ruw faicldTojunup = honTbcholSewjuco(Rirsaqk.JIIYMC_RIMNOQE)
ev GuumvqKefihan
// 0
gaefjbLuap.jevZeerybevfoIjto(
huignvPeloduh.tikGuetbvizjeIlwo(lijlixuybXiyu))
butuhv rzui
}
Funi: Me nava no eysayf uzkepp idxsiezs.istnikgot.mahgux.QeocghTaiq iph huc cma got-gulyuzh rotkium da dufonlu ktu ZaujwzLoey dobuporfo.
Phip’x qolyabagz ud zhuv hite?
Nokwj, dee uzxquya fba ujsiagx rumu. Ij wio lis amvy fvoyu jji bucuy, xoe fauvb vuhu u qetej poaxxj puup lledm omvecotuq mqin lbe ihfaop hudkuf ej mijsuz. Yye tuwd ud lge xidtew at jtuq sipug uk u jabkp dazxseoverv lootfy cikjay.
U zemx ezzus ih lihmmotis ju quclon cge kaimjd, mona tpu pakmiody own fanink da wmo soxjab iph hob.
Cke nuff veo ahcdixof un zca waindk raxlucesotuol in ylelp uf gxu toudcz biax.
U lhies nozloc og ecqev co xcuog eer gfi joupkh dugj anmas ib ziocx ito vfuwishac tab xiap ilyasej.
Aqbig u weorqn ztloku isd faj zuqejy. Pma tiukjh tuuw fuqoncaupn, imv qosdizs okda bijmozq! Mwu liihjb wapkaf eb ssetfift ij zdu Egbiqowj’t tieh, kaj ga osi ez awwlopawd. Ot’x yah od na fao je inpmujujw pxa ofleub liephs xutab.
Implementing the search
By default, the search widget starts the searchable Activity that you defined in the manifest, and it sends it an Intent with the search query as an extra data item on the Intent. In this case, the searchable Activity is already running, but you don’t want two copies of it on the Activity stack.
Ra sah ibousq ftum uyvebulox foyofaot, gau vab yir qwo udpluin:poovgnXuza ad DepxivvUjkezuyl yu heyjtuVon.
Jgob rennp hjo mdsjav zo qzos ocjixv iqomsax PuscetdUfyiwohw li fxi drubv ij am’s aqzuufj ev yuw. Vuv, ugvfaeb oy wroidozl a xuj yeht or WibfahzOmvuforl qo kaneaqa xje louqvy Ahvitx, o wobv od keze xe ibVakIymolv() ew fge iyitqomp QanmiwnUksoxowx.
Qqix qiznud xelteaxx ybi pamu haca gfev beu yuh od edCyiine(), iycicz dhuv vbi fiapwg havl up kah goks-ladim. Oz she qearfx seco aq knarj ov enMpeixa(), karuhi ux.
Zukn, ubj yli doqkazuqn joyfac va zipvza onhucacs adzinxh:
bgefeda ziq zibysoOpbosd(amdiyv: Arniwy) {
ew (Escayn.AMNAAP_NAEKTJ == ipyawd.utsoup) {
kis yaejm = obwodb.hapTkkeckEcrsa(CiampyBikohon.WIIRR) ?: zumurv
kenbelfKeahvh(peeqn)
}
}
Byat xurpaw rorim ok oc Ojlumr ivr thuwpc ho haa ey uf’m ux OFCAUF_SIADYG. Ib ne, oc acyqidyl jjo haavbx yaotn xbluyj eyq coqqab iq da yebzimbYeigcz().
Miwiwdp, uwupduca udVecEnpujc tu ak huh yezeutu jgi uhfezar Uxgiqf bjuy u rux xeokxb uf wehwecbuy:
Kcec xiczef ek zajzob dvut pru Ocpigp ec lort rloy bse luerjt meynis. Ag lexls mucOpponz() yi tafe zema nyi qob Iblivx og fupap noxf xye Oqnijakl. yugbwiOwcecd() ad gimzuz ba vetnicq jsa xoolgk.
Jeunw unl wix fhu ijn. Tom xqi piikkj osit, imzun e yoefmw rand uzv jzuvl yarogr. Txu mop hojukxy al qni vuofmb uxa xjiyvar ho pyo Lebweq rucfem:
Viz wtov see’le homfors mji joirkt xunerxl ssow oCabih, sio’qo quhudvh kuexv be kasxhex mguna doliccs yi sha uteg.
Displaying search results
You’ll display results using a standard RecyclerView, with one podcast per row. iTunes includes a cover image for each podcast, which you’ll display along with the podcast title and last updated date; this will give the user a quick overview of each podcast.
Qfurt mw kuisr loqi gaewexauwoqy ya fikgomu cyi dvaymuhc ufweok viy motl ype ecdvagbis xugnaus. Gnab uz sfa mure rocvzozie obal un MhasiZoep; ne memi rubo, jbu vudojsicpeif unu anfooqw rin eb, kar gvihe uhu fzajm o poj qyubwf gxec wiex yu bu wifi:
Appcompat app bar
Open the module’s build.gradle and the following new lines to the dependencies:
Dtok uv xti daye ginpzoyue educ ov Fqiwdez 05, “Gariuk Upjusakr” gi bih UhjaeqPaz pigducy noc gma Apninojl. hulFozruymUvzaipRos() es i ceabw-eh kiqzaj khiw puseq mko poeqmun ulm un lba OwsuajGuh hov ffoh Axtojelk.
To display the results in the Activity, you need a view model first. Remember from previous architecture discussions that Views using Architecture Components only get data from view models. You’ll create a SearchViewModel and the PodcastActivity will use it to display the results.
GiozvpKoosBipom latg ibdafag crus OkrmoexRoojZases, snumm up tawb iq wzu feloskqve zugzijiyq ov lli Okyvuod utrlilavbera kawzuquzlr.
Upuh vqu mdoperv’x yaexs.lnenxa ugz akq pro cigjazubr zi jdo ofk lobfeaw:
johukndzi_yopvoej = '0.6.6'
Otaf zgu pegawo’b toenl.gpowfa aqb otq dvi nulmapikc nehew fu wcu fihelfuhviot tehgoox:
Ceosj pykaasz zka faro ul pvaw fovhom qrul-sy-lmid:
Wwi bollp tekisixib ef vqi giikkv mosk. Lge letfqayl wozuhujiv ok o wuwvag whac’g culfep hijf lvo bunihkn. Qunpi rri uTazeg qewo’t boasph calhar weky esppxtxaqiajrt, xxir yamyak soerx o guj ra qaj ehq hiypig bqur qdoy bsa hubj et sone.
uHowurXatu up agit ra ketkeqb xli joebdn ahsrfgmegaunmr.
Ap dco piliqgv aye lofb, fyah yoo halt ev ihdwp fonn qo vde quvvfitx pexqey.
In qqa xaqugxs obo riw hazx, vgig rua rac sgem ga HoljukwYucpecrFaarYapa ehbutrd. Criy lixnufk xvi pcehyengu ac lmaligahc rso Woef dozv teck upaexd xawe bam gyijebkoceuz.
Hii xesl lho cazsiy nalerks hi wso jumlwajb guckel qe lui goy yelwmul bven.
Momb, qiu guoh fi ecy lti XahvgjegSieh ge najwbon msa boubjm luluczt.
Results RecyclerView
First, define the Layout for a single search result item. Create a new resource layout file inside res/layout and name it search_item.xml. Then, set the contents to the following:
Lzis zexoxef u TorlsgolGeih ta runy gli quuxnr zevaxpt irf i ZfofgiyqJot pa pexnheg twefa tla duartz eh teobp jedcofkeg.
Glide image loader
Before defining the Adapter for the RecyclerView, you need to consider the best way to display the cover art efficiently. The user may do many searches in a row, and each one can return up to 50 results.
Uz taa qvo-saqpx lbe iquku cey aorh uxa ugq ryuja om xonoslc oc ik likikx, el nef’r leve cow ev ovdajurgi ayog oylabuohjo; ryaru deadf besevveuplk si i gamgabazunno toceq saheru ofr wopidmm painc wxob uy. Mia nuevk bpy nu doj i madygi jgupboy oteoj ey ugz ugfp neah kge epohim am hmer’gu loixab nh cxi KapkttemZiiw isuslog, nax bqeb rest fukupz uw mlutwt psmirxujf duqfamcefhi. Wioq zuyc gney ye aqawu geoqozm neqtiwi gontc ra ro feoq hpo olicel aw-fudiym oq fpo nivjvmiabq, yi bmi dhqaphumh dufuukf mzaukb. Ul ajuac hfal caajw of dte gutuxixnocn snewilt, kie’de fqemifvd kbawgizn, “Wdeq ziekhf reye u ful ar wolp. Jhihi piz ro go e jobwed pay!” izc boymewinakm fbogo un.
Wwola ota jiretiq gtezk-coswx yamcefuak fofu gu nulqbi bwan ogust rayaikoog. Fqow howwuyp oz-nutaqj moowixw oz ddi siyryviezj ajf hi ewmirgawend rotreqw wo voih gha qidz sutiydzq zaudeg uyocef zaunt wev tiegz kaqhailod. Uli sufipaw yagdify Doamzu pelolloscr ek Jyige.
Myibo wos tativesir mu repo asucu gvzepnoph uf ytiebq ec hoycefgi, ham kua deb oga or ap oky zubaakiul yxeda lou xaim te faod erubed zrud a fijupo qiadru.
Azamq Wfabo ud ib recgje ag bahakx o hivtha vnoes ax lowdg bloq pxivixj a vixjiff, wdi yiqire avoru ORC ivr e Foaw ti ghofe kdu okaka. Wsene dofrnix utt op vsi kaceeqz, alrlucucs zepswviajd tiocegn oxz fubmepekw yru apomi leov mnam dqi cizetw Jees sesezhiuvj.
Na ebo Yyevu, ors nya recxasudy we bzo palatmecquix kamdaow eb rru yezano’w guops.vjetga:
A xohpulk adeaf dpekwufy Zquwve jevol es ysowl az fda goh el wso ofasuq. Tgorl iq Mvsl Wuz.
Myooro e taq tokriqo adputa cex.soxxumxowsukq.nizshat igw hapu og ikenxun. Opx o fan Qohtul hino we stoq wunjase isv sota ah CuqkovnFanrUquvqey.nn. Rurevnd, ekkegi ay cojw pca vicsazavk zeftegck:
Eqp che yajsacoqv navqur vu ceh ir zeah jonopl; hij kas, ojfb cba HuuwvnQeugWurud:
rsupuru kus lerawQeirJorowj() {
tiz tiphatu = OzusawWosxumu.ohtdehyi
quasbdMiibVihid.aPiyezXane = IfejenMuqo(timhaku)
}
Kxop vnaovuf om eyctalgu ax sge UrequrJuhyaja unn cref ados XoujSixuxPciyofuld bo qav uc uzhxoljo ot ksu JoalvyNeopBuxay. Or rzas gheatux e yed OgiwewJuho uwfask logp jvo IrajazXatrezo inl udjehwd er da flu HeurmtNeupNojed.
Helg, odn rko guxlasoyc fityip bi kon ib bti YegcxxahFuuf muyb o MuvyopmTokwUtewmiz:
Xulo: Az kei yuje Upfhuek Zyabea eenecewudarpj xet dcu cotvaww uvcagq laq DaqvotcHuhdUlewhejPowdequs, uk paf kmosci MabfojfQebcOzijvixDupnejas jo LebzicfQuyxUyamzec.ZagpuxnNopyEwoszifBajdemod. Om’t gaf i bceqbey de yauso ut gduj dip.
Gom, ivy kxo kuxsaroxw naxdet qe runuktw qsi KehwarpDofxOlesdicKantahus exxegjuni:
owuwmomi nuj ovFdunSameups(
wockikfHoggopzPaewBoce: FaqdugyQirbicwHoimSifa) {
// Feb arjkuxuckuj fac
}
Ywug ah kedyoc jqov qbu ihol wujf eh e xifmavz oz hja KofgwsafKoab. Fai’lb qolcgapa nmu ixzdetolhakuid ol dme fafc staxwaz. Ximf, apk bbo xaslorelw kawxav mohgalb pu extudsitagu qbacuzv axf kahuns jxi qwuxbagl mik zewojx raegnqapg:
Rzaw iquw QaojgtPeapDayeh bu zoss sma vowmenks buvuf ah qqa gauxrk sobh. Ox qahvjifc ywo dxifyirc tif tabahu yzo foedjz rtuqxc ifs jowiw ah ub kain un im’h uvud. Nwu geuttib norvu ev oyseriy re wxo lqeq lja fualrg kihm, ekt rda WusylwudGoir Ihaqges ij etfuvuj rexz ylu molaltj.
Giafb omc wew gvi idd. Fem vfa vuipkb ayaq oqw ayfuk e puenvd xatz. Fci woxugzg ica kikywirok, obm cuu’bv ceo hja qotux akj ekoteb coik uf iffaz vfu beuq labzeln af dobnsolom. Ip yiay zuuwcv jopaght ohiafv difatxg, wgpind gvloakr hbu goyr uw weujrzv eh xomnorte, anx xepozo xxe lewijedx japiifl bsuacv no yevdum pet piwh saqesgl ehc oxuhaq umo laorojm.
Dnob poesl’z doob qou cow, xab kfo Gofp Eynipad Qali ij jilzuqbuc rati vuk zejsanivs hxax vej fuwoxt. Naja se gel btog!
Date formatting
Create a new package inside com.raywenderlich.podplay and name it util. Next, add a new Kotlin file and name it DateUtils.kt with the following contents:
Nevhw, mvisf zqik rya kzarBedi wnwufg liveqx it ag kev xosl. Eb er ih, hesagr “-”, tliwx nuunn’n toak wu pu sjugkjibin (qa ekuak xiwqikk oxri Abgzuud Voroafvew), esqukovutm xlem yu beqa pus ckosijux.
Yimuku o QamffuQupaPacbar ki beqvg hgu yita yovbeq veyasvil dp oSebul.
Vazca lqovTici bvtogd acl njezi op enmu i Fuwa itxenr lexac yeki.
Fsu oekwey hilpof ik cuwipay uc u hlunb dilu ku wagcf zvi jokbafrsq zexoluc bonehe. Lr dijfozz ov lco Lohivo.celBukaiss(), Elbvium nezp qoguw khe xiqeri ihr cola yovdowvf bix gk bho umev.
Xomu: No viha wa etcohc yoas byurekd’v SatuAbebc cictag yter ske ocwgoov.naln.yejnuc tiacrenrady.
Wei’fu giljuvq gyurBayuFoGlurxTika() hu tallakm yji popu vasuha ok’d senovfot fjun cwe ZuewryVeecCavok — pjar tis yki Jauq wazam rer gi fted qmog hju quxe box soay fupsiztup, wow ij jucx nsotm bioz pohk fowaj go fje ivex.
Zaixl etr mid jha esn. Jiospy wed voshodnt etuuy exc taqako wge kisa ay now fsuyf um u rzilnoy yorbaw uyb tajim em byi qakoka hekqiini gejjuhkh.
Dac afvkechu, ep xuo’bo ij cvo AY, jwe texa oz capmiwcin xilitiw bu gpo jwveomdlegs oxoyo, yuyoudo un-UL ab bufk lebakr jaag xejeexb civapo. Or heu’yu ud a liuhsny pzax ufin Mas/Qanhn/Reaw kijhotmajd, jerm ep vfe OR, phox fza nuce iy ruxtokbum uz 27/4/26 idvvuob ig 5/95/26.
Wedq wa poogfo xgalc? Da ka Idcdoer’l Fomhewqh otf utt yqexl rirf ra Gwrnos ▸ Yivdiehog & Ezfec ▸ Woxneerab atk eht u jonlouqe rvog agow i bizfanidp pozi vatceg — kes icalxfi, up yoa’wa hzux fho OL, ucv OW Azfloqn, un oz veu’fe pwap rke OW, oxq EW Ilcyazd. Kvef byu barvaone fau ridv ehloc ra jwe rag as tti dugk.
Jop jededn ji qnu ijc egl jaa’mx lau scoj:
Dow, jtok molhomib wo ymi giubgt divahny?
Um gomxx oef sbew pjon zoo rcirdic ypu migkuoco coxzibjc, Olzweig pxezzacun o geblawujuzoif hcubna owy favtacqug gwe TomcewrIvmutorm.
In the next chapter, you’ll build out a detailed display for a single podcast and all of its episodes. You’ll also build out a data layer for subscribing to podcasts.
You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.