SIDEBAR
»
S
I
D
E
B
A
R
«
View All Icons in android.R.drawable
Oktober 23rd, 2009 by joecks

Based on an IconViewer I found in the web I desigend this app to show all icons on android.R.drawable. If you need you might change the Class easily.

Icon Viewer App

Icon Viewer App

package de.dailab.iconviewer;

import java.lang.reflect.Field;

import android.app.*;
import android.content.*;
import android.graphics.*;
import android.os.*;
import android.view.*;
import android.view.View.OnClickListener;
import android.widget.*;

public class IconViewer extends Activity {

    
	private int[] IDS;
	
    private int[] getIDS(){
    //	android.R.drawable.ic_menu_zoom
    //	17301504
    //	17301655
    //	android.R.drawable.ic_menu_zoom
    	int[] ids = new int[800];
    	for (int i = 0; i < ids.length; i++) {
			ids[i]= 17301504+i;
		}
    	return ids;
    }

    /**
     * Determines the Name of a Resource,
     * by passing the R.xyz.class and
     * the resourceID of the class to it.
     * @param aClass : like R.drawable.class
     * @param resourceID : like R.drawable.icon
     * @throws IllegalArgumentException if field is not found.
     * @throws NullPointerException if aClass-Parameter is null.
     * <br><br>
     * <b>Example-Call:</b><br>
     * String resName = getResourceNameFromClassByID(R.drawable.class, R.drawable.icon);<br>
     * Then resName would be '<b>icon</b>'.*/
    public String getResourceNameFromClassByID(Class<?> aClass, int resourceID)
                             throws IllegalArgumentException{
         /* Get all Fields from the class passed. */
         Field[] drawableFields = aClass.getFields();
         
         /* Loop through all Fields. */
         for(Field f : drawableFields){
              try {
                   /* All fields within the subclasses of R
                    * are Integers, so we need no type-check here. */
                   
                   /* Compare to the resourceID we are searching. */
                   if (resourceID == f.getInt(null))
                        return f.getName(); // Return the name.
              } catch (Exception e) {
                   e.printStackTrace();
              }
         }
         /* Throw Exception if nothing was found*/
         throw new IllegalArgumentException();
    }
    
    
    private OnClickListener onClicker;
    
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        IDS = getIDS();
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        
        onClicker = new OnClickListener(){

			@Override
			public void onClick(View v) {
				myImageView mv = (myImageView) v;
				String resName = "noname";
				try {
					resName  = getResourceNameFromClassByID(android.R.drawable.class, mv.resId); 
				} catch (IllegalArgumentException e) {
					// TODO: handle exception
				}
				
				String str = "this image is: " + mv.getResId() + "\n And name: "+ resName;
				System.out.println(str);
				Toast.makeText(getBaseContext(), str, Toast.LENGTH_SHORT).show();
			}
        	
        };
        
        AbsoluteLayout layout=new AbsoluteLayout(this);
        layout.setBackgroundColor(Color.rgb(255,255,255));
        setContentView(layout); 

        GridView gridView=new GridView(this);
        setALParams(gridView,0,0);
        gridView.setNumColumns(4);
        gridView.setGravity(Gravity.CENTER);
        gridView.setAdapter(new ImageAdapter(this));
        layout.addView(gridView);
        
        
    }
    
    public class myImageView extends ImageView{

		private int resId;

		public int getResId() {
			return resId;
		}

		public myImageView(Context context) {
			super(context);
			// TODO Auto-generated constructor stub
		}
		
		@Override
		public void setImageResource(int res){
			super.setImageResource(res);
			resId = res;
		}
    	
    }
    
    public class ImageAdapter extends BaseAdapter {
        private Context context;//コンテキスト
		
		

        public ImageAdapter(Context c) {
            context=c;
        }

        public int getCount() {
            return IDS.length;
        }

        public Object getItem(int position) {
            return position;
        }

        public long getItemId(int position) {
            return position;
        }
        
        

        public View getView(int position,View convertView,ViewGroup parent) {
            ImageView imageView;
            if (convertView==null) {
                imageView=new myImageView(context);
                imageView.setLayoutParams(new GridView.LayoutParams(45,45));
                imageView.setAdjustViewBounds(false);
                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                imageView.setPadding(8,8,8,8);
            } else {
                imageView=(ImageView)convertView;
            }
            imageView.setImageResource(IDS[position]);
            imageView.setOnClickListener(onClicker);
            return imageView;
        }
    }

    private static void setALParams(View view,int x,int y) {
        setALParams(view,x,y,
            AbsoluteLayout.LayoutParams.WRAP_CONTENT,
            AbsoluteLayout.LayoutParams.WRAP_CONTENT);
    }
    
    private static void setALParams(View view,int x,int y,int w,int h) {
        view.setLayoutParams(
            new AbsoluteLayout.LayoutParams(w,h,x,y));
    }
}

One Response  
  • K.R. writes:
    März 11th, 201015:23at

    Yes thats rly awesome shit ? ;)


Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This is a captcha-picture. It is used to prevent mass-access by robots. (see: www.captcha.net)

You must read and type the 5 chars within 0..9 and A..F, and submit the form.

  

Oh no, I cannot read this. Please, generate a

»  Substance:WordPress   »  Style:Ahren Ahimsa