clayton33
clayton33

Reputation: 4216

trying to make this code less messy in android, can anyone help?

i know that there must be a simpler way of doing this, i just don't understand how java works well enough, and i am struggling. could someone perhaps point me in the right direction?

ImageButton image = (ImageButton) findViewById(R.id.card1);
    ImageButton image2 = (ImageButton) findViewById(R.id.card2);
    ImageButton image3 = (ImageButton) findViewById(R.id.card3);
    ImageButton image4 = (ImageButton) findViewById(R.id.card4);
    ImageButton image5 = (ImageButton) findViewById(R.id.card5);
    ImageButton image6 = (ImageButton) findViewById(R.id.card6);
    ImageButton image7 = (ImageButton) findViewById(R.id.card7);
    ImageButton image8 = (ImageButton) findViewById(R.id.card8);
    ImageButton image9 = (ImageButton) findViewById(R.id.card9);
    ImageButton image10 = (ImageButton) findViewById(R.id.card10);
    ImageButton image11 = (ImageButton) findViewById(R.id.card11);
    ImageButton image12 = (ImageButton) findViewById(R.id.card12);
    ImageButton image13 = (ImageButton) findViewById(R.id.card13);
    ImageButton image14 = (ImageButton) findViewById(R.id.card14);
    ImageButton image15 = (ImageButton) findViewById(R.id.card15);
    //image.setImageResource(R.drawable.test2);
    Bitmap bMap = BitmapFactory.decodeResource(getResources(), R.drawable.vraag);
    Bitmap bMapScaled = Bitmap.createScaledBitmap(bMap, buttonSize, buttonSize, true);
    Bitmap bMap2 = BitmapFactory.decodeResource(getResources(), R.drawable.vraag);
    Bitmap bMapScaled2 = Bitmap.createScaledBitmap(bMap2, buttonSize, buttonSize, true);
    Bitmap bMap3 = BitmapFactory.decodeResource(getResources(), R.drawable.vraag);
    Bitmap bMapScaled3 = Bitmap.createScaledBitmap(bMap3, buttonSize, buttonSize, true);
    Bitmap bMap4 = BitmapFactory.decodeResource(getResources(), R.drawable.vraag);
    Bitmap bMapScaled4 = Bitmap.createScaledBitmap(bMap4, buttonSize, buttonSize, true);
    Bitmap bMap5 = BitmapFactory.decodeResource(getResources(), R.drawable.vraag);
    Bitmap bMapScaled5 = Bitmap.createScaledBitmap(bMap5, buttonSize, buttonSize, true);
    Bitmap bMap6 = BitmapFactory.decodeResource(getResources(), R.drawable.vraag);
    Bitmap bMapScaled6 = Bitmap.createScaledBitmap(bMap6, buttonSize, buttonSize, true);
    Bitmap bMap7 = BitmapFactory.decodeResource(getResources(), R.drawable.vraag);
    Bitmap bMapScaled7 = Bitmap.createScaledBitmap(bMap7, buttonSize, buttonSize, true);
    Bitmap bMap8 = BitmapFactory.decodeResource(getResources(), R.drawable.vraag);
    Bitmap bMapScaled8 = Bitmap.createScaledBitmap(bMap8, buttonSize, buttonSize, true);
    Bitmap bMap9 = BitmapFactory.decodeResource(getResources(), R.drawable.vraag);
    Bitmap bMapScaled9 = Bitmap.createScaledBitmap(bMap9, buttonSize, buttonSize, true);
    Bitmap bMap10 = BitmapFactory.decodeResource(getResources(), R.drawable.vraag);
    Bitmap bMapScaled10 = Bitmap.createScaledBitmap(bMap10, buttonSize, buttonSize, true);
    Bitmap bMap11 = BitmapFactory.decodeResource(getResources(), R.drawable.vraag);
    Bitmap bMapScaled11 = Bitmap.createScaledBitmap(bMap11, buttonSize, buttonSize, true);
    Bitmap bMap12 = BitmapFactory.decodeResource(getResources(), R.drawable.vraag);
    Bitmap bMapScaled12 = Bitmap.createScaledBitmap(bMap12, buttonSize, buttonSize, true);
    Bitmap bMap13 = BitmapFactory.decodeResource(getResources(), R.drawable.vraag);
    Bitmap bMapScaled13 = Bitmap.createScaledBitmap(bMap13, buttonSize, buttonSize, true);
    Bitmap bMap14 = BitmapFactory.decodeResource(getResources(), R.drawable.vraag);
    Bitmap bMapScaled14 = Bitmap.createScaledBitmap(bMap14, buttonSize, buttonSize, true);
    Bitmap bMap15 = BitmapFactory.decodeResource(getResources(), R.drawable.vraag);
    Bitmap bMapScaled15 = Bitmap.createScaledBitmap(bMap15, buttonSize, buttonSize, true);

    image.setImageBitmap(bMapScaled);
    image2.setImageBitmap(bMapScaled2);
    image3.setImageBitmap(bMapScaled3);
    image4.setImageBitmap(bMapScaled4);
    image5.setImageBitmap(bMapScaled5);
    image6.setImageBitmap(bMapScaled6);
    image7.setImageBitmap(bMapScaled7);
    image8.setImageBitmap(bMapScaled8);
    image9.setImageBitmap(bMapScaled9);
    image10.setImageBitmap(bMapScaled10);
    image11.setImageBitmap(bMapScaled11);
    image12.setImageBitmap(bMapScaled12);
    image13.setImageBitmap(bMapScaled13);
    image14.setImageBitmap(bMapScaled14);
    image15.setImageBitmap(bMapScaled15);

Upvotes: 0

Views: 738

Answers (2)

Abhishek Susarla
Abhishek Susarla

Reputation: 578

private ImageButton image[][]=new image[4][4];

Class c=Class.forName("com.test.R$id");
Integer i = new Integer(c.getField("ToolsbuttonR1C1").getInt(new R.id()));

// in this case let us suppose it is in R1C1 format - row and column

       for(int a=0,p=i;a<4;a++)
          {
                for(int b=0;b<4;b++)
                {
                    image[a][b]=(Button)findViewById(p); 
                    image[a][b].setOnClickListener(this); // an example
                    p++;
                }
        }

This might probably give u an error if the ids are not continuous.. like . if u have inserted rowwise in a tablerow layout in the xml ... the R.id will not be continuous.. so then u have to add another p++ in the first for loop...

this is how u can reduce the code..

Upvotes: 1

Mohamed Nuur
Mohamed Nuur

Reputation: 5655

I would probably use an array to make the code less messy. Try something like this:

// UPDATE: change from String to int
int[] imageIds = new int[15] {
  R.id.card1, R.id.card2, R.id.card3,
  R.id.card4, R.id.card5, R.id.card6,
  R.id.card7, R.id.card8, R.id.card9,
  R.id.card10, R.id.card11, R.id.card12,
  R.id.card13, R.id.card14, R.id.card15
};

// load the source image only once
Bitmap bMap = BitmapFactory.decodeResource(getResources(), R.drawable.vraag);
Bitmap bScaled = Bitmap.createScaledBitmap(bMap, buttonSize, buttonSize, true);

// render them all
ImageButton[] imageButtons = new ImageButton[15];
for(int i = 0; i < imageButtons.size(); i++) {
  ImageButton button = imageButtons[i] = (ImageButton) findViewById(imageIds[i]);
  button.setImageBitmap(bMapScaled);
}

Good luck!

Upvotes: 3

Related Questions