Wednesday 19 February 2014

How To Create Check Box Static And Dynamic With Reset Functionality

Static Creation

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">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="@dimen/padding_medium"
        android:text="Static check box creation"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="20dp"
        android:textSize="20sp"
        android:textColor="#336600"
        android:textStyle="bold"
         />

    <CheckBox
        android:id="@+id/checkBox1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="CheckBox 1"
        android:layout_marginLeft="40dp"/>

    <CheckBox
        android:id="@+id/checkBox2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="CheckBox 2"
        android:layout_marginLeft="40dp"/>

    <CheckBox
        android:id="@+id/checkBox3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="CheckBox 3"
        android:layout_marginLeft="40dp"/>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="20dp"
        android:text="Long press to reset checkbox" />

</LinearLayout>


CheckBoxActivity.java

package com.checkboxactivity;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;

public class CheckBoxActivity extends Activity implements OnClickListener{
    CheckBox checkBox1,checkBox2,checkBox3;
    Button btn;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
      
        checkBox1=(CheckBox)findViewById(R.id.checkBox1);
        checkBox2=(CheckBox)findViewById(R.id.checkBox2);
        checkBox3=(CheckBox)findViewById(R.id.checkBox3);
        btn=(Button)findViewById(R.id.button1);
        checkBox1.setOnClickListener(this);
        checkBox2.setOnClickListener(this);
        checkBox3.setOnClickListener(this);
        btn.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
checkBox1.setChecked(false);
checkBox2.setChecked(false);
checkBox3.setChecked(false);
Toast.makeText(getBaseContext(), "AllCheck box are unchecked", Toast.LENGTH_LONG).show();
return true;
}
});
        
    
    }
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v==checkBox1){
Toast.makeText(this, "Check box 1 is checked", Toast.LENGTH_LONG).show();
}

if(v==checkBox2){
Toast.makeText(this, "Check box 2 is checked", Toast.LENGTH_LONG).show();
}

if(v==checkBox3){
Toast.makeText(this, "Check box 3 is checked", Toast.LENGTH_LONG).show();
}

}

   
    
}

AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.checkboxactivity"
    android:versionCode="1"
    android:versionName="1.0" >

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

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:screenOrientation="portrait"
            android:name=".CheckBoxActivity"
            android:label="@string/title_activity_check_box" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>







No comments:

Post a Comment