Breaking

Monday 18 March 2019

Android Custom CheckBox | Android Tutoriyal | Custom CheckBox


Android Custom CheckBox

Android gives office to tweak the UI of view components instead of default.



You can make custom CheckBox in android. Thus, you can include some unique pictures of checkbox on the design.

Example Of Custom CheckBox



In this precedent, we make both default just as custom checkbox. Include the accompanying code in activity_main.xml document.

activity_main.xml

File :- activity_main.xml


<?xml version="1.0" encoding="utf-8"?>  
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    xmlns:app="http://schemas.android.com/apk/res-auto"  
    xmlns:tools="http://schemas.android.com/tools"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    tools:context="example.javatpoint.com.customcheckbox.MainActivity">  
  
  
    <TextView  
        android:id="@+id/textView1"  
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        android:gravity="center_horizontal"  
        android:textSize="25dp"  
        android:text="Default Check Box"  
        android:layout_alignParentTop="true"  
        android:layout_alignParentLeft="true"  
        android:layout_alignParentStart="true" />  
  
    <CheckBox  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:text="New CheckBox"  
        android:id="@+id/checkBox"  
        android:layout_below="@+id/textView1"  
        android:layout_centerHorizontal="true"  
        android:layout_marginTop="46dp" />  
  
    <CheckBox  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:text="New CheckBox"  
        android:id="@+id/checkBox2"  
        android:layout_below="@+id/checkBox"  
        android:layout_alignLeft="@+id/checkBox"  
        android:layout_alignStart="@+id/checkBox" />  
  
    <View  
        android:layout_width="fill_parent"  
        android:layout_height="1dp"  
        android:layout_marginTop="200dp"  
        android:background="#B8B894"  
        android:id="@+id/viewStub" />  
  
    <CheckBox  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:text="CheckBox 1"  
        android:id="@+id/checkBox3"  
        android:button="@drawable/customcheckbox"  
        android:layout_below="@+id/viewStub"  
        android:layout_centerHorizontal="true"  
        android:layout_marginTop="58dp" />  
  
    <CheckBox  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:text="CheckBox 2"  
        android:id="@+id/checkBox4"  
        android:button="@drawable/customcheckbox"  
        android:layout_below="@+id/checkBox3"  
        android:layout_alignLeft="@+id/checkBox3"  
        android:layout_alignStart="@+id/checkBox3" />  
  
    <TextView  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:textAppearance="?android:attr/textAppearanceSmall"  
        android:textSize="25dp"  
        android:text="Custom Check Box"  
        android:id="@+id/textView"  
        android:layout_alignTop="@+id/viewStub"  
        android:layout_centerHorizontal="true" />  
  
    <Button  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:text="Show Checked"  
        android:id="@+id/button"  
        android:layout_alignParentBottom="true"  
        android:layout_centerHorizontal="true" />  
  
</RelativeLayout>  



Presently actualize a selector in another document (checkbox.xml) under drawable organizer which alters the checkbox.

Checkbox.xml

File : checkbox.xml


<?xml version="1.0" encoding="utf-8"?>  

<selector xmlns:android="http://schemas.android.com/apk/res/android">  
<item android:state_checked="true" android:drawable="@drawable/checked" />  
<item android:state_checked="false" android:drawable="@drawable/unchecked"/>  

</selector>


Activity Class

File: MainActivity.java


  package example.cptech.com.customcheckbox;  
    
  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 MainActivity extends AppCompatActivity {  
      CheckBox cb1,cb2;  
      Button button;  
      @Override  
      protected void onCreate(Bundle savedInstanceState) {  
          super.onCreate(savedInstanceState);  
          setContentView(R.layout.activity_main);  
          cb1=(CheckBox)findViewById(R.id.checkBox3);  
          cb2=(CheckBox)findViewById(R.id.checkBox4);  
          button=(Button)findViewById(R.id.button);  
    
          button.setOnClickListener(new View.OnClickListener() {  
              @Override  
              public void onClick(View v) {  
                  StringBuilder sb=new StringBuilder("");  
    
                  if(cb1.isChecked()){  
                      String s1=cb1.getText().toString();  
                      sb.append(s1);  
                  }  
    
                  if(cb2.isChecked()){  
                      String s2=cb2.getText().toString();  
                      sb.append("\n"+s2);  
    
                  }  
                  if(sb!=null && !sb.toString().equals("")){  
                      Toast.makeText(getApplicationContext(), sb, Toast.LENGTH_LONG).show();  
    
                  }  
                  else{  
                      Toast.makeText(getApplicationContext(),"Nothing Selected", Toast.LENGTH_LONG).show();  
                  }  
    
              }  
    
          });  
      }  
  }


OutPut : -


No comments:

Post a Comment