Andrews caused a political storm last month when he stepped in to cancel the visa just hours after a Brisbane magistrate freed him on bail.
Haneef who was working in the Gold Coast Hospital was taken into custody two days after the failed Glasgow bombing. He was actually intercepted at the airport as he was about to leave for his native Bangalore in southern India on a one-way ticket.
Andrews claimed Haneef's association with two male relatives allegedly involved in failed bomb plots in Britain meant he was of bad character and should not be allowed to stay at his home on the Gold Coast.
It later emerged Haneef had given his mobile phone SIM card to second cousin Sabeel Ahmed and had once borrowed money from Sabeel's brother Kafeel, who has since died after suffering serious burns in a car bomb attack on Glasgow Airport.
For all the inspired stories in the media, even a fortnight after his arrest, the Australian Federal Police were unable to find anything incriminating against Haneef. It was at that stage the Brisbane magistrate intervened.
Still Immigration Minister Andrews played truant. Not only he peremptorily revoked the Indian doctor's visa, he also ordered that Haneef be held in detention pending the hearing of his charge.
Ten days later terrorism charges against the Indian doctor were dropped and he was released into residential detention in Brisbane. Still the passport was not returned. It was the next day, but again the minister refused to reinstate Haneef's work visa.
Meantime Haneef returned to India in a blaze of glory, but his lawyers pressed the visa case.
They disputed Andrew's use of the word 'association' and questioned whether any relationship with an alleged criminal, even an innocent one, would be enough to cancel somebody's visa.
Justice Jeffrey Spender also ruled the minister applied the wrong test when judging Haneef's character.
'The minister cancelled the visa by adopting a wrong criterion. He fell into jurisdictional error by applying the wrong test,' Justice Spender said.
He found that, had the minister relied upon information that Dr Haneef was considered a person of interest by British counter-terrorism police and had been formally charged with providing resources to a terrorist organisation, the minister would have been justified in cancelling his visa.
'These matters would have permitted the minister to conclude that the association between Dr Haneef and the Ahmed brothers went beyond a purely familial, social and 'innocent' relationship,' he said.
'On that material, it would have been open to the minister ... to cancel Dr Haneef's visa.'
Solicitor David Bennett filed an immediate application of stay, which Justice Spender granted for a period of 21 days.
Speaking shortly after sittings of State Parliament today, Queensland Premier Peter Beattie welcomed the court's ruling.
Greens senator Kerry Nettle demanded Mr Andrews step down.
'Today's vindication of Dr Haneef by the Federal Court makes Minister Andrews' position as immigration minister untenable,' Senator Nettle said.
'Minister Andrews has tarnished Australia's reputation and should do the right thing by resigning as a minister. After this decision, the community is right to have no confidence in Minister Andrews or the Federal Government.'
She urged the Government offer Dr Haneef an apology and compensation.
Beattie, who was critical of the minister's original decision, called in state parliament for Mr Andrews to resign, labelling him an 'international embarrassment'.
Federal Labor's immigration spokesman Tony Burke said Labor - which offered in-principle support for Mr Andrews' original decision - would not rush to call for his resignation.
'What we do need to maintain is community support for our immigration and anti-terror laws,' Mr Burke said.
Meanwhile in Bangalore Imran Siddiqui, a cousin of the Indian doctor's wife, said, 'Haneef is happy and so is the entire family..We are very gratified by the decision of the court.'