2
I am implementing a program that uses database and interacts by differentlayouts registration and user editing.
I’m working with RelativeLayout on all screens. On one of thelayouts, I insert a perfectly aligned button and give the command to android:visibility="gone" for him to appear when requested.
The problem is that when I need to use it by the command editarBt.setVisibility(View.VISIBLE), the button appears out of alignment and overwrites the fields for typing information.
Is there any way to hold the button by command?
I won’t put the entire code because it has 7 classes, so I’ll put only the classes that matter.
Class Enterpatientactivity
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_inserir_patient.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>
Considering your response, I’ve landed command on XML for
android:visibility="invisible"but the error remains the same. When I call the button, I already useView.VISIBLE,editarBt.setVisibility(View.VISIBLE);@iTSangar– Luiz Henrique Ugliano
I’m without Android Studio here now, but have you ever tried to set the invisible button in the code instead of xml? (ps.: kkkkkkkk was typing this comment and you doing testing the same thing there)
– iTSangar
I made the following changes and it worked, thank you very much!
senhaEt.setVisibility(View.INVISIBLE);salvarBt.setVisibility(View.INVISIBLE);@iTSangar– Luiz Henrique Ugliano