Monday, July 15, 2013

Washing Machines app in android

I managed to create a simple application that uses ListView component and couple of TextView components to store and show data about several washing machines I collected from wikipedia.

Some of the data about machines is arbitrary so not all info is correct. This is just a practice for Android.

strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">WashingMachines</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
    <string name="machine_type">Machine type</string>
    <string name="brand_name">Brand name</string>
    <string name="weight">Weight</string>
    <string name="maxRpm">Max RPM</string>
    <string name="washTemp">Washing temperature</string>
    <string name="hasEnergyStar">Has energy star</string>
    <string name="powerUsage">Power usage</string>
</resources>



activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <ListView
        android:id="@+id/scroll_view"
        android:layout_width="fill_parent"
        android:layout_height="200dp"
        android:orientation="vertical" >
    </ListView>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="140dp"
            android:layout_height="wrap_content"
            android:layout_marginRight="5dp"
            android:text="@string/machine_type" />

        <TextView
            android:id="@+id/machine_type_view"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:hint="@string/machine_type" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="140dp"
            android:layout_height="wrap_content"
            android:layout_marginRight="5dp"
            android:text="@string/brand_name" />

        <TextView
            android:id="@+id/brand_name_view"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:hint="@string/brand_name" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="140dp"
            android:layout_height="wrap_content"
            android:layout_marginRight="5dp"
            android:text="@string/weight" />

        <TextView
            android:id="@+id/weight_view"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:hint="@string/weight" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="140dp"
            android:layout_height="wrap_content"
            android:layout_marginRight="5dp"
            android:text="@string/maxRpm" />

        <TextView
            android:id="@+id/max_rpm_view"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:hint="@string/maxRpm" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="140dp"
            android:layout_height="wrap_content"
            android:layout_marginRight="5dp"
            android:text="@string/washTemp" />

        <TextView
            android:id="@+id/wash_temp_view"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:hint="@string/washTemp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="140dp"
            android:layout_height="wrap_content"
            android:layout_marginRight="5dp"
            android:text="@string/hasEnergyStar" />

        <TextView
            android:id="@+id/has_energy_star_view"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:hint="@string/hasEnergyStar" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="140dp"
            android:layout_height="wrap_content"
            android:layout_marginRight="5dp"
            android:text="@string/powerUsage" />

        <TextView
            android:id="@+id/power_usage_view"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:hint="@string/powerUsage" />
    </LinearLayout>

</LinearLayout>


Helper class: WashingMachine.java

package com.example.washingmachines;

public class WashingMachine {
    private String type; // top, front, hybrid
    private String brandName; // LG, Samsung, BSH, Whirlpool, Videocon, Gorenje
    private int weight; // 60kg, 80kg, 150kg, 220kg, 360kg
    private int maxRpm; // 800, 1200, 1600, 2000
    private int washTemp; // 40C, 36C
    private boolean hasEnergyStar;
    private int powerUsage; // 100kwh, 150kwh, 200kwh

    public WashingMachine() {

    }

    public WashingMachine(String type, String brandName, int weight,
            int maxRpm, int washTemp, boolean hasEnergyStar, int powerUsage) {
        super();
        this.type = type;
        this.brandName = brandName;
        this.weight = weight;
        this.maxRpm = maxRpm;
        this.washTemp = washTemp;
        this.hasEnergyStar = hasEnergyStar;
        this.powerUsage = powerUsage;
    }

    public String getType() {
        return type;
    }

    public String getBrandName() {
        return brandName;
    }

    public String getWeight() {
        return String.valueOf(weight) + " kg";
    }

    public String getMaxRpm() {
        return String.valueOf(maxRpm) + " rpm";
    }

    public String getWashTemp() {
        return String.valueOf(washTemp) + " C";
    }

    public boolean isHasEnergyStar() {
        return hasEnergyStar;
    }

    public String getPowerUsage() {
        return String.valueOf(powerUsage) + " KWh";
    }

}


MainActivity.java

package com.example.washingmachines;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity {
    private ListView machines = null;
    private List<WashingMachine> listOfMachines = new ArrayList<WashingMachine>();
    private ArrayAdapter<String> adapter = null;
    private List<String> machinesList = new ArrayList<String>();
    private TextView type = null;
    private TextView brandName = null;
    private TextView weight = null;
    private TextView maxRmp = null;
    private TextView washTemp = null;
    private TextView hasEnergyStar = null;
    private TextView powerUsage = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
    }

    private void initView() {

        this.machines = (ListView) findViewById(R.id.scroll_view);
       
        WashingMachine wm1 = new WashingMachine("front", "LG", 65,
                1200, 41, false, 100);
        this.listOfMachines.add(wm1);
       
        WashingMachine wm2 = new WashingMachine("top", "Samsung", 73,
                800, 60, false, 90);
        this.listOfMachines.add(wm2);
       
        WashingMachine wm3 = new WashingMachine("hybrid", "BSH", 52,
                1650, 36, true, 70);
        this.listOfMachines.add(wm3);
       
        WashingMachine wm4 = new WashingMachine("top", "Whirlpool", 105,
                1400, 56, false, 150);
        this.listOfMachines.add(wm4);
       
        WashingMachine wm5 = new WashingMachine("front", "Gorenje", 360,
                2000, 70, false, 300);
        this.listOfMachines.add(wm5);
       
        WashingMachine wm6 = new WashingMachine("hybrid", "Videocon", 300,
                2000, 85, false, 310);
        this.listOfMachines.add(wm6);
       
        this.machinesList.add(this.listOfMachines.get(0).getBrandName());
        this.machinesList.add(this.listOfMachines.get(1).getBrandName());
        this.machinesList.add(this.listOfMachines.get(2).getBrandName());
        this.machinesList.add(this.listOfMachines.get(3).getBrandName());
        this.machinesList.add(this.listOfMachines.get(4).getBrandName());
        this.machinesList.add(this.listOfMachines.get(5).getBrandName());
       
        this.type = (TextView) findViewById(R.id.machine_type_view);
        this.brandName = (TextView) findViewById(R.id.brand_name_view);
        this.weight = (TextView) findViewById(R.id.weight_view);
        this.maxRmp = (TextView) findViewById(R.id.max_rpm_view);
        this.washTemp = (TextView) findViewById(R.id.wash_temp_view);
        this.hasEnergyStar = (TextView) findViewById(R.id.has_energy_star_view);
        this.powerUsage = (TextView) findViewById(R.id.power_usage_view);
       
        this.adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, this.machinesList);
        this.machines.setAdapter(adapter);
       
        this.machines.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                type.setText(listOfMachines.get(arg2).getType());
                brandName.setText(listOfMachines.get(arg2).getBrandName());
                weight.setText(listOfMachines.get(arg2).getWeight());
                maxRmp.setText(listOfMachines.get(arg2).getMaxRpm());
                washTemp.setText(listOfMachines.get(arg2).getWashTemp());
                hasEnergyStar.setText(listOfMachines.get(arg2).isHasEnergyStar() ? "yes" : "no");
                powerUsage.setText(listOfMachines.get(arg2).getPowerUsage());
               
            }
        });

       

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}


Result image

result

No comments:

Post a Comment