Obama's visit comes as Spain remains mired in a months-old political crisis, with two general elections resulting in no clear victor.
On Sunday evening, the president made a visit to an American naval base in Rota, Cadiz, to address thousands of American military personnel and their families. On what is expected to be his last presidential visit to Europe, Obama is visitin.
To some degree, the trip is a familiar ritual for a president who has embarked in recent years on similar consolation missions with relentless frequency. He will spend a day in Spain and visi.
He said he has longed to return to Spain ever since he passed through while backpacking across Europe decades ago, during his 20s, but that the "horrific shootings in the United States require that I cut my time here short".
He also urged law enforcement to treat seriously complaints that they are heavy-handed and intolerant, particularly toward minorities. Chip Reid reports on the president's response to the attack.
Obama said the Black Lives Matter movement had grown out of a tradition that dated to the abolitionist movement, the women's suffrage campaign, and the protests against the war in Vietnam.
The White House stated that Obama will visit relatives of the officers killed in Thursday's attack.
Obama repeated that the vast majority of US police officers are doing a good job, and said rhetoric portraying them as otherwise does little to rally support for the effort to overhaul a criminal justice system broadly recognized as biased against minorities.
A respectful debate, Obama said, is "what's going to ultimately help make the job of being a cop a lot safer". Vice President Joe Biden and former First Lady Laura Bush will attend the service too.
Obama said that police and activists need to work together and "listen to each other" in order to mobilize real change in America.
Obama condemned the sniper-style shootings of Dallas police officers Friday, calling the assault a "vicious, calculated and despicable attack on law enforcement".
Obama was supposed to spend two days in Spain after attending a North Atlantic Treaty Organisation summit in Warsaw earlier this week.
The president sees delivering this sort of guidance a core part of his leadership, so much so that some of his memorable speeches were in honor of mass shooting victims, including his challenge to protect children from guns in Newtown, Conn. - "We're not doing enough." - and his singing of "Amazing Grace" after the shooting in a black church in Charleston, S.C. Tourists and curiosity seekers lined some streets in hopes of catching a glimpse of him, and local TV aired wall-to-wall coverage of his movements.
Obama met earlier Sunday with King Felipe VI.
Obama is squeezing in a visit to a key North Atlantic Treaty Organisation and European Union partner before dashing home to deal with the aftermath of a wrenching shooting in Dallas.
Spain has been gripped by a political stalemate for months, with Rajoy unable to rally the political support he needs to form a new coalition government following a late-June election.
As if to punctuate that message, Obama's last stop was at Naval Station Rota, a Spanish base on the Atlantic Ocean that's home to four USA destroyers.