Question

What do I need to do to get the resId of a drawable that is set to a button?

FLOW

User creates an event >> User clicks "picture button" >> User prompted with AlertDialog with a list of drawables >> User Selects Drawable >> Drawable is set to "picture button" >> User presses a "finish button >> The drawable resID should be passed as an int using putInt() from the CreateActivity to the MainActivity

I cannot seem to figure out how to get the resId of the drawable that was set to the "picture button" by the alertdialog in PicturePickerFragment.

I have tried using getCompoundDrawable(), but I am not sure if I am implementing it incorrectly.

In CreateActivity

// Will be called via the onClick attribute
// of the buttons in main.xml
public void onClick(View view) {
    switch (view.getId()) {
    case R.id.btn_confirm:
        String title = etTitle.getText().toString();
        String time = btTime.getText().toString();
        String date = btDate.getText().toString();

        btPic.getCompoundDrawables();
        int resId = getResources().getIdentifier("com.example.datetracker:drawable/baby", null, null);;

        Log.e("LOG", title);
        Log.e("LOG", time);
        Log.e("LOG", date);

        Bundle newBundle = new Bundle();
        newBundle.putString("TITLE", title);
        newBundle.putString("TIME", time);
        newBundle.putString("DATE", date);

        //Trying to pass a drawable from one activity to another
        newBundle.putInt("DRAWABLE", resId);

        Intent intent = new Intent(this, MainActivity.class);
        intent.putExtras(newBundle);

        setResult(RESULT_OK, intent);

        finish();

        break;

In PicturePickerFragment

// defining listview and using array adapter
    listView = (ListView) convertView.findViewById(R.id.listViewFragment2);
    DrawableAdapter adapter = new DrawableAdapter(getActivity(), rowItems);
    listView.setAdapter(adapter);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

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

            // final int item = images[position];
            Drawable myDrawable = getResources().getDrawable(
                    images[position]);
            Button b = (Button) getActivity()
                    .findViewById(R.id.btn_picture);
            b.setCompoundDrawablesWithIntrinsicBounds(null, myDrawable,
                    null, null);
        }

    });

in MainActivity

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    // Create the adapter to convert the array to views
    EventAdapter adapter = new EventAdapter(this, lstEvents);

    // attach adapter to a list view
    listView = (ListView) findViewById(R.id.listViewFragment);
    listView.setAdapter(adapter);

    if (requestCode == 100) {
        if (resultCode == RESULT_OK) {
            Bundle b = data.getExtras();
            title = b.getString("TITLE");
            time = b.getString("TIME");
            date = b.getString("DATE");

            // retrieving resId, creating drawable

            resId = b.getInt("DRAWABLE");
            Log.e("RESIDRESIED", "DRAWABLE " + resId);
            String resName = getResources().getResourceName(resId);
            Log.e("RESIDRESIED", "DRAWABLE " + resName);
Was it helpful?

Solution

I used the following with my Create Activity to retrieve the drawable from a button

Drawable drawable = btPic.getCompoundDrawables()[1];
Log.e("DRAWABLE", "DrawableId" + drawable);
Bitmap bitmap = ((BitmapDrawable)drawable).getBitmap();

and I used this to put it into a bundle

newBundle.putParcelable("DRAWABLE", bitmap);

This is how I retrieved the bitmap in my MainActivity and converted it to a drawable

Bitmap bitmap = (Bitmap) b.getParcelable("DRAWABLE");
Drawable drawable = new BitmapDrawable(getResources(),bitmap);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top