CheckBox Example in Android |Android App Development With AndroidCource

Monday 21 August 2017

CheckBox Example in Android

CheckBoxActivity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:id="@+id/activity_check_box"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:paddingBottom="@dimen/activity_vertical_margin"
                android:paddingLeft="@dimen/activity_horizontal_margin"
                android:paddingRight="@dimen/activity_horizontal_margin"
                android:paddingTop="@dimen/activity_vertical_margin"
                tools:context="com.bhaumik.programmingstudy.CheckBoxActivity">
    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Check Box Example"
            android:textSize="25sp"
            android:textColor="#000000"
            android:id="@+id/tv_def"/>

    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Select Programming Language"
            android:textSize="18sp"
            android:textColor="#000000"
            android:layout_below="@+id/tv_def"
            android:layout_marginTop="40sp"
            android:id="@+id/tv_selectDef"/>
    <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Android"
            android:layout_below="@+id/tv_selectDef"
            android:textSize="20sp"
            android:layout_marginTop="25sp"
            android:layout_marginLeft="50sp"
            android:id="@+id/chk_android"/>
    <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Java"
            android:layout_below="@+id/chk_android"
            android:textSize="20sp"
            android:layout_marginTop="10sp"
            android:layout_marginLeft="50sp"
            android:id="@+id/chk_java"/>
    <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Python"
            android:layout_below="@+id/chk_java"
            android:textSize="20sp"
            android:layout_marginLeft="50sp"
            android:layout_marginTop="10sp"
            android:id="@+id/chk_python"/>
    <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Select Language"
            android:textAllCaps="false"
            android:textSize="20sp"
            android:layout_below="@+id/chk_python"
            android:layout_marginTop="20sp"
            android:layout_marginLeft="30sp"
            android:id="@+id/btn_select"/>
</RelativeLayout>
CheckBoxActivity.java
package com.bhaumik.programmingstudy;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;

public class CheckBoxActivity extends AppCompatActivity {

    CheckBox android,java,python;
    Button select;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_check_box);

        android = (CheckBox)findViewById(R.id.chk_android);
        java = (CheckBox)findViewById(R.id.chk_java);
        python = (CheckBox)findViewById(R.id.chk_python);

        select = (Button)findViewById(R.id.btn_select);

        select.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                StringBuilder sb = new StringBuilder();
                sb.append("Your Select Language : \n");

                if(android.isChecked())
                {
                    sb.append("Android \n");
                }
                if(java.isChecked())
                {
                    sb.append("Java \n");
                }
                if(python.isChecked())
                {
                    sb.append("Python");
                }
                Toast.makeText(getBaseContext(),sb.toString(), Toast.LENGTH_SHORT).show();
            }
        });
    }

}

0 comments:

Post a Comment