I was able to make a navigation drawer and google sign in app using firebase tutorials from youtube https://www.youtube.com/playlist?list=PLGCjwl1RrtcR1j6EmpBxJyJYowK2QIsdT
I have activity_navigation.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="rjbc.cpvc.NavigationActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout = "@layout/navigation_action"
android:layout_height="wrap_content"
android:layout_width="match_parent" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_marginStart="101dp"
android:layout_marginTop="201dp"
android:id="@+id/textView5" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@mipmap/ic_person_black_24dp"
android:layout_below="@+id/textView5"
android:layout_alignEnd="@+id/textView5"
android:layout_marginEnd="42dp"
android:layout_marginTop="53dp"
android:id="@+id/imageView" />
</RelativeLayout>
</LinearLayout>
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:menu="@menu/navigation_menu"
android:layout_gravity = "start"
app:headerLayout="@layout/navigation_header" />
</android.support.v4.widget.DrawerLayout>
and navigation_header.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"
android:layout_width="match_parent" android:layout_height="140dp"
android:background="@color/colorPrimary"
android:padding="20dp"
android:id="@+id/pName">
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="13dp"
android:id="@+id/textView"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:textColor="@color/common_google_signin_btn_text_dark_default" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@mipmap/ic_person_black_24dp"
android:id="@+id/pPic"
android:layout_above="@+id/textView"
android:layout_alignEnd="@+id/textView"
android:layout_marginEnd="16dp"
android:layout_marginBottom="18dp" />
</RelativeLayout>
this is my navigation_activity.java
public class NavigationActivity extends AppCompatActivity {
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mToggle;
private Toolbar mToolbar;
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
private TextView mName;
private String name;
private Uri photoUrl;
private ImageView mPic;
private NavigationView mNavigationView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_navigation);
mToolbar = findViewById(R.id.nav_action);
setSupportActionBar(mToolbar);
mDrawerLayout = findViewById(R.id.drawerLayout);
mToggle = new ActionBarDrawerToggle(this,mDrawerLayout,R.string.open,R.string.close);
mDrawerLayout.addDrawerListener(mToggle);
mToggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
mAuth = FirebaseAuth.getInstance();
mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
if(firebaseAuth.getCurrentUser() == null) {
startActivity(new Intent(NavigationActivity.this,MainActivity.class));
}
}
};
mName = mNavigationView.getHeaderView(0).findViewById(R.id.textView);
mPic = mNavigationView.getHeaderView(0).findViewById(R.id.pPic);
getCurrentinfo();
mNavigationView = findViewById(R.id.nav_view);
mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(final MenuItem menuItem) {
int id = menuItem.getItemId();
switch (id) {
case R.id.nav_account:
Toast.makeText(NavigationActivity.this, "Account Selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.nav_settings:
Toast.makeText(NavigationActivity.this, "Setting Selected", Toast.LENGTH_SHORT).show();
default:
return true;
}
}
});
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(mToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
private void getCurrentinfo() {
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user != null) {
for (UserInfo profile : user.getProviderData()) {
// Id of the provider (ex: google.com)
String providerId = profile.getProviderId();
// UID specific to the provider
String uid = profile.getUid();
// Name, email address, and profile photo Url
name = profile.getDisplayName();
photoUrl = profile.getPhotoUrl();
mName.setText(name);
Picasso.with(getApplicationContext())
.load(photoUrl.toString())
.placeholder(R.drawable.logo)
.resize(100, 100)
.transform(new CircleTransform())
.centerCrop()
.into(mPic);
}
}
}
}
in my getcurrentInfo
function I am able to place my name and profile pic in the activity_navigation.xml
but I want to place it an navigation_header.xml
I tried changing the id of my ImageView
and TextView
to match with that in the navigation_header
but my app crashes.