0

my question is i want to change my display image. i have a setting fragment where i can update my information. when i click on my image is showing another activity where i have set of few images. From this activity i want to choose an image and i want to update my display image in my fragment. so basically i am updating my profile picture and displaying it from fragment to activity. i have added images as well would you please have a look. Many thanks in advance thanks enter image description hereenter image description here

this is my code below on image selection. i can find the solution choosing image from gallery but in my case is differnt and hard to solve my problem as a new in android.

AdapterView.OnItemClickListener myOnItemClickListener = new AdapterView.OnItemClickListener(){

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        int prompt = (int)parent.getItemAtPosition(position);
        Toast.makeText(getApplicationContext(), prompt, Toast.LENGTH_LONG).show();
        finish();



    }};

1 Answers1

0

You could use startActivityForResult to start the Activity where you choose the image, then on finish you could pass the selected image.

In the fragment you would need to override the OnActivityResult method and use it to change the profile picture.

Edit 1

Code that goes in your fragment:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    /** your code **/


    profilePicture.setOnClickListener(new OnClickListener() {
        void onClick(View v) {
            Intent i = new Intent(this, ActivityToPickImage.class);
            startActivityForResult(i, 1); // you should define a constant instead of 1
        }
    });

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (requestCode == 1) {
        if(resultCode == Activity.RESULT_OK) {
            int result = data.getIntExtra("result", 0);
            //result is the code of the picked image
            //code to change profile picture goes here
        }
    }

}

Code that finishes the activity:

AdapterView.OnItemClickListener myOnItemClickListener = new AdapterView.OnItemClickListener(){

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        int prompt = (int)parent.getItemAtPosition(position);
        Toast.makeText(getApplicationContext(), prompt, Toast.LENGTH_LONG).show();
        Intent returnIntent = new Intent();
        returnIntent.putExtra("result", prompt);
        setResult(Activity.RESULT_OK, returnIntent);
        finish();

}};

Example on starting an activity for result

When you use startActivityForResult you are able to pass an intent to the previous Activity/Fragment when it is finishig. When that happens the onActivityResult method is called and then you can set the right profile picture.

Community
  • 1
  • 1
rlinoz
  • 130
  • 5