1

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) {

    }
AdamMc331
  • 16,492
  • 10
  • 71
  • 133
Swamy Veera
  • 227
  • 1
  • 3
  • 13
  • https://github.com/codepath/android_guides/wiki/Fragment-Navigation-Drawer – Suhas Bachewar Nov 30 '15 at 17:18
  • Possible duplicate of [Android replace the current fragment with another fragment](http://stackoverflow.com/questions/8163104/android-replace-the-current-fragment-with-another-fragment) – AdamMc331 Nov 30 '15 at 18:12

2 Answers2

1

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?

ThimoKl
  • 329
  • 2
  • 9
  • 1
    @SwamyVeera as he explained in the comments of the answer, you need to replace that line with whatever the id is for the FrameLayout in your activity's XML layout. – AdamMc331 Nov 30 '15 at 18:13
0

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

Piotr Labunski
  • 1,638
  • 4
  • 19
  • 26
sky00145
  • 24
  • 5