-1

I wanted to change the name and image of the header to the username of the people who are currently logged in but no idea how to do it I'm new to android studio and don't know how this works, i also tried to find the @string/nav_header_subtitle and i can't find it anywhere can anyone help me?

Edit:

This is the main activity

    public class MainActivity extends AppCompatActivity

    implements NavigationView.OnNavigationItemSelectedListener();

    NavigationView  navigationView = (NavigationView) findViewById(R.id.navigation_view);
    View header = navigationView.getHeaderView(0);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        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.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id == R.id.nav_profile) {

        } else if (id == R.id.nav_balance) {

        } else if (id == R.id.nav_setting) {

        } else if (id == R.id.nav_help) {

        } else if (id == R.id.nav_logout) {

            final ProgressDialog dlg = new ProgressDialog(MainActivity.this);
            ParseUser.logOut();
            moveTaskToBack(true);
            android.os.Process.killProcess(android.os.Process.myPid());
            System.exit(1);

        }
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;

      }
   }

Where should I put the code in there?

Edit: I did the codes below and this showed up

Edit: Header:

<?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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingBottom="@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/txt_namedisplay"
    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.Body2" />

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/nav_header_subtitle" />

Ali Khaki
  • 1,184
  • 1
  • 13
  • 24
Euxicius
  • 47
  • 9

3 Answers3

2

Change your main activity to something like this:

TextView username, mailusernave; 
ImageView imageUser;
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
View headerView = navigationView.getHeaderView(0);
username = (TextView) headerView.findViewById(R.id.usernamenav);
mailusernave = (TextView) headerView.findViewById(R.id.mailnav);
imageUser = (ImageView) headerView.findViewById(R.id.imgUser);
username.setText("name")
tharkay
  • 5,913
  • 2
  • 26
  • 33
0

First define TextView and ImageView in your NavigationView header then do this in your Java code

NavigationView  navigationView = (NavigationView) findViewById(R.id.navigation_view);
View            HeaderView= navigationView.getHeaderView(0);         
TextView        TextHeaderTitle = (TextView) headerView.findViewById(R.id.txt_namedisplay);
TextView        TextHeaderSubTitle = (TextView) headerView.findViewById(R.id.txt_namedisplay);
ImageView       ImageUser = (ImageView) headerView.findViewById(R.id.imageView);

TextHeaderTitle.setText("TITLE TEXT");
TextHeaderSubTitle .setText("SUB TITLE TEXT");
ImageHeaderNav.setImageResource(R.drawable.image_user);
Ali Khaki
  • 1,184
  • 1
  • 13
  • 24
0

Solution:

You can get the HeaderView from the NavigationView object like this:

NavigationView  navigationView = (NavigationView) findViewById(R.id.navigation_view);
View header = navigationView.getHeaderView(0);

Then,

TextView HeaderName = (TextView) header.findViewById(R.id.txt_namedisplay);
TextView HeaderEmail = (TextView) header.findViewById(R.id.textView);
ImageView imgHeaderLogo = (ImageView) header.findViewById(R.id.imageView);

Then, get the String which you want to display:

String title = "Your_Title_String";
String desc = "Your_Description_String";
String image = "Your_base64_Image";

HeaderName.setText(title);
HeaderEmail.setText(desc);

Then, For Profile image, (if you have BASE64 String), Use:

byte[] image = Base64.decode(image, Base64.DEFAULT);
int i = image.length;
Bitmap bmp = BitmapFactory.decodeByteArray(image, 0, image.length);
imgHeaderLogo.setImageBitmap(bmp);

Use all this inside your onCreate()

That's it..

Hope it helps.

Ümañg ßürmån
  • 9,695
  • 4
  • 24
  • 41
  • I tried using your codes and have problems with Cannot resolve symbol 'imgProfileimage' / 'lblHeaderName' / 'lblHeaderEmail' and in the HeaderName.username(Your_name_string); HeaderEmail.setText(Your_Email_String); I totally hve no idea what to do – Euxicius Oct 23 '18 at 09:15
  • Can you show me your code so that I'll assist you further.. This is a working example of my own project. @Euxicius – Ümañg ßürmån Oct 23 '18 at 09:16
  • friendly are good.but here we search for solution and there are a main rule for answering in this site that just created for help people spend less time for find good and clear answer , and friendly not one of that rule ! – Ali Khaki Oct 23 '18 at 09:19
  • I edited above the image on what happen when i put it in the main – Euxicius Oct 23 '18 at 09:19
  • @Euxicius In the place of red colored Cannot resolve, you must write your own xml ids – Ümañg ßürmån Oct 23 '18 at 09:19
  • Okay @AliKhaki Thanks. But, that's my way of writing. – Ümañg ßürmån Oct 23 '18 at 09:20