"Any attempt to endanger China's sovereignty and security ... or use Hong Kong to carry out infiltration and sabotage activities against the mainland is an act that crosses a red line," Xi said, in a nationalistic speech which emphasized Beijing's control over the city, now a special administrative region of China.
It's Xi's first visit to the city since he took power in Beijing in 2013, and comes amid fears by some Hong Kongers of increasing Chinese encroachment on the city's autonomy -- guaranteed under a framework known as "one country, two systems."
His words came after he swore in Carrie Lam as Hong Kong's first female Chief Executive. In her speech, she called for unity to heal the city's divided society, hours after police scuffled with pro-democracy protesters attempting to reach the harbor-side venue where the ceremony took place.
Lam said she recognized the political and economic challenges facing Hong Kong but added that the city's problems cannot be solved overnight.
Protests and arrests
As officials prepared for the sanctioned event Saturday morning, scuffles broke out when a pro-democracy march was attacked by pro-China campaigners.
Protesters had hoped to march on a flag-raising ceremony, but were prevented from moving more than a few hundred meters by flag-waving counter-demonstrators and a huge police cordon.
Leaders including high-profile campaigner Joshua Wong were arrested and bundled into police vans as their supporters shouted "shame." He was later released.
The police cordon was swiftly dropped after 8 a.m. local time, after the flag-raising ceremony went smoothly, with pro-China crowds singing the national anthem and applauding.
Lawmaker "Longhair" Leung Kwok-hung was also briefly taken into custody. Friday night, he had urged Hong Kongers to march on Saturday to "have your voices heard."
Hong Kong lawmaker Leung Kwok-hung (right) at a pro-democracy rally Saturday. Protesters were prevented from leaving the venue by pro-China counter demonstrators.
The confrontation, and police response, exposes the tensions in the city between those who seek greater freedom and those loyal to leadership on the mainland.
At a gala event Friday night, the Chinese leader urged Hong Kongers to unite and build on the progress made by previous generations.
Chinese President Xi Jinping attends a variety show on June 30, 2017 in Hong Kong.
"The road ahead is by no means smooth, but our faith in implementing 'one country, two systems' will not change, our determination won't be swayed," he said, referring to system that affords Hong Kong a degree of separation from the mainland.
Elsewhere protest leaders were urging Hong Kongers to have their voices heard in an annual pro-democracy march Saturday afternoon, by which time Xi will have left the city.
China's President Xi Jinping sings a song entitled "My Country" at the end of a gala event in Hong Kong on June 30, 2017.
Show of force
Xi's visit, which has seen unprecedented levels of security with roadblocks, huge barriers and a massive police presence, has been largely peaceful.
Multiple attempts by protesters to get anywhere near Xi during his time in the city ended in failure, while a pro-Hong Kong independence rally scheduled for Friday night was canceled after police refused organizers permission to use a public square.
Two dozen activists were arrested Wednesday after they stormed and occupied the public square where the flag-raising took place. The protesters, including "Umbrella Movement" leaders Joshua Wong and Nathan Law, were not released until early Friday morning.
Protesters stormed the Golden Bauhinia Square in central Hong Kong on June 28, 2017 ahead of an official visit by Chinese President Xi Jinping.
Pro-democracy activists are now turning their attention to the annual July 1 march, which regularly attracts more than 100,000 participants.
Protests and challenges
The mood among many pro-democracy campaigners is muted, nothing like the anger and frustration that saw hundreds of thousands occupy the streets of central Hong Kong during 2014's "Umbrella Movement."
Lam has indicated she may revive efforts to pass a controversial anti-sedition law known as Article 23.
Article 23 requires the Hong Kong government to "enact laws on its own to prohibit any act of treason, secession, sedition, subversion against the Central People's Government."
Some warn the law could be used as an excuse to crack down on criticism of the government or the discussion of controversial topics like Hong Kong independence.
Many locals fear the law could be the final nail in the coffin for the city's autonomy from China, granted under the principle of "one country, two systems."
A previous attempt to introduce the legislation drew half a million protesters onto the streets and resulted in the resignation of a key minister in 2003.
On Friday, Chinese Foreign Ministry spokesman Lu Kang said the Sino-British Joint Declaration, the agreement by which Hong Kong was handed over from the UK to China, was now just an "historical document" with no practical significance.
Chris Patten, the city's last British colonial governor, said that China had been pressuring Hong Kong "in all sort of ways."
"You see this in attacks on the judiciary and the rule of law, interference in court cases, you see it in the atmosphere of hostility toward the independence of universities and the media," he told CNN.
At a rally on Friday, practitioners of Falun Gong -- a Chinese breathing exercise similar to yoga that is denounced as a cult and banned in China -- expressed concern they may be suppressed in the future.
"By the law, we have freedom of speech and freedom of belief," Chow Wai-tung said. "We want to tell President Xi to stop persecuting Falun Gong in China."
Chow said he was worried the situation in Hong Kong was getting worse. "'One country, two systems' is a lie, in a reality it's just one system," he said. "They