How to obtain Drawables Programatically?

KradasboKradasbo CLMember ✭✭
edited September 2017 in Xamarin.Android

I'm trying to obtain the Drawables programatically so to bring pictures to the rows of a listView programatically, the listView is loaded from a DB in a Web-Server but it doesn't contain the images, however, the options than the listView shows are equal for all users so i can put the images from the app side without a problem.

I don't have control over the db, nor the web-server in depth (just enough to connect the Mobile App with the Web-Server, and that's it).

Things than i already try: (or other posible answers to this question)

  • Populate a Dictionary<string, drawable> with ALL the Drawables (they are not that many in this case and app) and make the link in the adapter using it.
    R: I can't implement it because i don't know how to call every single one of the drawables programatically (for example, how to load a for) so to, dinamically populate the Dictionary and link it with a string in the dictionary.

  • Call it directly from Drawables and asign it at runtime directly on the ListView adapter
    R: I don't know how to call directly at runtime a Drawable using him's name, and non-deprecated functions, so i would need to know how to do it.

This is the adapter of the listView i'm modifing:

public override View GetView(int position, View convertView, ViewGroup parent)
            View row = convertView;

            if (row == null)
                row = LayoutInflater.From(mContext).Inflate(Resource.Layout.listview_row, null, false);

            //TextView txtName = row.FindViewById<TextView>(Resource.Id.txtName);
            Refractored.Controls.CircleImageView imgCircleAccion = row.FindViewById<Refractored.Controls.CircleImageView>(Resource.Id.imgCircleAccion);
            TextView txtDeparment = row.FindViewById<TextView>(Resource.Id.txtDepartment);

            //txtName.Text = mItems[position].ID.ToString();

            imgCircleAccion.SetImageDrawable(this.mContext.Resources.GetDrawable("a145", "drawable", PackageName)) Resource.Drawable mItems[position].Nombre.Trim().RemoveDiacritics()):
            txtDeparment.Text = mItems[position].Nombre;

            return row;

Yeah, i know, there is broken code on it, that is because i can't decide what to place there, the function to find a Drawable at runtime using a string was the thing i can't find, in any case, if i can do that, i can place that value inside SetImageDrawable() and everything should work like a charm.

In any case, Any of the two ways of answer the question would be cool answers for it

Thanks in advance


  • KrayemKrayem LYMember ✭✭

    So, you want to place drawable in imageview only by drawable name?
    try this code in your listView adapter

    int mydrw;
        mydrw = (int)typeof(Resource.Drawable).GetField("drwName").GetValue(null);
        mydrw = Resource.Drawable.nophoto; // use this when no drawable matchs
  • KradasboKradasbo CLMember ✭✭

    thanks you @Krayem , that fix the issue, it works like a charm!

    I forget to mark this question as question, but you have my thanks and a like for it, otherwise, if this was tagged "question" i would mark it like the right answer.


Sign In or Register to comment.