Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why android navigation drawer creating two headers?

I followed a tutorial to push facebook info from login into an activity with a navigation, then put the into on top, as shown, which, after days of altering code, I finally got it to work. But now I cant get the original header to go away. I've tried changing every part of the code, but I always end up with all or nothing. I know two heads are better than one, but this is an exception to the rule.

Also, I guess I cant post images, so, the original header is on top, where it should be. The new header with my facebook profile picture and info, is underneath that. And below that, starts the navigation drawer menu.

public class HomeActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {


JSONObject response, profile_pic_data, profile_pic_url;
TextView user_name, user_email, tokens;
ImageView user_picture;
NavigationView navigation_view;

String name;

Button button2;

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

    tokens = (TextView)findViewById(R.id.textView17);

    button2 = (Button)findViewById(R.id.button2);

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

    Intent intent = getIntent();
    String jsondata = intent.getStringExtra("jsondata");
    final String uid = intent.getStringExtra("Uid");


    setNavigationHeader();    // call setNavigationHeader Method.
    setUserProfile(jsondata, uid);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
            fab.setVisibility(View.INVISIBLE);

    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            Firebase ref = new Firebase("https://luckycashslots.firebaseio.com/data/users/" + MainActivity.uID + "/");

            Tokens token = new Tokens("100");

            ref.setValue(token);

            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.setDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

    //Toast.makeText(getApplicationContext(), uid, Toast.LENGTH_LONG).show();

    button2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

           // Toast.makeText(getApplicationContext(), name, Toast.LENGTH_LONG).show();
        updateText();

        }
    });


    Firebase ref = new Firebase("https://luckycashslots.firebaseio.com/data/users/" + MainActivity.uID + "/");
    Firebase tokRef = ref.child("tokens");


    //tokRef.setValue(mAuthData.getProvider());

    //Tokens token = new Tokens(100);

    //ref.setValue(token);

    ref.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            if (dataSnapshot.child("tokens").getValue() != null) {
                name = (String) dataSnapshot.child("tokens").getValue().toString();
                tokens.setText(name);
                //tokens.setText(dataSnapshot.getValue().toString());
                //  Toast.makeText(getApplicationContext(), name, Toast.LENGTH_LONG).show();
                // System.out.println(dataSnapshot.getValue());
                // String woot = dataSnapshot.getValue().toString();
                // tokens.setText(woot);
            }

        }

        @Override
        public void onCancelled(FirebaseError firebaseError) {

            Toast.makeText(getApplicationContext(), "couldnt update token text", Toast.LENGTH_LONG).show();

        }
    });



}

public void updateText(){

    Firebase ref = new Firebase("https://luckycashslots.firebaseio.com/data/users/" + MainActivity.uID + "/");
    Firebase tokRef = ref.child("tokens");


    //tokRef.setValue(mAuthData.getProvider());

   // Tokens token = new Tokens(100);

  //  ref.setValue(token);

    ref.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

        for (DataSnapshot tokenSnapshot: dataSnapshot.getChildren()){

            Tokens token = tokenSnapshot.getValue(Tokens.class);
             System.out.println(token.toString());
            name = token.toString();
           // name = (String) dataSnapshot.child("tokens").getValue();
            tokens.setText(name);

        }

       // name = (String) dataSnapshot.child("tokens").getValue().toString();
       // tokens.setText(name);

        }

        @Override
        public void onCancelled(FirebaseError firebaseError) {

            Toast.makeText(getApplicationContext(), "couldnt update token text", Toast.LENGTH_LONG).show();

        }
    });


}

public void setNavigationHeader(){
    navigation_view = (NavigationView) findViewById(R.id.nav_view);
    navigation_view.removeHeaderView(null);
    View header = LayoutInflater.from(this).inflate(R.layout.nav_header_home, null);
            navigation_view.addHeaderView(header);
    user_name = (TextView) header.findViewById(R.id.username);
    user_picture = (ImageView) header.findViewById(R.id.profile_pic);
    user_email = (TextView) header.findViewById(R.id.email);
}

public void setUserProfile(String jsondata, String uid){
    try
    {

        response = new JSONObject(jsondata);
        user_email.setText(response.get("email").toString());
       // user_email.setText(MainActivity.uEmail);

         user_name.setText(response.get("name").toString());
       // user_name.setText(MainActivity.uName);
        profile_pic_data = new JSONObject(response.get("picture").toString());
        profile_pic_url = new JSONObject(profile_pic_data.getString("data"));


        Picasso.with(this).load(profile_pic_url.getString("url")).into(user_picture);
    }
    catch
            (Exception e) {
        e.printStackTrace();
    }
}

@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.home, 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) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();

    if (id == R.id.nav_slots) {
        // Handle the camera action1
        Intent intent2 = new Intent(this, SlotPageView.class);
        startActivity(intent2);

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

        Intent intent2 = new Intent(this, DailySpinActivity.class);
        startActivity(intent2);

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

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

        Intent intent2 = new Intent(this, PrizesActivity.class);
        startActivity(intent2);

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

        Intent intent2 = new Intent(this, WinnersActivity.class);
        startActivity(intent2);

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

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

        Intent intent2 = new Intent(this, AccountActivity.class);
        startActivity(intent2);

    }

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

activity_home.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout"
        android:layout_width="match_parent"     android:layout_height="match_parent"
   android:fitsSystemWindows="true" tools:openDrawer="start">

<include layout="@layout/app_bar_home" android:layout_width="match_parent"
    android:layout_height="match_parent" />

<android.support.design.widget.NavigationView android:id="@+id/nav_view"
    android:layout_width="wrap_content" android:layout_height="match_parent"
    android:layout_gravity="start" android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_home" app:menu="@menu/activity_home_drawer" />

App bar home xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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:fitsSystemWindows="true"
tools:context="learn2crack.learn2crackfb.HomeActivity">

<android.support.design.widget.AppBarLayout android:layout_height="wrap_content"
    android:layout_width="match_parent" android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar android:id="@+id/toolbar"
        android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_home" />

<android.support.design.widget.FloatingActionButton android:id="@+id/fab"
    android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin"
    android:src="@android:drawable/ic_dialog_email" />

</android.support.design.widget.CoordinatorLayout>

Nav header home 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="@dimen/nav_header_height"
android:background="@drawable/side_nav_bar"
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" android:orientation="vertical"
android:gravity="bottom">

<de.hdodenhof.circleimageview.CircleImageView
    android:id="@+id/profile_pic"
    android:layout_width="80dp"
    android:layout_height="80dp"
    android:paddingTop="@dimen/nav_header_vertical_spacing"
    android:src="@android:drawable/sym_def_app_icon"
    />

<TextView android:id="@+id/username"
android:layout_width="match_parent" android:layout_height="wrap_content"
    android:paddingTop="@dimen/nav_header_vertical_spacing"
    android:text="Android Studio"
    android:textSize="14dp"
android:textAppearance="@style/TextAppearance.AppCompat.Body1"

android:textColor="#000000" />

<TextView
    android:id="@+id/email"
    android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:text="[email protected]" />

</LinearLayout>
like image 925
JDKSoftDev Avatar asked Mar 14 '23 08:03

JDKSoftDev


1 Answers

u have to pass the header view into

navigation_view.removeHeaderView(navigation_view.getHeaderView(0);

and not null

navigation_view.removeHeaderView(null);
like image 172
JAAD Avatar answered Mar 23 '23 18:03

JAAD