Hello, I'm new here and I've already had problems, kkk.
Well, after several tutorials I was able to create users to my application and save email, name and password.
I would now like to be able to recover after logging in, and show the name and email of the navigation drawer, respectively.
Below is what I already did in the registry.
public class CadastroActivity extends AppCompatActivity {
private TextInputEditText campoNome, campoEmail, campoSenha;
private FirebaseAuth autenticacao;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cadastro);
campoNome = findViewById(R.id.editnome);
campoEmail = findViewById(R.id.editEmail);
campoSenha = findViewById(R.id.editSenha);
}
public void cadastrarUsuario(final Usuario usuario) {
autenticacao = ConfiguracaoFirebase.getFirebaseAutentificacao();
autenticacao.createUserWithEmailAndPassword(
usuario.getEmail(), usuario.getSenha()
).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
Toast.makeText(CadastroActivity.this,
"Usuario cadastrado com sucesso", Toast.LENGTH_SHORT).show();
//
UsuarioFirebase.atualizarNomeUsuario(usuario.getNome());
finish();
//Salvando usuario no database do firebase
try{
String indentificadorUsuario = Base64Custom.codificarBase64(usuario.getEmail());
usuario.setId( indentificadorUsuario );
usuario.salvar();
}
catch (Exception e){
e.printStackTrace();
}
} else {
//validações e tratamento de exceções do firebase
String excecao = "";
try {
throw task.getException();
} catch (FirebaseAuthWeakPasswordException e) {
excecao = "Digite uma senha descente com caracteres especiais e numeros";
} catch (FirebaseAuthInvalidCredentialsException e) {
excecao = "Digite um email valido, isso é um aplicativo serio de familia tradicional";
} catch (FirebaseAuthUserCollisionException e) {
excecao = "Essa Conta já existe bb, não tente me enganar";
} catch (Exception e) {
excecao = "Erro ao cadastrar usuário: " + e.getMessage();
e.printStackTrace();
}
Toast.makeText(CadastroActivity.this,
excecao, Toast.LENGTH_SHORT).show();
}
}
});
}
public void validarCadastroUsuario(View view){
//recuperando os textos digitados
String textoNome = campoNome.getText().toString();
String textoEmail = campoEmail.getText().toString();
String textSenha = campoSenha.getText().toString();
//verificação de campo vazio
if (!textoNome.isEmpty())
{
if (!textoEmail.isEmpty())
{
if (!textSenha.isEmpty())
{
Usuario usuario = new Usuario();
usuario.setNome(textoNome);
usuario.setEmail(textoEmail);
usuario.setSenha(textSenha);
cadastrarUsuario(usuario);
}else
{
Toast.makeText(CadastroActivity.this,
"Digite sua senha", Toast.LENGTH_SHORT).show();
}
}
else
{
Toast.makeText(CadastroActivity.this,
"Digite seu email", Toast.LENGTH_SHORT).show();
}
}
else
{
Toast.makeText(CadastroActivity.this,
"Digite seu Nome", Toast.LENGTH_SHORT).show();
}
}
}
A main:
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
private FirebaseAuth autenticacao;
private String identificadorUsuario;
private EditText perfilNome;
@SuppressLint("WrongViewCast")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
autenticacao = ConfiguracaoFirebase.getFirebaseAutentificacao();
perfilNome = findViewById(R.id.perfilNome);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
switch ( item.getItemId() ){
case R.id.menu_sair:
deslogarUsuario();
finish();
break;
}
return super.onOptionsItemSelected(item);
}
public void deslogarUsuario(){
try{
autenticacao.signOut();
}
catch (Exception e){
e.printStackTrace();
}
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
//Recuperar dados do Usuário
FirebaseUser usuario = UsuarioFirebase.getUsuarioAtual();
String email = usuario.getEmail();
if( email != null){
}
else{
}
perfilNome.setText( usuario.getDisplayName() );
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_camera) {
// Handle the camera action
} else if (id == R.id.nav_gallery) {
} else if (id == R.id.nav_slideshow) {
} else if (id == R.id.nav_manage) {
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
and the nav_header where I want by name and password:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="@dimen/nav_header_height"
android:background="@drawable/side_nav_bar"
android:gravity="bottom"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:theme="@style/ThemeOverlay.AppCompat.Dark">
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/nav_header_desc"
android:paddingTop="@dimen/nav_header_vertical_spacing"
app:srcCompat="@mipmap/ic_launcher_round" />
<TextView
android:id="@+id/perfilNome"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/nav_header_vertical_spacing"
android:text="@string/nav_header_title"
android:textAppearance="@style/TextAppearance.AppCompat.Body1" />
<TextView
android:id="@+id/emailUsuario"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/nav_header_subtitle" />
</LinearLayout>