Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

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

Dynamic check-box in android. Main part of this demo is manage dynamic click of check-box.


MainActivity.java

package com.java4you.in;

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;

/** * * @author Gopi * */

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("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.java4you"
android:versionCode="1"
android:versionName="1.0" >

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

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.java4you.in.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>


This post first appeared on Java4you - Java Programming Tutorials, Examples,, please read the originial post: here

Share the post

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

×

Subscribe to Java4you - Java Programming Tutorials, Examples,

Get updates delivered right to your inbox!

Thank you for your subscription

×