Let $\Omega \subset \mathbb R^d$ a smooth bounded connected domain of $\mathbb R^d$. Let $A$ symmetric and uniformly elliptic matrix defined on $\Omega $. Let $f\in L^2(\Omega )$ and $g\in H^{-1/2}(\partial \Omega )$. Prove that $$\begin{cases}-\nabla \cdot (A\nabla u)=f&\Omega \\ A\nabla u\cdot \nu=g&\partial \Omega \end{cases}$$ has a unique weak solution in $H^1(\Omega )$ if and only if $$\int_\Omega f+\int_{\partial \Omega }g=0.$$
Let $X=\{u\in H^1(\Omega )\mid \int_\Omega u=0\}$. The weak equation is given by $$\int_\Omega A\nabla u\cdot \nabla v=\int_{\partial \Omega }gv+\int_\Omega fv,\quad \forall v\in X.\tag{E}$$
Question 1
For the necessary condition it's written : assume (E). taking $v=1$, the claim follow. But what I don't understand is that such a $v$ is not in $X$, so how can we take $v=1$ and replace in (E) ?
Question 2
For the existence and uniqueness, they do as follow : Set $$a(u,v)=\int_\Omega A\nabla u\cdot \nabla v, \quad \forall u,v\in H^1(\Omega )/\mathbb R,$$ and $$F(v)=\int_{\partial \Omega }gv+\int_\Omega fv,\quad \forall v\in H^1(\Omega ).$$ The claim follow from Lax-Miligram.
The set $H^1(\Omega )/\mathbb R$ is the space of $H^1(\Omega )$ function that are not constant. Why do we take $u,v\in H^1(\Omega )/\mathbb R$ and not simply in $X$ ?
Your weak formulation (E) is not correct.
The correct weak formulation is: find $u \in H^1(\Omega)$, such that $$ a(u,v) = \int_\Omega f \, v \, \mathrm{d}x + \int_{\partial\Omega} g \, v \, \mathrm{d}x \qquad\forall v \in H^1(\Omega),$$ where the bilinear form $a$ is defined by using $A$.
Let us define the bounded, linear operators $L, I : H^1(\Omega) \to H^1(\Omega)'$ and the functional $F \in H^1(\Omega)'$ via $$ \langle L u , v \rangle_{H^1(\Omega)',H^1(\Omega)} := a(u,v) \\ \langle I u , v \rangle_{H^1(\Omega)',H^1(\Omega)} := \int_\Omega u \, v \, \mathrm{d}x \\ \langle F , v \rangle_{H^1(\Omega)',H^1(\Omega)} := \int_\Omega f \, v \, \mathrm{d}x + \int_{\partial\Omega} g \, v \, \mathrm{d}x$$ for all $u, v \in H^1(\Omega)$.
Now, the weak formulation becomes $$L u = F.$$
Further, it is easy to check (via Lax-Milgram), that $L + I$ is boundedly invertible. Moreover, $I$ is compact (via Rellich-Kondrachov). Thus, $L$ is a Fredholm operator with index $0$. Hence, $L u = F$ is solvable if and only if $$ \langle F, v \rangle = 0 \qquad \forall v \in H^1(\Omega) \text{ with } L^* v = 0.$$ Now, $L^* v = 0$ implies that $v$ is a constant function. Thus, $$ \langle F, 1 \rangle = 0 $$ is a necessary and sufficient condition for the solvability of the weak formulation and this is exactly your condition.
Finally, let me mention why (E) is always solvable. It is easy to check that the solution of (E) does not change if $f$ is replaced by $f + c$ for any constant $c$. By setting $c$ to the appropriate value, you can achieve the compatibility condition $\int f + \int g = 0$. However, the associated solution will not be a weak solution in $H^1(\Omega)$.