Reputation: 237
I am a beginner using Android Studio to create an app and when I click a navigation menu item I want to show a different fragment. I have searched tutorials but I have not found how to use it.
For example, in the template, when I click camera it should show a camera fragment, gallery would show a gallery fragment, and so on:
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_camara) {
// Handle the camera action
// How do I display fragment?
} else if (id == R.id.nav_gallery) {
} else if (id == R.id.nav_slideshow) {
} else if (id == R.id.nav_manage) {
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
}
Upvotes: 1
Views: 1759
Reputation: 24
You Can try like this,
final StorageReference filePath = UserProfileImagesRef.child("Profile Images").child(currentUserID + ".jpg");
filePath.putFile(resultUri).addOnCompleteListener(new OnCompleteListener < UploadTask.TaskSnapshot > () {
@Override
public void onComplete(@NonNull Task < UploadTask.TaskSnapshot > task) {
if (task.isSuccessful()) {
filePath.getDownloadUrl().addOnSuccessListener(new OnSuccessListener < Uri > () {
@Override
public void onSuccess(Uri uri) {
String downloadUrl = uri.toString();
RootRef.child("Users").child(currentUserID).child("image").setValue(downloadUrl).addOnCompleteListener(new OnCompleteListener < Void > () {
@Override
public void onComplete(@NonNull Task < Void > task) {
if (task.isSuccessful()) {
loadingBar.dismiss();
Toast.makeText(SettingActivity.this, "Your picture Saved successfully", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(SettingActivity.this, "Problem occurred while tryng to save your picture..", Toast.LENGTH_SHORT).show();
}
}
});
}
});
} else {
Toast.makeText(SettingActivity.this, "Your picture did NOT saved", Toast.LENGTH_SHORT).show();
loadingBar.dismiss();
}
}
});
Upvotes: 0
Reputation: 329
Your Activity should include a FrameLayout
. In onNavigationItemSelected()
you create the Fragment
for the camera, gallery, etc. Then you put this fragment into the FrameLayout
.
public boolean onNavigationItemSelected(MenuItem item) {
Fragment newFragment; // This is the fragment you want to put into the FrameLayout
int id = item.getItemId();
if (id == R.id.nav_camara) {
newFragment = new CameraFragment();
} else if (id == R.id.nav_gallery) {
newFragment = new GalleryFragment();
} else if (id == R.id.nav_slideshow) {
// [...]
}
// Let's put the new fragment into the FrameLayout
// If you use the support action bar, use getSupportFragmentManager(), else getFragmentManager()
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, newFragment); // R.id.fragment_container = FrameLayout ID
transaction.commit();
}
Does this help?
Upvotes: 1