Layout Alignment Between Class Swapping - Android Studio

2

I'm implementing a program that uses database and interacts with different layouts of entries and editing users.

I'm working with RelativeLayout on all screens. In one of the layouts , I enter a perfectly aligned button and give the android:visibility="gone" command to appear when prompted.

The problem is that when I need to use it with the editarBt.setVisibility(View.VISIBLE) command, the button appears out of alignment and overrides the fields for typing information.

Is there any way to keep the button position per command?

I'm not going to put the whole code because it has 7 classes, so I'll just put the classes that interest.

EnterPatientActivity Class

package br.luizhmu.aulas_android_sqlite;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

/**
* Created by LuizHMU on 2/17/15.
*/
public class EnterPatientActivity extends Activity {

private Paciente paciente = new Paciente();
private EditText nomeEt;
private EditText emailEt;
private EditText senhaEt;
private Button salvarBt;
private Button editarBt;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_inserir_paciente);

    nomeEt = (EditText) findViewById(R.id.editTextNome);
    emailEt = (EditText) findViewById(R.id.editTextEmail);
    senhaEt = (EditText) findViewById(R.id.editTextSenha);
    salvarBt = (Button) findViewById(R.id.buttonSalvar);
    editarBt = (Button) findViewById(R.id.buttonEditar);

    Intent intent = getIntent();
    if(intent != null){
        Bundle bundle = intent.getExtras();
        if(bundle != null){

            paciente.setId(bundle.getLong("id"));
            paciente.setNome(bundle.getString("nome"));
            paciente.setEmail(bundle.getString("email"));

            nomeEt.setText(paciente.getNome());
            emailEt.setText(paciente.getEmail());

            senhaEt.setVisibility(View.GONE);
            salvarBt.setVisibility(View.GONE);
            editarBt.setVisibility(View.VISIBLE);

        }
    }
}

public void salvar(View view){
    paciente.setNome(nomeEt.getText().toString());
    paciente.setEmail(emailEt.getText().toString());
    paciente.setSenha(senhaEt.getText().toString());

    DataBase bd = new DataBase(this);
    bd.inserir(paciente);

    Toast.makeText(this, "Paciente inserido com sucesso!", Toast.LENGTH_SHORT).show();
}


public void editar(View view){
    paciente.setNome(nomeEt.getText().toString());
    paciente.setEmail(emailEt.getText().toString());

    DataBase bd = new DataBase(this);
    bd.atualizar(paciente);

    Toast.makeText(this, "Paciente \""+paciente.getNome()+"\" atualizado com sucesso.", Toast.LENGTH_SHORT).show();
}

}

activity_insert_paciente.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:background="#ffffea0a"
tools:context=".EnterPatientActivity">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:text="Novo paciente"
    android:id="@+id/textView3"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:textColor="#ff1727ff"
    android:textSize="20dp"/>

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="textPersonName"
    android:hint="*Nome"
    android:ems="10"
    android:id="@+id/editTextNome"
    android:layout_below="@+id/textView3"
    android:layout_alignRight="@+id/buttonSalvar"
    android:layout_alignEnd="@+id/buttonSalvar" />

<EditText
    android:hint="Telefone"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="phone"
    android:ems="10"
    android:id="@+id/editTextTelefone"
    android:layout_below="@+id/editTextNome"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<EditText
    android:hint="*E-mail"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="textEmailAddress"
    android:ems="10"
    android:id="@+id/editTextEmail"
    android:layout_below="@+id/editTextTelefone"
    android:layout_alignLeft="@+id/editTextTelefone"
    android:layout_alignStart="@+id/editTextTelefone" />

<EditText
    android:hint="*Senha"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="textPassword"
    android:ems="10"
    android:id="@+id/editTextSenha"
    android:layout_below="@+id/editTextEmail"
    android:layout_alignLeft="@+id/editTextEmail"
    android:layout_alignStart="@+id/editTextEmail" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Salvar"
    android:id="@+id/buttonSalvar"
    android:onClick="salvar"
    android:layout_below="@+id/textView4"
    android:layout_alignRight="@+id/editTextSenha"
    android:layout_alignEnd="@+id/editTextSenha" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Editar"
    android:id="@+id/buttonEditar"
    android:layout_alignTop="@+id/buttonSalvar"
    android:layout_toLeftOf="@+id/buttonSalvar"
    android:layout_toStartOf="@+id/buttonSalvar"
    android:visibility="gone"
    android:onClick="editar"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:text="* Campos de preenchimento obrigatório"
    android:textColor="#000000"
    android:id="@+id/textView4"
    android:layout_below="@+id/editTextSenha"
    android:layout_alignLeft="@+id/editTextSenha"
    android:layout_alignStart="@+id/editTextSenha" />


</RelativeLayout>
    
asked by anonymous 18.02.2015 / 01:00

1 answer

3

According to android documentation :

  

View.GONE This view is invisible, and it does not take any space for   layout purposes.

     

View.INVISIBLE This view is invisible, but it still takes up space for   layout purposes.

In other words, when you use View.GONE your button does not occupy any space on the screen, in this case even you aligning the button in xml when using the View.GONE property this alignment is lost.

In the case of View.INVISIBLE , it maintains the alignment of the layout and only leaves its component invisible.

    
18.02.2015 / 02:23