Wrox Programmer Forums
|
BOOK: Beginning Android Application Development
This is the forum to discuss the Wrox book Beginning Android Application Development by Wei-Meng Lee; ISBN: 978-1-1180-1711-1
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning Android Application Development section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 4th, 2013, 05:26 PM
Registered User
 
Join Date: Apr 2013
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Wallpaper App Code Optimization

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) :

Code:
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

Code:
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

Code:
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:

Code:
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...

Code:
<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 code for all images...

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





Similar Threads
Thread Thread Starter Forum Replies Last Post
Wallpaper Daniel1402 BOOK: Professional Android 4 Application Development 0 April 3rd, 2013 10:08 AM
Python code optimization icopec Python 2 December 13th, 2011 05:13 AM
Creating wallpaper for mobile phones Lucy Intro Programming 1 October 5th, 2006 11:31 AM
App wide code - where to put it? VictorVictor ASP.NET 2.0 Basics 5 May 31st, 2006 02:20 PM
App Code store in DLL Aal Pro VB 6 2 December 23rd, 2004 11:00 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.