Tuesday, July 16, 2013

Washing Machines App with images

I managed to improve the application by adding images of the machines to the project.

The complete source code is on git hub https://github.com/bluePlayer/practices/tree/master/WashingMachines

- I added 6 images for each machine brand: LG, Samsung, BSH, Gorenje, Whirlpool and Videocon into the res/drawable-mdpi. You can add in other drawable folders for different screen resolutions but for this small application this is enough.

Note: Images and data for the washing machines are arbitrary and therefore not correct. 

In details.xml I added this ImageView component

<ImageView
        android:id="@+id/machine_image"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="40dp"
        android:contentDescription="@string/machineImageDescrp"
        android:layout_width="230dp"
        android:layout_height="200dp"
        android:src="@drawable/bsh" />


Here it is:


- I added this line in strings.xml for the image view description attribute

<string name="machineImageDescrp">Current machine image</string>

- I dynamically change the image resource id for appropriate machine.

In DetailsActivity.java like this

this.machineImage = (ImageView) findViewById(R.id.machine_image);       this.machineImage.setImageResource(this.setImageId(this.brandName.getText().toString()));      
where setImageId() changes the image id according to the brand name

private int setImageId(String brandNameStr) {
        int result = 0;
        if (brandNameStr.equals(WMConstants.LG)) {
            result = R.drawable.lg;
        }
        if (brandNameStr.equals(WMConstants.SAMSUNG)) {
            result = R.drawable.samsung;
        }
        if (brandNameStr.equals(WMConstants.BSH)) {
            result = R.drawable.bsh;
        }
        if (brandNameStr.equals(WMConstants.WHIRLPOOL)) {
            result = R.drawable.whirlpool;
        }
        if (brandNameStr.equals(WMConstants.VIDEOCON)) {
            result = R.drawable.videocon;
        }
        if (brandNameStr.equals(WMConstants.GORENJE)) {
            result = R.drawable.gorenje;
        }
        return result;
    }


- Now you can see some new class called WMConstants. It is a new class I added for the project to keep track of all constants because using constants is much easier than remembering many strings. And also immune to errors.

Here it is

package com.example.washingmachines;
public class WMConstants {
    public static final String LG = "LG";
    public static final String SAMSUNG = "Samsung";
    public static final String BSH = "BSH";
    public static final String WHIRLPOOL = "Whirlpool";
    public static final String GORENJE = "Gorenje";
    public static final String VIDEOCON = "Videocon";

    public static final String FRONT_TYPE = "front";
    public static final String TOP_TYPE = "top";
    public static final String HYBRID_TYPE = "hybrid";

    public static final String MACHINE_TYPE = "type";
    public static final String MACHINE_BRAND_NAME = "brandName";
    public static final String MACHINE_WEIGHT = "weight";
    public static final String MACHINE_MAX_RPM = "maxRpm";
    public static final String MACHINE_WASH_TEMP = "washTemp";
    public static final String MACHINE_HAS_ENERGY_STAR = "hasEnergyStar";
    public static final String MACHINE_POWER_USAGE = "powerUsage";
}


- Most of the files used are same as in previous tutorials but you can use them all from git hub link above without no problems as it has the latest version of this application.

Here is Details.java



- MainActivity.java



Here are some resulting images:

list of machines

Gorenje Details

Videocon details

Whirlpool details

I have added sample images in the assets folder. You can use them for your project or you can use your own images. They should be 230x200 pixels

No comments:

Post a Comment