Breaking

Thursday, 20 December 2018

CheckBox Tutorial With Example In Android Studio | Android Solution | CheckBox Tutoriyal


CheckBox Tutorial With Example In Android Studio




In Android, CheckBox is a kind of two state catch either unchecked or checked in Android. Or on the other hand you can say it is a sort of on/off switch that can be flipped by the clients. You should utilize checkbox while exhibiting a gathering of selectable alternatives to clients that are not totally unrelated. CompoundButton is the parent class of CheckBox class.


In android there is a great deal of use of check box. For example, to take review in Android application we can list couple of choices and enable client to pick utilizing CheckBox. The client will just checked these checkboxes as opposed to type their very own choice in EditText. Another exceptionally normal utilization of CheckBox is as recollect me choice in Login shape.

CheckBox code in XML:


<CheckBox
android:id="@+id/simpleCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"



Important Note: You can check the present condition of a check box automatically by utilizing isChecked() method. This method restores a Boolean esteem either obvious or false, on the off chance that a check box is checked, it returns genuine else it returns false. The following is an example code in which we checked the present condition of a check box.

CheckBox simpleCheckBox = (CheckBox) findViewById(R.id.simpleCheckBox);
 
//check current state of a check box (true or false)
Boolean checkBoxState = simpleCheckBox.isChecked();

Attributes of CheckBox:


Presently allows we talk about important attributes that causes us to design a check confine XML document (layout).





1.id: id is an ascribe used to remarkably identify a check box. Beneath we set the id of a picture catch.

<CheckBox
android:id="@+id/simpleCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Simple CheckBox"/>

2. checked: checked is a trait of check box used to set the present condition of a check box. The esteem ought to be valid or false where genuine demonstrates the checked state and false shows unchecked condition of a check box. The default estimation of checked quality is false. We can likewise set the present state automatically.



The following is an example code in which we set genuine incentive for checked characteristic that sets the present state to checked.

<CheckBox
    android:id="@+id/simpleCheckBox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Simple CheckBox" 
    android:checked="true"/> <!--set the current state of the check box-->

Setting Current State Of CheckBox In Java Class:


// initiate a check box
CheckBox simpleCheckBox = (CheckBox) findViewById(R.id.simpleCheckBox);
 
// set the current state of a check box
simpleCheckBox.setChecked(true);

3. gravity: The gravity characteristic is a discretionary ascribe which is utilized to control the arrangement of the content in CheckBox like left, right, focus, top, base, center_vertical, center_horizontal and so forth.

Beneath we set the privilege and center_vertical gravity for the content of a check box.

<CheckBox
    android:id="@+id/simpleCheckBox"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Simple CheckBox"
    android:checked="true"
    android:gravity="right|center_vertical"/>

4. text: text ascribe is utilized to set the text in a check box. We can set the text in xml and in addition in the java class.

The following is the example code with clarification included in which we set the "Text Attribute Of Check Box" for a check box.

<CheckBox
    android:id="@+id/simpleCheckBox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:checked="true"
    android:text="Text Attribute Of Check Box"/> 

Setting text in CheckBox In Java class:

// initiate check box
CheckBox simpleCheckBox = (CheckBox) findViewById(R.id.simpleCheckBox);
 
// displayed text of the check box
simpleCheckBox.setText("Text Attribute Of Check Box");

5. textColor: textColor ascribe is utilized to set the text shade of a check box. Shading esteem is in type of "#argb", "#rgb", "#rrggbb", or "#aarrggbb".

Beneath we set the red shading for the showed text of a check box.

<CheckBox
    android:id="@+id/simpleCheckBox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Text is Red Color"
    android:textColor="#f00" 
    android:checked="true"/>

Setting textColor in CheckBox In Java class:

//initiate the checkbox
CheckBox simpleCheckBox = (CheckBox) findViewById(R.id.simpleCheckBox);
 
//red color for displayed text
simpleCheckBox.setTextColor(Color.RED);

6. textSize: textSize ascribe is utilized to set the span of text of a check box. We can set the text measure in sp(scale independent pixel) or dp(density pixel).

The following is the example code in which we set the 20sp size for the text of a check box.

<CheckBox
    android:id="@+id/simpleCheckBox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Text size Attribute Of Check Box"
    android:textColor="#00f"
    android:checked="false"
    android:textSize="20sp"/><!--set Text Size of text in CheckBox-->

Setting Text Size in CheckBox In Java class:

CheckBox simpleCheckBox = (CheckBox) findViewById(R.id.simpleCheckBox);
//set 20sp displayed text size
simpleCheckBox.setTextSize(20);

7. textStyle: textStyle ascribe is utilized to set the text style of the text of a check box. The conceivable text styles are strong, italic and typical. In the event that we have to utilize at least two styles for a text see then "|" administrator is utilized for that.

Beneath we set the striking and italic text styles for text of a check box.

<CheckBox
    android:id="@+id/simpleCheckBox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Text Style Attribute Of Check Box"
    android:textColor="#44f"
    android:textSize="20sp"
    android:checked="false"
    android:textStyle="bold|italic"/>

8. background: background ascribe is utilized to set the background of a check box. We can set a shading or a drawable in the background of a check box.

Underneath we set the dark shading for the background, white shading for the showed text of a check box.

<CheckBox
    android:id="@+id/simpleCheckBox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Text size Attribute Of Check Box"
    android:textColor="#fff"
    android:textSize="20sp"
    android:textStyle="bold|italic"
    android:checked="true"
    android:background="#000" />

Setting Background in CheckBox In Java class:

CheckBox simpleCheckBox = (CheckBox) findViewById(R.id.simpleCheckBox);
// set background in CheckBox
simpleCheckBox.setBackgroundColor(Color.BLACK);

9. padding: padding credit is utilized to set the padding from left, right, best or base.
* paddingRight :set the padding from the correct side of the check box.
* paddingLeft :set the padding from the left side of the check box.
* paddingTop :set the padding from the best side of the check box.
* paddingBottom :set the padding from the base side of the check box.

Padding :set the padding from the all side's of the check box.

The following is the example code of padding where we set the 30dp padding from all the side's of the check box.

<CheckBox
    android:id="@+id/simpleCheckBox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Padding Attribute Of Check Box"
    android:textColor="#44f" 
    android:textSize="20sp"
    android:textStyle="bold|italic"
    android:checked="false"
    android:padding="30dp"/>

Example of CheckBox In Android Studio:


The following is the example of CheckBox in Android, in which we show five check boxes using background and different attributes we examines prior in this post. Each check box speaks to an alternate subject name and at whatever point you tap on a check box then text of that checked check enclose is shown a toast. The following is the final yield, code and well ordered clarification of the example:



Stage 1: Create another task and name it CheckBoxExample



In this progression we make another venture in android studio by filling all the important subtleties of the application like application name, bundle name, programming interface forms and so forth.

Select File -> New -> New Project and Fill the forms and click "Finish" button.
Stage 2: Now Open res - > layout - > activity_main.xml (or) main.xml and include the following code:
In this progression we open a xml document and include the code for displaying five one TextView and check boxes.
<RelativeLayout 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: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=".MainActivity">
 
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Select Your Programming language: "
        android:textColor="#f00"
        android:textSize="20sp"
        android:textStyle="bold" />
 
    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:background="#e0e0e0"
        android:orientation="vertical">
 
        <CheckBox
            android:id="@+id/androidCheckBox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:checked="false"
            android:padding="20dp"
            android:text="@string/android"
            android:textColor="#44f"
            android:textSize="20sp"
            android:textStyle="bold|italic" />
 
        <CheckBox
            android:id="@+id/javaCheckBox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:checked="false"
            android:padding="20dp"
            android:text="@string/java"
            android:textColor="#f44"
            android:textSize="20sp"
            android:textStyle="bold|italic" />
 
        <CheckBox
            android:id="@+id/phpCheckBox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:checked="false"
            android:padding="20dp"
            android:text="@string/php"
            android:textColor="#444"
            android:textSize="20sp"
            android:textStyle="bold|italic" />
 
        <CheckBox
            android:id="@+id/pythonCheckBox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:checked="false"
            android:padding="20dp"
            android:text="@string/python"
            android:textColor="#888"
            android:textSize="20sp"
            android:textStyle="bold|italic" />
 
        <CheckBox
            android:id="@+id/unityCheckBox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:checked="false"
            android:padding="20dp"
            android:text="@string/unity"
            android:textColor="#101010"
            android:textSize="20sp"
            android:textStyle="bold|italic" />
    </LinearLayout>
 
</RelativeLayout>
Stage 3: Now Open application - > java-> bundle - > MainActivity.java
In this progression we add the code to initiate the check boxes we made. And afterward we perform click occasion on catch and show the text for chose check boxes using a toast.
package example.abhiandriod.checkboxexample;
 
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;
 
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
 
    CheckBox android, java, python, php, unity3D;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // initiate views
        android = (CheckBox) findViewById(R.id.androidCheckBox);
        android.setOnClickListener(this);
        java = (CheckBox) findViewById(R.id.javaCheckBox);
        java.setOnClickListener(this);
        python = (CheckBox) findViewById(R.id.pythonCheckBox);
        python.setOnClickListener(this);
        php = (CheckBox) findViewById(R.id.phpCheckBox);
        php.setOnClickListener(this);
        unity3D = (CheckBox) findViewById(R.id.unityCheckBox);
        unity3D.setOnClickListener(this);
    }
 
    @Override
    public void onClick(View view) {
 
        switch (view.getId()) {
            case R.id.androidCheckBox:
                if (android.isChecked())
                    Toast.makeText(getApplicationContext(), "Android", Toast.LENGTH_LONG).show();
                break;
            case R.id.javaCheckBox:
                if (java.isChecked())
                    Toast.makeText(getApplicationContext(), "Java", Toast.LENGTH_LONG).show();
                break;
            case R.id.phpCheckBox:
                if (php.isChecked())
                    Toast.makeText(getApplicationContext(), "PHP", Toast.LENGTH_LONG).show();
                break;
            case R.id.pythonCheckBox:
                if (python.isChecked())
                    Toast.makeText(getApplicationContext(), "Python", Toast.LENGTH_LONG).show();
                break;
            case R.id.unityCheckBox:
                if (unity3D.isChecked())
                    Toast.makeText(getApplicationContext(), "Unity 3D", Toast.LENGTH_LONG).show();
                break;
        }
    }
}

Stage 5: Open res - >values - > strings.xml

In this progression we demonstrate string document which is utilized to store string information of an application.

<resources>
    <string name="app_name">CheckBoxExample</string>
    <string name="hello_world">Hello world!</string>
    <string name="action_settings">Settings</string>
    <string name="android">Android</string>
    <string name="java">Java</string>
    <string name="php">PHP</string>
    <string name="python" >Python</string>
    <string name="unity">Unity 3D</string>
</resources>



OutPut :-



No comments:

Post a Comment