简体   繁体   中英

how to display login information of user in header of navigation drawer?

I am working on an application in which user will login by providing username and email after login a page will be open with navigation drawer then i want to display the username and email in header of navigation drawer. I have tried many things like getting text from edittext and display it into textview but nothing works and I do not know how to this.

below are the files.

Navigation activity:
package com.example.hp.another;


import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class NavigationActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {


    private DrawerLayout mdrawerlayout;
    private ActionBarDrawerToggle mtoggie;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_navigation);
        mdrawerlayout=(DrawerLayout) findViewById(R.id.drawer);
        mtoggie= new ActionBarDrawerToggle(this, mdrawerlayout, R.string.Open, R.string.Close);
        mdrawerlayout.addDrawerListener(mtoggie);
        mtoggie.syncState();
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);



    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (mtoggie.onOptionsItemSelected(item)){
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        return false;
    }
}
header.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="160dp"
    android:orientation="vertical"
    android:background="@drawable/headerbg"
    android:padding="20dp">

    <ImageView
        android:src="@drawable/profile"
        android:layout_width="75dp"
        android:layout_height="75dp" />
    <TextView
        android:id="@+id/Username"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/colorPrimaryDark"
        android:textStyle="bold"
        android:layout_marginTop="5dp"/>
    <TextView
        android:id="@+id/Email"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/colorPrimaryDark"/>
</LinearLayout>
loginactivity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/logo"
    tools:context=".ProfileActivity">

    <EditText
        android:id="@+id/UserNmae"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginStart="64dp"
        android:layout_marginTop="144dp"
        android:ems="10"
        android:inputType="textPersonName"
        android:hint="Enter your Name"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="64dp" />

    <EditText
        android:id="@+id/UserEmail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignStart="@+id/UserNmae"
        android:layout_centerVertical="true"
        android:ems="10"
        android:inputType="textPersonName"
        android:hint="Enter your E-mail"
        android:layout_alignLeft="@+id/UserNmae" />

    <EditText
        android:id="@+id/Password"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true"
        android:layout_marginBottom="153dp"
        android:layout_marginStart="56dp"
        android:ems="10"
        android:inputType="textPersonName"
        android:hint="Enter your Password"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="56dp" />

    <Button
        android:id="@+id/Signup"
        android:layout_width="236dp"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="61dp"
        android:textColor="@color/colorPrimary"
        android:text="SignUp" />
</RelativeLay
Login Activity
package com.example.hp.another;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Switch;

public class ProfileActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText UserName, UserEmail, Password;
    private Button Signup;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profile);

        UserName=(EditText) findViewById(R.id.UserNmae);
        UserName.setOnClickListener(this);
        UserEmail=(EditText) findViewById(R.id.UserEmail);
        UserEmail.setOnClickListener(this);
        Password=(EditText) findViewById(R.id.Password);
        Password.setOnClickListener(this);
        Signup=(Button) findViewById(R.id.Signup);
        Signup.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.Signup:
                Intent intent=new Intent(ProfileActivity.this, NavigationActivity.class);
                startActivity(intent);
        }



    }
}

First access your header textview using the following code

        View nav = navigationView.getHeaderView(0);
        TextView Username= (ImageView)nav.findViewById(R.id.Username);
        TextView Email= (ImageView)nav.findViewById(R.id.Email);

After you login save the username and email in your sharedPreferences and set the value to these textViews

Whenever we store or retrieve data in/from SharedPreferences we need to commit that specific purpose.

private SharedPreferences sharedPref;
private static String PREF_STRING = "pref_value";

  // method to store value in sharedPreferences
 public void login(String username_params, String email_params)
 {
   // initialize the sharePreferences
   sharedPref = getApplicationContext().getSharedPreferences(PREF_STRING,0);
   // use the editor to save the data
   SharedPreferences.Editor editor = sharedPref.edit();
   // add data to sharedPreferences using put() method

   editor.putString("username",username_params);
   editor.putString("email",email_params);

   // only this won't save your data to preferences, we need to commit this 
   transaction
   editor.commit(); // only after commit() data is saved in sharedPreferences
 }

Now after you save the data in preferences, we can access that value from any part of our application.

  public void getDataFromPrefernces()
  {
      // initialize the sharePreferences
      SharedPreferences prefefnces = 
      getApplicationContext().getSharedPreferences(PREF_STRING,0);
      // use the editor to retrieve the data
      SharedPreferences.Editor editor = prefefnces.edit();

      // here we save the sharedpreference values to string variables
      String username = prefefnces.getString("username",""); // make sure 
      you donot mismatch the name of the key/ it must be same with that you 
      stored
      String email = prefefnces.getString("email",""); 

      // again to get data we need to commit()
      editor.commit();

      // you can use these values from string variables wherever you need
   }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM