0

I am working on an Android project that follows the tutorial at this youtube link: https://www.youtube.com/watch?v=YCHNAi9kJI4 . It is an animated listView deletion.

I have an image setup within part of the opaque_text_view.xml file:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/image_text_vew_list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/tv_background_with_divider"
    android:textAppearance="?android:attr/textAppearanceListItemSmall"
    android:gravity="center_vertical"
    android:drawableLeft="@drawable/ic_launcher"   <--------------------------
    android:paddingStart="?android:attr/listPreferredItemPaddingStart"
    android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
    android:minHeight="?android:attr/listPreferredItemHeightSmall"
/>

Which comes up perfectly, but, I would like to change the image dynamically so that it will adjust with gmail-like icons that reflect the letters and colors of the mail itself. I already have the code for setting an image to look like the Gmail client which I obtained here: Colored boxed with letters a la Gmail .

The issue I am having is that the code to adjust the opaque_text_view.xml is only found in an adapter:

public class ListViewRemovalAnimation extends Activity {

    StableArrayAdapter mAdapter;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_view_deletion);

        mBackgroundContainer = (BackgroundContainer) findViewById(R.id.listViewBackground);

        //Set the adapter here
        --> mAdapter = new StableArrayAdapter(this,R.layout.opaque_text_view, list, mTouchListener);** <--
        mListView.setAdapter(mAdapter);
}

The StableArrayAdapter is its own class and is defined like this:

public class StableArrayAdapter extends ArrayAdapter<String> {
...
}

I basically have to edit the picture from within that StableArrayAdapter, but as it does not extend to an activity, I can't find a way to do it. I tried working in the code from this How to programmatically set drawableLeft on Android button? link, but same problem.

To phrase it another way, how do I change the image in the opaque_text_view (drawableleft) to update it to this:

final LetterTileProvider tileProvider = new LetterTileProvider(this);
final Bitmap letterTile = tileProvider.getLetterTile("name", "key", tileSize, tileSize);
//SET BITMAP IMAGE HERE!!!

without the benefit of an activity. Anyone have any recommendations?

Community
  • 1
  • 1
PGMacDesign
  • 6,092
  • 8
  • 41
  • 78
  • I'm not clear on what the specific problem is. Are you asking how to instantiate the LetterTileProvider in the Adapter? Or how to set the Drawable on the TextView? Or something else? – Mike M. Dec 13 '14 at 20:25
  • Apologies Mike! Basically, how do I set the drawable in the text view. I'm able to instantiate the LetterTileProvider, I just can't figure out how to pass it to the draw able on the text view. – PGMacDesign Dec 13 '14 at 20:51

1 Answers1

1

I'm not sure I understand as there isn't much code provided. The full Activity code, ArrayAdapter code and the code for the class of your items would have helped.

In the activity onCreate you want something like:

    yourListView.setAdapter(yourAdapter);
    yourListView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
            YourItemClass itemClicked = view.yourGetData();
            yourAdapter.changeIcon(itemClicked);
        }
    });

In the ArrayAdapter you want to create a function like:

public void changeIcon(YourItemClass item) {
    YourItemClass.setClicked(true);
    notifyDataSetChanged();
}

Also in your ArrayAdapter in getView(int position, View convertView, ViewGroup parent) add:

if (yourArray.get(position).isClicked()) {
    yourTextView.setCompoundDrawables(getContext().getResources().getDrawable( R.drawable.your_clicked_icon), null, null, null);
}

In your item class you would then need some way of storing what type it is. For example, as coded here you need an isClicked boolean with setClicked() and isClicked() methods.

TTransmit
  • 3,270
  • 2
  • 28
  • 43