0

Evening,

I came across this very useful post and I have everything looking good except for sizing... Post: how to add icon in alert dialog before each item?

There was a comment about setBounds and then invoking a different setCompoundDrawables.

Here is what I have

...
switch(currently_selected){
...
    case 3:

                final Item[] items2 = {
                    new Item("Company 1", R.drawable.c_1),
                    new Item("Company 2", R.drawable.c_2),
                    new Item("Company 3", R.drawable.c_3),
                    new Item("Company 4", R.drawable.c_4),
                    new Item("Company 5", R.drawable.c_5)//no icon for this one
                };

                ListAdapter adapter = new ArrayAdapter<Item>(
                                                MainPortal.this,
                                                android.R.layout.select_dialog_item,
                                                android.R.id.text1,
                                                items2){
                        public View getView(int position, View convertView, ViewGroup parent) {
                            //User super class to create the View
                            View v = super.getView(position, convertView, parent);
                            TextView tv = (TextView)v.findViewById(android.R.id.text1);

                            //Put the image on the TextView

                            tv.setCompoundDrawablesWithIntrinsicBounds(items2[position].icon, 0, 0, 0);

                            //Add margin between image and text (support various screen densities)
                            int dp5 = (int) (5 * getResources().getDisplayMetrics().density + 0.5f);
                            tv.setCompoundDrawablePadding(dp5);

                            return v;
                        }
                    };

                AlertDialog.Builder builder1 = new AlertDialog.Builder(MainPortal.this);
                builder1.setTitle("Please select the Module you wish to load and use");
                builder1.setAdapter(adapter, new DialogInterface.OnClickListener() {
                //builder1.setItems(items1, new DialogInterface.OnClickListener() { // comment out this line to try to use the adapter
                    public void onClick(DialogInterface dialog, int item) {
                        Toast.makeText(getApplicationContext(), items1[item], Toast.LENGTH_LONG).show();
                    }
                }).create().show();

                break;
...
}//end switch

so this works very well and after 5-6 hours I got it working.

Though I'm too new at this to understand what some of these things are. My company 1-5, will have the option to load a company header to kind of display alongside the name.

I'd like to scale these down while maintaining their aspect ratios if possible. thus,

int max_allowed = 50;
if(getWidth() > getHeight){
    scaleTo(50, (getWidth()/getHeight()*50));
}else if(getWidth() < getHeight){
    scaleTo((getWidth()/getHeight()*50), 50) ;
}else{
    scaleTo(50, 50);
}

Any and help towards the proper way to set the bounds would be greatly appreciated, I just don't understand the items2[position].icon being an integer and referencing or converting a drawable from it.

thanks, Joshua

Community
  • 1
  • 1
jc.021286
  • 220
  • 1
  • 4
  • 15

1 Answers1

0

I managed to find the answer after a fair bit of digging.

The changes required were as follows in order to resize. After which a simple if statement will suffice to adjust company banner sizes

final Drawable image;
Resources res = getResources();
image = res.getDrawable(items2[position].icon);
image.setBounds(0, 0, 50, 50);
tv.setCompoundDrawables(image, null, null, null);

//tv.setCompoundDrawablesWithIntrinsicBounds(image,0,0,0);//items2[position].icon, 0, 0, 0);
jc.021286
  • 220
  • 1
  • 4
  • 15