Showing posts with label Checkbox. Show all posts
Showing posts with label Checkbox. Show all posts

Monday, September 2, 2013

Android Dynamic Check-box Example | Check-box Demo in Android | Manage click in dynamic check-box in android

Hello Friends!
Today I am sharing very important code for dynamic check-box in android. Main part of this demo is manage dynamic click of check-box. Just copy paste below code and modify according your need.


MainActivity.java


package com.manish.checkboxdemo;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;

import com.manish.checkboxdemo.R;

/**
 * 
 * @author manish
 * 
 */

public class MainActivity extends Activity {
 LinearLayout linearMain;
 CheckBox checkBox;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  linearMain = (LinearLayout) findViewById(R.id.linearMain);
  /**
   * create linked hash map for store item you can get value from database
   * or server also
   */
  LinkedHashMap<String, String> alphabet = new LinkedHashMap<String, String>();
  alphabet.put("1", "Apple");
  alphabet.put("2", "Boy");
  alphabet.put("3", "Cat");
  alphabet.put("4", "Dog");
  alphabet.put("5", "Eet");
  alphabet.put("6", "Fat");
  alphabet.put("7", "Goat");
  alphabet.put("8", "Hen");
  alphabet.put("9", "I am");
  alphabet.put("10", "Jug");

  Set<?> set = alphabet.entrySet();
  // Get an iterator
  Iterator<?> i = set.iterator();
  // Display elements
  while (i.hasNext()) {
   @SuppressWarnings("rawtypes")
   Map.Entry me = (Map.Entry) i.next();
   System.out.print(me.getKey() + ": ");
   System.out.println(me.getValue());

   checkBox = new CheckBox(this);
   checkBox.setId(Integer.parseInt(me.getKey().toString()));
   checkBox.setText(me.getValue().toString());
   checkBox.setOnClickListener(getOnClickDoSomething(checkBox));
   linearMain.addView(checkBox);
  }

 }

 View.OnClickListener getOnClickDoSomething(final Button button) {
  return new View.OnClickListener() {
   public void onClick(View v) {
    System.out.println("*************id******" + button.getId());
    System.out.println("and text***" + button.getText().toString());
   }
  };
 }

}

activity_main.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/linearMain"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

</LinearLayout>

AndroidManifest.xml


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.manish.checkboxdemo"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="16" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.manish.checkboxdemo.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Thanks!