Thursday, May 31, 2012

Basic Android Training: Using CheckBox and ImageView

Basic Android Training: Using CheckBox and ImageView

Create android project MyFavouriteFruitApp.
Copy three images named as apple, orange and banana to res/drawable-hdpi directory.
Then copy the following files:
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="instruction">Please pick your favourite fruit:</string>
    <string name="app_name">FavouriteFruitApp</string>
    <string name="chkAppleLabel">Apple</string>
    <string name="chkOrangeLabel">Orange</string>
    <string name="chkBananaLabel">Banana</string>
    <string name="chkClearLabel">Clear</string>
</resources>

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/instruction" />

    <CheckBox
        android:id="@+id/chkApple"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/chkAppleLabel" />

    <CheckBox
        android:id="@+id/chkOrange"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/chkOrangeLabel" />

    <CheckBox
        android:id="@+id/chkBanana"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/chkBananaLabel" />


    <CheckBox
        android:id="@+id/chkClear"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/chkClearLabel" />


    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/banana" />

</LinearLayout>

package com.kent;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.ImageView;

public class FavouriteFruitAppActivity extends Activity implements OnClickListener{
           
            private CheckBox chkApple, chkOrange;
           
    @Override
    public void onCreate(Bundle savedInstanceState) {
           
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        chkApple = (CheckBox) findViewById(R.id.chkApple);
        chkApple.setOnClickListener(this);
       
        chkOrange = (CheckBox) findViewById(R.id.chkOrange);
        chkOrange.setOnClickListener(this);
       
       ((CheckBox) findViewById(R.id.chkBanana)).setOnClickListener(this);
       ((CheckBox) findViewById(R.id.chkClear)).setOnClickListener(this);
    }


    public void onClick(View v) {
           
/*          CheckBox chkTemp = (CheckBox) v;
            chkTemp.getText();
           
            if (chkTemp.isChecked()) {
                                   
                        Toast.makeText(FavouriteFruitAppActivity.this,
                        "You have selected " + chkTemp.getText(),
                        Toast.LENGTH_SHORT).show();
            }*/
           
            ImageView imgView = imgView = (ImageView) findViewById(R.id.imageView1);
           
            switch(v.getId()) {
           
           
            case R.id.chkApple:
                                    // Change the ImageView to Apple photo
                        imgView.setImageResource(R.drawable.apple);
                                    break;
                                   
                        case R.id.chkOrange:
                                    // Change the ImageView to Orange photo
                        imgView.setImageResource(R.drawable.orange);
                                    break;
                                   
                        case R.id.chkBanana:
                                    // Change the ImageView to Banana photo
                        imgView.setImageResource(R.drawable.banana);
                                    break;
                                   
                        case R.id.chkClear:
                                    // Clear the existing photo
                                    imgView.setImageResource(android.R.color.transparent);
                                    break;
            }
    
    }  
}


No comments:

Post a Comment