I am coding the add fragment. That is on the right of the bottom navigation menu. But when i press add on the navigation it keeps navigating to the home page. And when it works properly i can not add a photo picker to users to pick image from gallery. My code is below
package com.example.uninotes;
import static android.app.Activity.RESULT_OK;
import static android.content.ContentValues.TAG;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.core.app.ActivityCompat;
import androidx.fragment.app.Fragment;
public class AddFragment extends Fragment {
ImageView imgGallery;
Button btnGallery;
ActivityResultLauncher<Intent> activitylauncher = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
new ActivityResultCallback<ActivityResult>() {
@Override
public void onActivityResult(ActivityResult result) {
Log.d(TAG, "onActivityResult: ");
}
}
);
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_add,container,false);
btnGallery = getView().findViewById(R.id.clickable);
imgGallery = getView().findViewById(R.id.imageView2);
btnGallery.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(Intent.ACTION_PICK,MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
activitylauncher.launch(intent);
}
});
return view;
}
}
I was trying to make the user pick image from the gallery.