Sunday, August 19, 2012

Android - Checkbox Example


Below is example of Checkbox element, it can be used to provide features to user to select multiple items.

I used Eclipse as development tool and tested the sample in Emulator for Android 4.0.3.

There are lot of resources are used to develop android app. but for this example, we are using three basis components like code base file, layout file and string file.

Layout file (.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">
    <CheckBox 
        android:id="@+id/chkbanana"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/chkbanana"
        />
    <CheckBox 
        android:id="@+id/chkorange"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/chkorange"
        />
    
    <CheckBox 
        android:id="@+id/chkapple"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/chkapple"
        />
        
<Button 
   android:id="@+id/btnok"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="@string/btnok"
   />
    
</LinearLayout>


String file (.XML):
<resources>

    <string name="app_name">CheckBoxExample</string>
    <string name="tvmessage"></string>
    <string name="menu_settings">Settings</string>
    <string name="title_activity_checkbox_example">CheckboxExample</string>
<string name="chkbanana">Banana</string>
<string name="chkapple">Apple</string>
<string name="chkorange">Orange</string>
<string name="btnok">OK</string>
</resources>


Code base file(.JAVA):

package com.example.checkboxexample;

import android.os.Bundle;
import android.app.Activity;
import android.text.InputFilter.LengthFilter;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;

public class CheckboxExample extends Activity {

private CheckBox chkBanana, chkOrange, chkApple;
private Button btnOk;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_checkbox_example);
        
        chkBanana = (CheckBox) findViewById(R.id.chkbanana);
        chkOrange = (CheckBox) findViewById(R.id.chkorange);
        chkApple = (CheckBox) findViewById(R.id.chkapple);
        btnOk = (Button) findViewById(R.id.btnok);
        onClickOkButton();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_checkbox_example, menu);
        return true;
    }
    
    public void onClickOkButton(){
   
    btnOk.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
String sStr = new String();
if (chkBanana.isChecked()){
sStr += "Banana";
}
if (chkOrange.isChecked()){
if (sStr.length()>0)
sStr +=  ", ";
sStr += "Orange";
}
if (chkApple.isChecked()){
if (sStr.length()>0)
sStr += ", ";
sStr += "Apple";
}
if (sStr.length()>0){
sStr += " selected.";
}
Toast.makeText(getApplicationContext(), sStr, Toast.LENGTH_LONG).show();
}
});
    }
    
    
}


Screenshot:


No comments:

Post a Comment