-2

I want to change the appearance of the profile in the navigation drawer name and e-mail to the name and e-mail user as in the playstore. This is my code of login using MySQL:

LoginActivity.java

 @Override
                public void onResponse(String response) {
                    try {
                        JSONObject jsonObject= new JSONObject(response);
                        String success = jsonObject.getString("success");
                        JSONArray jsonArray = jsonObject.getJSONArray("login");

                        if (success.equals("1")){

                            for (int i=0; i<jsonArray.length();i++){
                                JSONObject object= jsonArray.getJSONObject(i);
                                String name = object.getString("name").trim();
                                Toast.makeText(LoginActivity.this, "Login Berhasil. \nSelamat Datang : "+name, Toast.LENGTH_SHORT).show();
                                Intent intent = new Intent(LoginActivity.this,MainActivity.class);
                                intent.putExtra("name", name);
                                intent.putExtra("email", email);
                                startActivity(intent);
                                loading.setVisibility(View.GONE);

                            }
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                        loading.setVisibility(View.GONE);
                        btnLogin.setVisibility(View.VISIBLE);
                        Toast.makeText(LoginActivity.this, "Error Check Kembali Email dan Password"+e.toString(), Toast.LENGTH_SHORT).show();
                    }
                }
            },
            new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            loading.setVisibility(View.GONE);
            btnLogin.setVisibility(View.VISIBLE);
            Toast.makeText(LoginActivity.this, "Error"+error.toString(), Toast.LENGTH_SHORT).show();
        }

My problem just visual from this login to the profile that display name and email on navigation bar.

This is my source of navigation

Nav_drawer.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:background="@color/colorAccent"
    android:gravity="bottom"
    android:padding="16dp"
    android:theme="@style/ThemeOverlay.AppCompat.Dark"
    android:layout_height="match_parent"
    android:id="@+id/nav">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:src="@mipmap/ic_launcher_round"/>

    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingTop="8dp"
        android:text="Your Name"
        android:textAppearance="@style/TextAppearance.AppCompat.Body1"
        />
    <TextView
        android:id="@+id/email"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Your Email@gmail.com"/>
</LinearLayout>

home.java

public class MainActivity extends AppCompatActivity {
    private DrawerLayout drawer;
    private TextView name,email;

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

        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        name = findViewById(R.id.name);
        email = findViewById(R.id.email);

        Intent intent = getIntent();
        String extraName = intent.getStringExtra("name");
        String extraEmail = intent.getStringExtra("email");

        name.setText(extraName);
        email.setText(extraEmail);

        drawer = 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();
    }

    @Override
    public void onBackPressed() {
        if (drawer.isDrawerOpen(GravityCompat.START)){
            drawer.closeDrawer(GravityCompat.START);
        }else {
            super.onBackPressed();
        }

    }
}
Nisse Engström
  • 4,738
  • 23
  • 27
  • 42
Floren
  • 33
  • 6
  • you can get the Navigation Drawer's header View like nav.getHeaderView(0); then change the child views :) – Darshan Aug 04 '18 at 13:40
  • so i should replace nav.getHeaderView after setContentView? okay i'll try it first – Floren Aug 04 '18 at 13:45

1 Answers1

2

You have to get the header view first, then after that you will need to get the child views that you are interested in, before you can set the text e.g.

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //... other onCreate setup        

        NavigationView mNavigationView = findViewById(R.id.nav_view);               
        View headerView = mNavigationView.getHeaderView(0);
        // get user name and email textViews
        TextView userName = headerView.findViewById(R.id.text_user_name);
        TextView userEmail = headerView.findViewById(R.id.text_email_address);
        // set user name and email
        userName.setText("username");
        userEmail.setText("email.user@domain.com");

}
Ebi Igweze
  • 460
  • 3
  • 8