[Java for Android App Development] coolbud012:
Hey friend I am making a wallpaper app using bitmap.. I have more than 50 images for this app...The problem I am facing is that I have to add line of code for all the images in the java code as well as xml file...

So please let me know that is there any way to overcome this by adding any loop or something...anything which can reduce my code and I can add more images...

The java code which I am repeating is(just few line which are getting repeated) :

display = (ImageView) findViewById(R.id.IVDisplay);

ImageView image1 = (ImageView) findViewById(R.id.viewImage1);

ImageView image2 = (ImageView) findViewById(R.id.viewImage2);

ImageView image3 = (ImageView) findViewById(R.id.viewImage3);

ImageView image4 = (ImageView) findViewById(R.id.viewImage4);

ImageView image5 = (ImageView) findViewById(R.id.viewImage5);

ImageView image6 = (ImageView) findViewById(R.id.viewImage6);

ImageView image7 = (ImageView) findViewById(R.id.viewImage7);

ImageView image8 = (ImageView) findViewById(R.id.viewImage8);

ImageView image9 = (ImageView) findViewById(R.id.viewImage9);

so on

then after that again in java code itself

toPhone = R.id.IVDisplay;

image1.setOnClickListener(this);

image2.setOnClickListener(this);

image3.setOnClickListener(this);

image4.setOnClickListener(this);

image5.setOnClickListener(this);

image6.setOnClickListener(this);

image7.setOnClickListener(this);

image8.setOnClickListener(this);

image9.setOnClickListener(this);

so on

then in switch case I have to repeat the code again

switch (v.getId()){

case R.id.viewImage1:

display.setImageResource(R.drawable.a);

toPhone = R.drawable.a;

break;

case R.id.viewImage2:

display.setImageResource(R.drawable.b);

toPhone = R.drawable.b;

break;

case R.id.viewImage3:

display.setImageResource(R.drawable.c);

toPhone = R.drawable.c;

break;

case R.id.viewImage4:

display.setImageResource(R.drawable.d);

toPhone = R.drawable.d;

break;

case R.id.viewImage5:

display.setImageResource(R.drawable.e);

toPhone = R.drawable.e;

break;

case R.id.viewImage6:

display.setImageResource(R.drawable.f);

toPhone = R.drawable.f;

break;

case R.id.viewImage7:

display.setImageResource(R.drawable.g);

toPhone = R.drawable.g;

break;

case R.id.viewImage8:

display.setImageResource(R.drawable.h);

toPhone = R.drawable.h;

break;

and so on

case R.id.setBG:

Bitmap bMap = BitmapFactory.decodeStream(getResources().openRawResource(toPhone));

try{

getApplicationContext().setWallpaper(bMap);

}catch(IOException e){

e.printStackTrace();

}

Now coming to the XML parth even there I have to add all the images...

<LinearLayout

android:layout_width="150dp"

android:layout_height="wrap_content"

android:orientation="horizontal" >

<ImageView

android:id="@+id/viewImage1"

android:layout_width="125dp"

android:layout_height="150dp"

android:padding="15dp"

android:src="@drawable/a" />

<ImageView

android:id="@+id/viewImage2"

android:layout_width="125dp"

android:layout_height="150dp"

android:padding="15dp"

android:src="@drawable/b" />

<ImageView

android:id="@+id/viewImage3"

android:layout_width="125dp"

android:layout_height="150dp"

android:padding="15dp"

android:src="@drawable/c" />

<ImageView

android:id="@+id/viewImage4"

android:layout_width="125dp"

android:layout_height="150dp"

android:padding="15dp"

android:src="@drawable/d" />

<ImageView

android:id="@+id/viewImage5"

android:layout_width="125dp"

android:layout_height="150dp"

android:padding="15dp"

android:src="@drawable/e" />

<ImageView

android:id="@+id/viewImage6"

android:layout_width="125dp"

android:layout_height="150dp"

android:padding="15dp"

android:src="@drawable/f" />

<ImageView

android:id="@+id/viewImage7"

android:layout_width="125dp"

android:layout_height="150dp"

android:padding="15dp"

android:src="@drawable/g" />

<ImageView

android:id="@+id/viewImage8"

android:layout_width="125dp"

android:layout_height="150dp"

android:padding="15dp"

android:src="@drawable/h" />

<ImageView

android:id="@+id/viewImage9"

android:layout_width="125dp"

android:layout_height="150dp"

android:padding="15dp"

android:src="@drawable/i" />

and so on...

Friends please dont give negative rating...I have already made this app but there are 51 images and I have to write code for 51...I want to optimize my app so that I can add more images and even I would get to learn a new thing. Hope someone would come and help me with this optimization thing...

Thanks